Skip to content
ReferenceVersion 6.0.2

RSegment ​

RSegment renders options in Naive UI Tabs' segment mode. Use value and onUpdateValue or v-model:value to change business state. For an uncontrolled initial value, use defaultValue. Render page content from the selected value in the caller.

Assistive technology receives a radio group. Name it with aria-label. Tab focuses the selected option. Left and Right move between options and skip disabled items. Home and End select the first and last enabled options.

PropDefaultDescription
options[]RSegmentOptions[]. Each option needs a unique key.
width'fitContent''block' fills the width. 'fitContent' sizes to content. Numbers are pixels.
themeOverridesBuilt-in segment spacingCaller overrides are shallow-merged with built-in settings.

Each option requires key: string \| number and label (text, VNode, or a render value). Optional fields include disabled, icon (VNode or function), popover (text or Naive UI Popover options with label), and slots.default. displayDirective exists in the type but is not forwarded to options, so it cannot control panel mounting. The component fixes type="segment" and its animation. Tab editing, position, pane styles, and other omitted Tabs props are outside this API.

Interactive example ​

View example source
tsx
import type { RSegmentWidth } from '@/components'
import { useI18n } from '@/hooks'
import type { ComponentSize } from '@/types'
import { RIcon } from '@ray-template/ui/icon'
import { RSegment } from '@ray-template/ui/segment'
import type { RSegmentOptions } from '@ray-template/ui/segment'
import { NCard, NFlex, NRadioButton, NRadioGroup } from 'naive-ui'
import DemoPage from '../components/DemoPage'

export default defineComponent({
  name: 'SegmentDemo',
  setup() {
    const { t } = useI18n('showcase')
    const value = ref<string | number>('overview')
    const size = ref<ComponentSize>('medium')
    const width = ref<RSegmentWidth>('fitContent')
    const options = computed<RSegmentOptions[]>(() => [
      {
        label: t('segmentOverview'),
        key: 'overview',
        icon: () => <RIcon name="navigation/dashboard" />,
      },
      {
        label: t('segmentReports'),
        key: 'reports',
        popover: t('segmentPopover'),
      },
      { label: t('segmentDisabled'), key: 'disabled', disabled: true },
    ])

    return () => (
      <DemoPage
        title={t('segmentTitle')}
        description={t('segmentDescription')}
        eyebrow="RAY TEMPLATE / EXAMPLES"
      >
        <div class="r-demo-grid">
          <NCard class="r-demo-grid__wide" title={t('segmentPlayground')}>
            <NFlex vertical size="large">
              <NFlex align="center">
                <NRadioGroup
                  v-model:value={width.value}
                  aria-label={t('segmentWidth')}
                >
                  <NRadioButton value="fitContent">fitContent</NRadioButton>
                  <NRadioButton value="block">block</NRadioButton>
                  <NRadioButton value={480}>480px</NRadioButton>
                </NRadioGroup>
                <NRadioGroup
                  v-model:value={size.value}
                  aria-label={t('segmentSize')}
                >
                  <NRadioButton value="small">S</NRadioButton>
                  <NRadioButton value="medium">M</NRadioButton>
                  <NRadioButton value="large">L</NRadioButton>
                </NRadioGroup>
              </NFlex>
              <div class="r-demo-stage">
                <RSegment
                  v-model:value={value.value}
                  options={options.value}
                  width={width.value}
                  size={size.value}
                />
              </div>
              <output>
                {t('segmentSelected', {
                  value:
                    options.value.find(({ key }) => key === value.value)
                      ?.label ?? value.value,
                })}
              </output>
            </NFlex>
          </NCard>

          <NCard title={t('segmentUncontrolled')}>
            <RSegment
              defaultValue="overview"
              options={options.value.slice(0, 2)}
            />
          </NCard>
          <NCard title={t('segmentCustom')}>
            <RSegment
              defaultValue="overview"
              options={[
                options.value[0],
                {
                  key: 'custom',
                  label: h(
                    'span',
                    { style: { fontVariantNumeric: 'tabular-nums' } },
                    t('segmentCustomLabel'),
                  ),
                },
              ]}
            />
          </NCard>
        </div>
      </DemoPage>
    )
  },
})
tsx
import { RSegment, type RSegmentOptions } from '@/components'
import { defineComponent, ref } from 'vue'

export default defineComponent({
  setup() {
    const selected = ref<string | number>('day')
    const options: RSegmentOptions[] = [
      { key: 'day', label: 'Day', popover: 'View by day' },
      { key: 'week', label: 'Week' },
    ]
    return () => (
      <RSegment
        options={options}
        value={selected.value}
        onUpdateValue={(value) => {
          selected.value = value
        }}
      />
    )
  },
})

Implementation · Option types · Omitted Tabs props