import { useState } from 'react' import type { Meta, StoryObj } from '@storybook/react-vite' import { fn } from 'storybook/test' import { Accordion, type AccordionItem } from './Accordion' import { ACCORDION_VARIANTS } from './accordion.tokens' const FAQ: AccordionItem[] = [ { value: 'billing', title: 'When will I be charged?', content: 'Plans are billed monthly on the date you subscribed. Annual plans are billed once a year and include two months free.', }, { value: 'cancel', title: 'Can I cancel at any time?', content: 'Yes. Cancel from Settings → Billing and you keep access until the end of the current period. No questions asked.', }, { value: 'seats', title: 'How do seats work?', content: 'Each member of your workspace uses one seat. Guests with view-only access are free, and you can add seats whenever you need them.', }, { value: 'sso', title: 'Do you support single sign-on?', content: 'SAML single sign-on is available on the Enterprise plan.', disabled: true, }, ] const meta = { title: 'Navigation/Accordion', component: Accordion, tags: ['autodocs'], parameters: { docs: { description: { component: 'A stack of collapsible sections, built on Base UI Accordion. Each trigger is a button inside a heading (`headingLevel`, default 3) whose `aria-controls` points at its panel. Closed panels stay in the DOM as `hidden="until-found"`, so find-in-page opens them. Set `multiple` to let several sections stay open.', }, }, }, argTypes: { items: { control: false }, multiple: { control: 'boolean' }, variant: { control: 'inline-radio', options: ACCORDION_VARIANTS, table: { defaultValue: { summary: 'bordered' } } }, headingLevel: { control: 'inline-radio', options: [2, 3, 4, 5, 6], table: { defaultValue: { summary: '3' } } }, disabled: { control: 'boolean' }, value: { control: 'object' }, defaultValue: { control: 'object' }, onValueChange: { control: false }, }, args: { items: FAQ, multiple: false, variant: 'bordered', headingLevel: 3, disabled: false, defaultValue: ['billing'], onValueChange: fn(), }, decorators: [ (Story) => (
), ], } satisfies Meta export default meta type Story = StoryObj export const Playground: Story = {} const ACCOUNT: AccordionItem[] = [ { value: 'email', title: 'How do I change my email address?', content: 'Go to Settings → Profile, enter the new address and confirm it from the email we send to name@example.com.' }, { value: 'export', title: 'Can I export my data?', content: 'Yes. Settings → Data lets you download everything as CSV or JSON.' }, { value: 'delete', title: 'How do I delete my account?', content: 'Workspace owners can delete the account from Settings → Danger zone. This cannot be undone.' }, ] const SECURITY: AccordionItem[] = [ { value: '2fa', title: 'Is two-factor authentication available?', content: 'Every plan supports authenticator apps and security keys.' }, { value: 'encryption', title: 'Is my data encrypted?', content: 'Data is encrypted in transit with TLS 1.3 and at rest with AES-256.' }, { value: 'audit', title: 'Do you keep an audit log?', content: 'Business and Enterprise plans keep a searchable audit log for 12 months.' }, ] const GROUPS = { bordered: FAQ, separated: ACCOUNT, ghost: SECURITY } export const Variants: Story = { parameters: { docs: { description: { story: 'The three variants, each with its own questions.' } } }, render: (args) => (
{ACCORDION_VARIANTS.map((variant) => (

{variant}

))}
), } export const Separated: Story = { args: { variant: 'separated', defaultValue: ['cancel'] } } export const Ghost: Story = { args: { variant: 'ghost' } } export const Multiple: Story = { args: { multiple: true, defaultValue: ['billing', 'seats'] }, } export const AllClosed: Story = { args: { defaultValue: [] } } export const Controlled: Story = { render: function Render(args) { const [value, setValue] = useState(['seats']) return (
{ setValue(next) args.onValueChange?.(next) }} />

Open: {value.length ? value.join(', ') : 'none'}

) }, }