Components
Segment group

Segment Group

The SegmentGroup component offers an effective way to organize and navigate between multiple sections within a single view. It provides a clear and compact option to improve usability and accessibility.

Usage

Start by importing the necessary components from the package:

import {
  Segment,
  SegmentControl,
  SegmentGroup,
  SegmentGroupIndicator,
  SegmentLabel,
} from '@ark-ui/react'

Here are some examples of how to use the SegmentGroup component:

import { useState } from 'react'
import { Segment, SegmentControl, SegmentGroup, SegmentGroupIndicator, SegmentLabel } from './index'

const Basic = () => (
  <SegmentGroup>
    <SegmentGroupIndicator />
    {options.map((option, id) => (
      <Segment key={id} value={option.id}>
        <SegmentLabel>{option.label}</SegmentLabel>
        <SegmentControl />
      </Segment>
    ))}
  </SegmentGroup>
)

Initial Value

To set a default segment on initial render, use the defaultValue prop:

import { useState } from 'react'
import { Segment, SegmentControl, SegmentGroup, SegmentGroupIndicator, SegmentLabel } from './index'

const InitialValue = () => (
  <SegmentGroup defaultValue="react">
    <SegmentGroupIndicator />
    {options.map((option, id) => (
      <Segment key={id} value={option.id}>
        <SegmentLabel>{option.label}</SegmentLabel>
        <SegmentControl />
      </Segment>
    ))}
  </SegmentGroup>
)

Controlled SegmentGroup

To create a controlled SegmentGroup component, manage the current selected segment using the value prop and update it when the onChange event handler is called:

import { useState } from 'react'
import { Segment, SegmentControl, SegmentGroup, SegmentGroupIndicator, SegmentLabel } from './index'

const Controlled = () => {
  const [value, setValue] = useState('react')
  return (
    <SegmentGroup value={value} onValueChange={(e) => setValue(e.value)}>
      <SegmentGroupIndicator />
      {options.map((option, id) => (
        <Segment key={id} value={option.id}>
          <SegmentLabel>{option.label}</SegmentLabel>
          <SegmentControl />
        </Segment>
      ))}
    </SegmentGroup>
  )
}

Disabled Segment

To disable a segment, simply pass the disabled prop to the Segment component:

import { useState } from 'react'
import { Segment, SegmentControl, SegmentGroup, SegmentGroupIndicator, SegmentLabel } from './index'

const Disabled = () => (
  <SegmentGroup defaultValue="react">
    <SegmentGroupIndicator />
    {options.map((option, id) => (
      <Segment key={id} value={option.id} disabled={option.id === 'svelte'}>
        <SegmentLabel>{option.label}</SegmentLabel>
        <SegmentControl />
      </Segment>
    ))}
  </SegmentGroup>
)

Conclusion

The SegmentGroup component provides a versatile and adaptable solution for introducing segmented navigation to your applications. With a broad variety of customization options and features, it can cater to various use cases and designs.

Previous

Rating Group

Next

Select