Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.6 KB
Raw Blame
import type { Meta, StoryObj } from '@storybook/react-vite'
import * as React from 'react'
import { SegmentedControl, SegmentedControlDivider, SegmentedControlItem } from '.'

const meta = {
  title: 'Base/UI/SegmentedControl',
  component: SegmentedControl,
  parameters: {
    layout: 'centered',
    docs: {
      description: {
        component:
          'Required single-choice segmented control built on Base UI RadioGroup and Radio. Use it for mode, filter, and view selection that does not need tabpanel semantics.',
      },
    },
  },
  tags: ['autodocs'],
  args: {
    defaultValue: 'one',
  },
} satisfies Meta<typeof SegmentedControl>

export default meta
type Story = StoryObj<typeof meta>

type SegmentedControlProps = {
  defaultValue: string
  values: string[]
  iconOnly?: boolean
  noPadding?: boolean
}

function Icon() {
  return <i className="i-ri-information-line size-4 shrink-0" aria-hidden="true" />
}

function Item() {
  return (
    <React.Fragment>
      <Icon />
      <span className="px-0.5">Item</span>
    </React.Fragment>
  )
}

function SegmentedControlExample({
  defaultValue,
  values,
  iconOnly = false,
  noPadding = false,
}: SegmentedControlProps) {
  return (
    <SegmentedControl
      defaultValue={defaultValue}
      aria-label="Segmented control"
      className={noPadding ? 'rounded-lg border-[0.5px] border-divider-subtle p-0' : undefined}
    >
      {values.map((itemValue, index) => (
        <span key={itemValue} className="relative flex items-center">
          <SegmentedControlItem
            value={itemValue}
            aria-label={iconOnly ? `Item ${index + 1}` : undefined}
          >
            <Icon />
            {!iconOnly && <span className="px-0.5">Item</span>}
          </SegmentedControlItem>
          {index === 1 && (
            <span
              className="pointer-events-none absolute -inset-e-px top-0 flex h-full items-center"
              aria-hidden="true"
            >
              <SegmentedControlDivider />
            </span>
          )}
        </span>
      ))}
    </SegmentedControl>
  )
}

function SpecColumn() {
  const values = ['one', 'two', 'three']

  return (
    <div className="flex flex-col items-center gap-6">
      <SegmentedControlExample defaultValue="one" values={values} />
      <SegmentedControlExample defaultValue="one" values={values} iconOnly />
      <SegmentedControlExample defaultValue="one" values={values} noPadding />
      <SegmentedControlExample defaultValue="one" values={values} iconOnly noPadding />
    </div>
  )
}

function SpecPanel({ className, children }: { className?: string; children: React.ReactNode }) {
  return (
    <div className={className}>
      <div className="flex min-h-105 items-center justify-center">{children}</div>
    </div>
  )
}

export const DesignSpec: Story = {
  render: () => (
    <div className="overflow-hidden rounded-3xl bg-components-panel-bg-alt p-4">
      <SpecPanel className="w-120 overflow-hidden rounded-2xl bg-components-chart-bg">
        <SpecColumn />
      </SpecPanel>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story:
          'Figma node 2473:9851: segmented control examples with text+icon and icon-only rows, with and without outer padding.',
      },
    },
  },
}

export const DataAttributeStates: Story = {
  render: () => (
    <div className="flex flex-col gap-5">
      <SegmentedControl defaultValue="active" aria-label="Basic states">
        <SegmentedControlItem value="default">
          <Item />
        </SegmentedControlItem>
        <SegmentedControlItem value="active">
          <Item />
        </SegmentedControlItem>
        <SegmentedControlItem value="disabled" disabled>
          <Item />
        </SegmentedControlItem>
      </SegmentedControl>

      <SegmentedControl defaultValue="accent-light" aria-label="Active states">
        <SegmentedControlItem value="accent-light">
          <Item />
        </SegmentedControlItem>
        <SegmentedControlItem value="neutral" className="data-checked:text-text-primary">
          <Item />
        </SegmentedControlItem>
        <SegmentedControlItem
          value="accent"
          className="data-checked:border-components-segmented-control-item-active-accent-border data-checked:bg-components-segmented-control-item-active-accent-bg data-checked:text-text-accent"
        >
          <Item />
        </SegmentedControlItem>
      </SegmentedControl>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story:
          '`SegmentedControlItem` gets `data-checked` and `data-disabled` from Base UI Radio. Accent and neutral states are composed through props and className.',
      },
    },
  },
}