import type { Meta, StoryObj } from '@storybook/react-webpack5'; import { fn } from 'storybook/test'; import { Card } from '@transferwise/icons'; import { createSandboxStory, globalScope, } from '../../../../.storybook/components/sandbox/SandboxEditor'; import Button from '../../../Button'; import { Snackbar } from '../Snackbar'; import type { SnackbarCreationProps } from '../Snackbar.types'; import SnackbarProvider from '../SnackbarProvider'; import useSnackbar from '../useSnackbar'; const meta = { component: Snackbar, title: 'Prompts/Snackbar', tags: ['new'], parameters: { docs: { description: { component: `Snackbar provides short feedback after an action, with one optional action of its own. Use \`useSnackbar()\` inside \`SnackbarProvider\`. A newer message replaces the current one; Snackbars do not stack. Change the Playground controls, then click **Show Snackbar** to try them. Keep messages short and plain. See [Accessibility](?path=/docs/prompts-snackbar-accessibility--docs) for keyboard and announcement behaviour.`, }, }, }, argTypes: { text: { control: 'text', table: { type: { summary: 'ReactNode' } } }, sentiment: { control: 'select', options: ['neutral', 'success', 'warning', 'negative', 'proposition'], }, duration: { control: 'select', options: ['default', 'extended', 'indefinite'] }, loading: { control: 'boolean' }, media: { control: false, table: { type: { summary: 'ReactNode' } } }, action: { control: false, table: { type: { summary: 'SnackbarAction', detail: '{ label: ReactNode; href?: string; target?: string; onClick?: MouseEventHandler }', }, }, }, onDismiss: { control: false }, announcementKey: { table: { disable: true } }, onDismissRequest: { table: { disable: true } }, onExited: { table: { disable: true } }, visible: { table: { disable: true } }, timeout: { table: { disable: true } }, timestamp: { table: { disable: true } }, theme: { table: { disable: true } }, }, } satisfies Meta; export default meta; type PreviewStoryArgs = Pick< SnackbarCreationProps, 'sentiment' | 'duration' | 'loading' | 'onDismiss' > & { text: string; previewMedia: 'default' | 'card'; previewActionLabel: string; previewActionHref: string; }; function SnackbarTrigger({ label = 'Show Snackbar', ...props }: SnackbarCreationProps & { label?: string }) { const createSnackbar = useSnackbar(); return ( ); } const onActionClick = fn(); type SnackbarExample = SnackbarCreationProps & { label: string }; function SnackbarExamples({ examples }: { examples: SnackbarExample[] }) { return (
{examples.map(({ label, ...props }) => ( ))}
); } const getExamplesCode = (examples: string, withMedia = false) => `import { Button, SnackbarProvider, useSnackbar } from '@transferwise/components'; ${withMedia ? "import { Card } from '@transferwise/icons';\n" : ''} function App() { return ( ); } function ShowExamples() { const createSnackbar = useSnackbar(); const examples = ${examples}; return (
{examples.map(({ label, ...props }) => ( ))}
); } export default App;`; const getExampleCode = ({ text, sentiment, duration, loading, previewMedia, previewActionLabel, previewActionHref, }: PreviewStoryArgs) => `function App() { return ( ); } function ShowSnackbar() { const createSnackbar = useSnackbar(); return ( ); }`; /** Change the controls, then click Show Snackbar to create a message with those values. */ export const Playground: StoryObj = { args: { text: 'Changes saved', sentiment: 'success', duration: 'default', loading: false, previewMedia: 'default', previewActionLabel: '', previewActionHref: '', onDismiss: fn(), }, argTypes: { previewMedia: { name: 'Preview with custom media', control: 'select', options: ['default', 'card'], table: { category: 'Storybook Preview options' }, }, previewActionLabel: { name: 'Action label (empty for no action)', control: 'text', table: { category: 'Storybook Preview options' }, }, previewActionHref: { name: 'Action href (empty for callback only)', control: 'text', table: { category: 'Storybook Preview options' }, }, }, render: ({ previewMedia, previewActionLabel, previewActionHref, ...props }) => ( : undefined} action={ previewActionLabel ? { label: previewActionLabel, href: previewActionHref || undefined, onClick: onActionClick, } : undefined } /> ), parameters: { chromatic: { disableSnapshot: true }, docs: { source: { type: 'dynamic', transform: (source: string, { args }: { args: PreviewStoryArgs }) => `import { Button, SnackbarProvider, useSnackbar } from '@transferwise/components';\n${args.previewMedia === 'card' ? "import { Card } from '@transferwise/icons';\n" : ''}\n${getExampleCode(args)}\n\nexport default App;`, }, }, }, }; export const Sandbox = createSandboxStory({ code: getExampleCode({ text: 'Card added', sentiment: 'neutral', duration: 'extended', previewMedia: 'card', previewActionLabel: 'View', previewActionHref: '', }), scope: globalScope, }); /** * Choose a sentiment to match the message. Each has default media; all use polite announcements. * Show one at a time using the buttons below. */ export const Sentiments: StoryObj = { render: () => ( ({ label: sentiment, sentiment, text: 'Your details have been updated' }), )} /> ), parameters: { controls: { disable: true }, chromatic: { disableSnapshot: true }, docs: { source: { code: getExamplesCode(`(['neutral', 'success', 'warning', 'negative', 'proposition'] as const).map( (sentiment) => ({ label: sentiment, sentiment, text: 'Your details have been updated' }), )`), }, }, }, }; /** * Default media follows the sentiment. Override it with a custom icon, including for neutral and * proposition. Wise icons fill the 24×24px slot without an explicit size. */ export const MediaTypes: StoryObj = { render: () => ( }, { label: 'Custom proposition', text: 'Your new card is ready', sentiment: 'proposition', media: , }, ]} /> ), parameters: { controls: { disable: true }, chromatic: { disableSnapshot: true }, docs: { source: { code: getExamplesCode( `[ { label: 'Default media', text: 'Changes saved', sentiment: 'success' as const }, { label: 'Custom neutral', text: 'Card added', media: }, { label: 'Custom proposition', text: 'Your new card is ready', sentiment: 'proposition' as const, media: , }, ]`, true, ), }, }, }, }; /** Loading replaces media, uses neutral styling and prevents auto-dismiss, regardless of duration. */ export const Loading: StoryObj = { ...Playground, args: { ...Playground.args, text: 'Uploading document', loading: true }, }; /** * Add one action: a callback with `onClick`, navigation with `href`, or both. * Activating it requests dismissal after the callback runs. */ export const WithAction: StoryObj = { render: () => ( ), parameters: { controls: { disable: true }, chromatic: { disableSnapshot: true }, docs: { source: { code: getExamplesCode(`[ { label: 'Callback action', text: 'Transfer deleted', duration: 'extended' as const, action: { label: 'Undo', onClick: () => console.log('Undo clicked') }, }, { label: 'Navigation action', text: 'Your transfer is ready', duration: 'extended' as const, action: { label: 'View transfer', href: '/transfers/123', target: '_blank' }, }, ]`), }, }, }, }; /** * `default` lasts 4 seconds, `extended` lasts 6, and `indefinite` has no timer. * Timers begin after entry and pause during hover, focus or a swipe, then resume with the remaining time. */ export const Durations: StoryObj = { render: () => ( ), parameters: { controls: { disable: true }, chromatic: { disableSnapshot: true }, docs: { source: { code: getExamplesCode(`[ { label: 'Default (4s)', text: 'Changes saved', duration: 'default' as const }, { label: 'Extended (6s)', text: 'Transfer deleted', duration: 'extended' as const, action: { label: 'Undo', onClick: () => console.log('Undo clicked') }, }, { label: 'Indefinite', text: 'Uploading document', duration: 'indefinite' as const, loading: true, }, ]`), }, }, }, }; /** * Dismiss with Escape, the desktop close button, an action, or a downward swipe/drag of at least half * the surface height. `onDismiss` fires after exit; check the Actions panel to see it. */ export const Dismissal: StoryObj = { ...Playground, args: { ...Playground.args, duration: 'indefinite' }, };