For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/v/0.6.0/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/v/0.6.0/en/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/v/0.6.0/en/components/segmented.md.
  • English
  • Segmented

    Switch between related options.

    Basic usage

    Current view: Weekly
    basic.tsx
    import { Segmented, Space, Text } from '@ifhover/leaf-ui';
    
    import { useState } from 'react';
    
    export function SegmentedBasic() {
      const [value, setValue] = useState('Weekly');
      return (
        <Space direction="vertical" align="stretch">
          <Segmented options={['Daily', 'Weekly', 'Monthly']} value={value} onChange={setValue} />
          <output>
            <Text tone="muted">Current view: {value}</Text>
          </output>
        </Space>
      );
    }
    

    More examples

    Fill container
    Small size
    advanced.tsx
    import { Segmented, Space, Text } from '@ifhover/leaf-ui';
    
    export function SegmentedOptions() {
      return (
        <Space direction="vertical" align="stretch">
          <Text tone="muted">Fill container</Text>
          <Segmented
            block
            options={[
              { value: 'list', label: 'List' },
              { value: 'grid', label: 'Grid' },
              { value: 'archived', label: 'Archive (unavailable', disabled: true },
            ]}
          />
          <Text tone="muted">Small size</Text>
          <Segmented size="sm" options={['A', 'B', 'C']} />
        </Space>
      );
    }
    

    Usage

    The default width follows the content; block fills the container. options accepts strings or labeled options with icons. Control selection with value / onChange and switch options using arrow keys. Set name to include the value in native form submissions.

    Types

    Import these types for data, callbacks and refs.

    import type { SegmentedOption, SegmentedProps } from '@ifhover/leaf-ui';

    SegmentedOption API

    PropertyTypeDefaultDescription
    valuestringRequiredControlled value
    labelReactNodeRequiredDisplay label
    iconReactNode—Custom icon
    disabledboolean—Disable interactions

    SegmentedProps API

    PropertyTypeDefaultDescription
    optionsreadonly (string | SegmentedOption)[]RequiredAvailable options
    valuestring—Controlled value
    defaultValuestring—Initial value, restored by uncontrolled form resets
    onChange(value: string) => void—Value change callback
    disabledboolean—Disable interactions
    size'sm' | 'md' | 'lg''md'Size or spacing preset
    blockbooleanfalseFill container width
    namestring—Form field name
    formstring—Associated form ID