# Segmented Control

> A compact row of options that switches one mode, view or filter.

```tsx title="hero.tsx"
import { SegmentedControl, SegmentedControlItem } from '@langgenius/dify-ui/segmented-control'
import { StatusDot } from '@langgenius/dify-ui/status-dot'
import * as React from 'react'

type RunFilter = 'all' | 'succeeded' | 'failed'

const runs = [
  { id: 'run-4821', name: 'Summarize support ticket', status: 'succeeded', time: '2 min ago' },
  { id: 'run-4820', name: 'Translate release notes', status: 'failed', time: '9 min ago' },
  { id: 'run-4819', name: 'Classify inbound email', status: 'succeeded', time: '14 min ago' },
  { id: 'run-4818', name: 'Generate weekly report', status: 'failed', time: '1 hr ago' },
] as const

export function SegmentedControlHeroDemo() {
  const [filter, setFilter] = React.useState<RunFilter>('all')
  const titleId = React.useId()
  const visibleRuns = filter === 'all' ? runs : runs.filter((run) => run.status === filter)

  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <div className="flex flex-wrap items-center justify-between gap-2">
        <p id={titleId} className="system-sm-semibold whitespace-nowrap text-text-primary">
          Run history
        </p>
        <SegmentedControl<RunFilter>
          value={filter}
          onValueChange={setFilter}
          aria-label="Filter runs by status"
        >
          <SegmentedControlItem<RunFilter> value="all">All</SegmentedControlItem>
          <SegmentedControlItem<RunFilter> value="succeeded">Succeeded</SegmentedControlItem>
          <SegmentedControlItem<RunFilter> value="failed">Failed</SegmentedControlItem>
        </SegmentedControl>
      </div>
      <ul aria-labelledby={titleId} className="flex flex-col divide-y divide-divider-subtle">
        {visibleRuns.map((run) => (
          <li key={run.id} className="flex items-start gap-2.5 py-2.5 first:pt-0 last:pb-0">
            <StatusDot
              status={run.status === 'succeeded' ? 'success' : 'error'}
              className="mt-1.5"
            />
            <div className="flex min-w-0 flex-1 flex-col">
              <span className="truncate system-sm-medium text-text-secondary">{run.name}</span>
              <span className="system-xs-regular text-text-tertiary">
                {run.status === 'succeeded' ? 'Succeeded' : 'Failed'} · {run.time}
              </span>
            </div>
          </li>
        ))}
      </ul>
    </div>
  )
}
```

## Usage guidelines

- Use `SegmentedControl` for one mode, view or filter for content that stays in place. Use `Tabs` when the choice swaps the panel it controls, and `RadioGroup` for one persistent field value from a small set of options that all stay visible.
- Pass `value` or `defaultValue`. One item is always selected and the selected item cannot be cleared, so the control has no empty state.
- Name the group with `aria-label`, or with `aria-labelledby` when visible text already labels it. Items take their name from their text; give an icon-only item an `aria-label`, mark its icon `aria-hidden` and pair it with a `Tooltip` that shows the same words.
- Keep the caller's value type. `SegmentedControl<Value>` and `SegmentedControlItem<Value>` accept unions and numbers; type each item when its value is not a string.
- Items render native `type="button"` elements, so they never submit a surrounding form. The control owns the track, the focus ring and the sliding selection thumb; keep its density and dimensions when adopting it.
- Use `RadioGroup` with `RadioItem` instead when the options need a custom single-choice surface, such as separate filter pills.

## Anatomy

```tsx
import {
  SegmentedControl,
  SegmentedControlDivider,
  SegmentedControlItem,
} from '@langgenius/dify-ui/segmented-control'
```

```tsx
<SegmentedControl>
  <SegmentedControlItem />
  <SegmentedControlDivider />
  <SegmentedControlItem />
</SegmentedControl>
```

## Examples

### Icons and icon-only items

Put a decorative icon before the label, or use the icon alone with an `aria-label` on the item and a `Tooltip` that shows the same words, with the item as the `render` of `TooltipTrigger`. Wrap adjacent tooltips in `TooltipProvider` so moving between items skips the open delay.

```tsx title="icons.tsx"
import { SegmentedControl, SegmentedControlItem } from '@langgenius/dify-ui/segmented-control'
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@langgenius/dify-ui/tooltip'

const layouts = [
  { value: 'grid', label: 'Grid view', icon: 'i-ri-layout-grid-line' },
  { value: 'list', label: 'List view', icon: 'i-ri-list-check' },
]

export function SegmentedControlIconsDemo() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-6">
      <SegmentedControl defaultValue="preview" aria-label="Output view">
        <SegmentedControlItem value="preview">
          <span aria-hidden="true" className="i-ri-eye-line size-4 shrink-0" />
          <span className="px-0.5">Preview</span>
        </SegmentedControlItem>
        <SegmentedControlItem value="markdown">
          <span aria-hidden="true" className="i-ri-markdown-line size-4 shrink-0" />
          <span className="px-0.5">Markdown</span>
        </SegmentedControlItem>
      </SegmentedControl>
      <TooltipProvider>
        <SegmentedControl defaultValue="grid" aria-label="App layout">
          {layouts.map((layout) => (
            <Tooltip key={layout.value}>
              <TooltipTrigger
                render={
                  <SegmentedControlItem value={layout.value} aria-label={layout.label}>
                    <span aria-hidden="true" className={`${layout.icon} size-4 shrink-0`} />
                  </SegmentedControlItem>
                }
              />
              <TooltipContent>{layout.label}</TooltipContent>
            </Tooltip>
          ))}
        </SegmentedControl>
      </TooltipProvider>
    </div>
  )
}
```

### Typed controlled value

Control the value with `value` and `onValueChange` when another part of the page reads it. Here the value is a number union, typed on the root and on each item.

```tsx title="typed-value.tsx"
import { SegmentedControl, SegmentedControlItem } from '@langgenius/dify-ui/segmented-control'
import * as React from 'react'

const pageSizes = [10, 25, 50] as const

type PageSize = (typeof pageSizes)[number]

export function SegmentedControlTypedValueDemo() {
  const [pageSize, setPageSize] = React.useState<PageSize>(25)

  return (
    <div className="flex flex-col items-start gap-2">
      <SegmentedControl<PageSize>
        value={pageSize}
        onValueChange={setPageSize}
        aria-label="Logs per page"
      >
        {pageSizes.map((size) => (
          <SegmentedControlItem<PageSize> key={size} value={size} className="tabular-nums">
            {size}
          </SegmentedControlItem>
        ))}
      </SegmentedControl>
      <p className="system-xs-regular text-text-tertiary tabular-nums">
        Showing {pageSize} logs per page.
      </p>
    </div>
  )
}
```

### Dividers and disabled items

`SegmentedControlDivider` separates items and hides itself next to the selected one. A disabled item stays visible and is skipped by the keyboard.

```tsx title="divider-disabled.tsx"
import {
  SegmentedControl,
  SegmentedControlDivider,
  SegmentedControlItem,
} from '@langgenius/dify-ui/segmented-control'
import * as React from 'react'

export function SegmentedControlDividerDisabledDemo() {
  const noteId = React.useId()

  return (
    <div className="flex flex-col items-start gap-2">
      <SegmentedControl defaultValue="7d" aria-label="Time range">
        <SegmentedControlItem value="today">Today</SegmentedControlItem>
        <SegmentedControlDivider />
        <SegmentedControlItem value="7d">7 days</SegmentedControlItem>
        <SegmentedControlDivider />
        <SegmentedControlItem value="30d">30 days</SegmentedControlItem>
        <SegmentedControlDivider />
        <SegmentedControlItem value="12m" disabled aria-describedby={noteId}>
          12 months
        </SegmentedControlItem>
      </SegmentedControl>
      <p id={noteId} className="system-xs-regular text-text-tertiary">
        12-month retention requires the Team plan.
      </p>
    </div>
  )
}
```

## Keyboard

| Key                       | Action                                                                          |
| ------------------------- | ------------------------------------------------------------------------------- |
| `Tab`                     | Moves focus into the control on the selected item, then out of it.              |
| `ArrowRight`, `ArrowDown` | Moves focus to the next enabled item and selects it, wrapping at the end.       |
| `ArrowLeft`, `ArrowUp`    | Moves focus to the previous enabled item and selects it, wrapping at the start. |

## API reference

### SegmentedControl

A single-choice segmented control with an animated selection thumb for mode, filter, or view switching. Renders a `<div>` element.

Requires either `value` or `defaultValue`. Use `Tabs` instead when each option controls a corresponding panel.

**Props**

| Prop            | Type    | Default | Description                                                                                                                                           |
| --------------- | ------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `defaultValue?` | `Value` | —       | The uncontrolled value of the radio button that should be initially selected. To render a controlled radio group, use the `value` prop instead.       |
| `value?`        | `Value` | —       | The controlled value of the radio item that should be currently selected. To render an uncontrolled radio group, use the `defaultValue` prop instead. |

Valid prop combinations:

- `defaultValue: Value`, `value?: undefined`
- `value: Value`, `defaultValue?: undefined`

Inherits `className`, `render`, `style`, `disabled`, `readOnly`, `required`, `name`, `form`, `onValueChange`, `inputRef` from [Base UI RadioGroup](https://base-ui.com/react/components/radio-group).

**Data attributes**

| Attribute       | Description                               |
| --------------- | ----------------------------------------- |
| `data-disabled` | Present when the radio group is disabled. |

### SegmentedControlDivider

Accepts the props of the element it renders.

### SegmentedControlItem

An individual option inside `SegmentedControl`. Renders a `<button>` element and a hidden `<input>` beside.

**Props**

| Prop            | Type      | Default | Description                                                                                                                                                |
| --------------- | --------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `nativeButton?` | `boolean` | `true`  | Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button. |

Inherits `className`, `render`, `style`, `value`, `disabled`, `required`, `readOnly`, `inputRef` from [Base UI Radio.Root](https://base-ui.com/react/components/radio).

**Data attributes**

| Attribute        | Description                                                                 |
| ---------------- | --------------------------------------------------------------------------- |
| `data-checked`   | Present when the radio is checked.                                          |
| `data-unchecked` | Present when the radio is not checked.                                      |
| `data-disabled`  | Present when the radio is disabled.                                         |
| `data-readonly`  | Present when the radio is readonly.                                         |
| `data-required`  | Present when the radio is required.                                         |
| `data-valid`     | Present when the radio is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the radio is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the radio has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the radio's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the radio is checked (when wrapped in Field.Root).             |
| `data-focused`   | Present when the radio is focused (when wrapped in Field.Root).             |
