Skip to Content
Cristal

Forms

Segmented control

Pick one of a few options shown side by side, with a sliding thumb; or tabs, one panel visible at a time.

<cr-segmented><cr-tab-panel>CrSegmentedCrTabPanel

The control has two modes, picked by its children:

  • option elements: a radio group, whose value can be sent with a form;
  • CrTabPanel elements: tabs, each with its content panel, one visible at a time.

The four variant values and fit apply to both modes.

Example

Options are child option elements, as with CrSelect. A thumb slides under the chosen option with a slight spring.

variantEffect
thumbSliding Thumb: a raised thumb, the chosen label goes bold (default)
clipClip Text: a coloured thumb; while it slides, text turns white only where it covers it
underlineno track: a bar slides under the chosen label, the classic tabs look
outlinebordered track, tinted thumb ringed with the primary colour

Chosen period: month

'use client';

import { useState } from 'react';
import { CrBox, CrSegmented } from '@cristal-ui/react';

const variants = ['thumb', 'clip', 'underline', 'outline'] as const;

export default function SegmentedBasic() {
  const [period, setPeriod] = useState('month');

  return (
    <CrBox display="flex" direction="column" gap="xl" align="center">
      {variants.map((variant) => (
        <CrSegmented key={variant} label={`Period, ${variant} style`} variant={variant} value={period} onCrChange={(event) => setPeriod(event.detail)}>
          <option value="day">Day</option>
          <option value="week">Week</option>
          <option value="month">Month</option>
          <option value="year">Year</option>
        </CrSegmented>
      ))}
      <p>Chosen period: {period}</p>
    </CrBox>
  );
}

Counts

data-count on an option adds a count next to its label. When the option is picked, its count counts up to its value and pops.

import { CrSegmented } from '@cristal-ui/react';

export default function SegmentedCounts() {
  return (
    <CrSegmented label="Completed tasks" value="month">
      <option value="day" data-count="4">Day</option>
      <option value="week" data-count="12">Week</option>
      <option value="month" data-count="38">Month</option>
      <option value="year" data-count="96">Year</option>
    </CrSegmented>
  );
}

Fitted width

By default, segments all have the same width. With fit, each one hugs its label and the thumb resizes as it slides. A disabled option cannot be picked.

import { CrSegmented } from '@cristal-ui/react';

export default function SegmentedFit() {
  return (
    <CrSegmented label="View" fit value="all">
      <option value="all">All</option>
      <option value="unread">Unread</option>
      <option value="mentions">Mentions</option>
      <option value="archived" disabled>
        Archived
      </option>
    </CrSegmented>
  );
}

Tabs

With CrTabPanel children instead of option elements, the control becomes tabs: each panel's label becomes a tab, its content the panel. value names the chosen tab by its panel's value, or else by its position as a string ("0", "1"…); the first tab by default. onCrChange returns that value. In tabs mode, the control sends nothing with a form.

The content comes in from the side of the chosen tab: from the right when moving forward, from the left when moving back. With underline, the base line runs the full width; add fit for tabs that hug their labels.

Solid oak desk lamp, washed linen shade.

Height 45 cm, E27 bulb included, 2 m fabric cable.

4.6 out of 5: “Lovely warm light, assembled in two minutes.”

Solid oak desk lamp, washed linen shade.

Height 45 cm, E27 bulb included, 2 m fabric cable.

4.6 out of 5: “Lovely warm light, assembled in two minutes.”

'use client';

import { useState } from 'react';
import { CrBox, CrSegmented, CrTabPanel } from '@cristal-ui/react';

const variants = ['underline', 'thumb'] as const;

export default function SegmentedTabs() {
  const [tab, setTab] = useState('description');

  return (
    <CrBox display="flex" direction="column" gap="xl">
      {variants.map((variant) => (
        <CrSegmented key={variant} label={`Product sheet, ${variant} style`} variant={variant} fit value={tab} onCrChange={(event) => setTab(event.detail)}>
          <CrTabPanel label="Description" value="description">
            <p>Solid oak desk lamp, washed linen shade.</p>
          </CrTabPanel>
          <CrTabPanel label="Specifications" value="specs">
            <p>Height 45 cm, E27 bulb included, 2 m fabric cable.</p>
          </CrTabPanel>
          <CrTabPanel label="Reviews (128)" value="reviews">
            <p>4.6 out of 5: “Lovely warm light, assembled in two minutes.”</p>
          </CrTabPanel>
        </CrSegmented>
      ))}
    </CrBox>
  );
}

Panels that are not chosen are hidden by the component itself: imported from @cristal-ui/react/ssr, tabs arrive in the right state, with no flash of the other panels.

If the user asked their system to reduce motion, the thumb jumps straight to the chosen option or tab, counts do not move and the panel appears without sliding.

Accessibility

With options

  • The control is a radio group (role="radiogroup") named by label, which is required since there is no visible label.
  • A single Tab stop enters the group, on the chosen option; arrow keys pick the previous or next option (wrapping), Home and End the first or last one.
  • The count is read with the label ("Month (38)"); the counting number is not read.
  • With name, the chosen value is sent with the form.

With tabs

  • The component follows the W3C tabs pattern: role="tablist" named by label, one role="tab" per tab with aria-selected and aria-controls, and a role="tabpanel" named by its tab (aria-labelledby).
  • A single Tab stop enters the tab bar, on the chosen tab; one more Tab goes into the panel.
  • Automatic activation: Right Arrow and Left Arrow pick the next or previous tab (wrapping), Home and End the first or last one.
  • The chosen tab does not rely on colour alone: the thumb or the thick bar is under it.

API

Properties

PropertyTypeDefaultDescription
disabledbooleanfalseDésactive le contrôle.
fitbooleanfalseSegments à la largeur de leur libellé, la pastille change de largeur en glissant ; sinon largeurs égales.
label (obligatoire)string—Nom lu par les lecteurs d'écran (le contrôle, ou la barre d'onglets, n'a pas de libellé visible).
namestring—Nom envoyé avec le formulaire ; sans effet en mode onglets.
valuestring''Valeur de l'option choisie ; la première option disponible par défaut. En mode onglets : value du cr-tab-panel choisi, ou à défaut sa position en texte ("0", "1"…).
variant"clip" | "outline" | "thumb" | "underline"'thumb'Style : thumb (pastille en relief, libellé en gras), clip (pastille colorée, le texte ne devient blanc que là où elle passe), underline (trait qui glisse sous le libellé) ou outline (pastille teintée et cerclée dans un rail bordé).

Events

Reactevent.detailDescription
onCrChangestringÉmis quand l'option (ou l'onglet) choisie change ; detail : sa valeur.

Slots

SlotDescription
defaultLes cr-tab-panel, en mode onglets.

CSS parts

To style it from outside: cr-segmented::part(name) { … }

PartDescription
panelLa zone du panneau affiché, en mode onglets.
segmentUn segment (un onglet en mode onglets).
thumbLa pastille (ou le trait) qui glisse sous le segment choisi ; absente avec variant clip.

CrTabPanel API

Properties

PropertyTypeDefaultDescription
label (obligatoire)string—Libellé de l'onglet.
valuestring—Valeur de l'onglet, reprise par value et crChange de cr-segmented ; sa position en texte ("0", "1"…) par défaut.

Slots

SlotDescription
defaultLe contenu du panneau.

CSS parts

To style it from outside: cr-tab-panel::part(name) { … }

PartDescription

On this page