import { useEffect, useRef, type ReactElement } from 'react' import type { Decorator, Preview } from '@storybook/react-vite' import '../src/index.css' import { applyPalette } from '../src/theme/applyPalette' import { palettes, DEFAULT_PALETTE, type PaletteName } from '../src/theme/palettes' import { tansyTheme } from './tansyTheme' /** * Wraps every story in a themed surface. * * The palette globals are set on this wrapper rather than on * `document.documentElement` so a story can opt into a *different* palette on * its own subtree without fighting the global one, and so the docs page keeps * its own chrome. * * Declared as a named component (not an inline arrow) so the rules-of-hooks lint * recognises the hook calls below. */ function PaletteSurface({ palette, children }: { palette: PaletteName; children: ReactElement }) { const ref = useRef(null) useEffect(() => { if (ref.current) applyPalette(ref.current, palette) }, [palette]) return (
{children}
) } const withPalette: Decorator = (Story, context) => { const palette = (context.globals.palette ?? DEFAULT_PALETTE) as PaletteName return } const preview: Preview = { title: 'Tansy UI / Component Workshop', decorators: [withPalette], globalTypes: { palette: { description: 'Brand palette applied to every element in the canvas.', toolbar: { title: 'Palette', icon: 'paintbrush', items: Object.values(palettes).map((p) => ({ value: p.name, title: `${p.label} — ${p.note}`, })), dynamicTitle: true, }, }, }, initialGlobals: { palette: DEFAULT_PALETTE, }, parameters: { docs: { theme: tansyTheme, }, options: { storySort: { order: ['Welcome', 'Actions', 'Forms', 'Data entry', 'Feedback', 'Display', 'Layout', 'Navigation', 'Overlays'], }, }, backgrounds: { default: 'showcase', values: [ { name: 'showcase', value: '#f3f4f6' }, { name: 'surface', value: '#ffffff' }, { name: 'dark', value: '#12151c' }, ], }, controls: { matchers: { color: /(background|color)$/i, date: /Date$/i, }, }, }, } export default preview