import { useState, useEffect, useRef, type ReactNode } from 'react'; import { createPortal } from 'react-dom'; import { Meta, StoryObj, Decorator } from '@storybook/react-webpack5'; import { fn } from 'storybook/test'; import { Bank, Briefcase, Star, Travel } from '@transferwise/icons'; import { createSandboxStory, globalScope, } from '../../../../.storybook/components/sandbox/SandboxEditor'; import Button from '../../../Button'; import { CriticalBanner, type CriticalBannerProps } from '..'; /** * **Design guidance**: wise.design/components/critical-banner * * Can be controlled or uncontrolled. When `onToggle` is provided, the consumer manages the expanded state via `expanded` and the banner calls `onToggle` when the user clicks the chevron. When `onToggle` is not provided, the banner manages its own expanded state internally and toggles it when the user clicks the chevron. */ const meta = { component: CriticalBanner, title: 'Prompts/CriticalBanner', tags: ['new'], parameters: { docs: { toc: true }, padding: '0' }, args: { title: 'Your account requires verification', description: 'Please verify your identity to continue using all features.', sentiment: 'negative', expanded: true, action: { label: 'Verify now', onClick: fn() }, actionSecondary: undefined, media: undefined, animateEntry: true, id: undefined, className: undefined, 'data-testid': undefined, onToggle: fn(), }, argTypes: { sentiment: { control: 'radio', options: ['negative', 'warning', 'neutral', 'success'], }, title: { control: 'text', table: { type: { summary: 'ReactNode' } }, }, description: { control: 'text', table: { type: { summary: 'ReactNode' } }, }, expanded: { control: 'boolean' }, animateEntry: { control: 'boolean' }, media: { control: false, description: 'Replaces the default Status Icon with a custom image or AvatarView.', table: { type: { summary: 'PromptMedia', detail: '{ imgSrc?: string; avatar?: { imgSrc?: string; profileName?: string; profileType?: string; asset?: ReactNode; badge?: { flagCode: string } }; aria-label?: string }', }, }, }, action: { control: false, table: { type: { summary: 'CriticalBannerAction', detail: '{ label: ReactNode; onClick?: () => void; href?: string; target?: string }', }, }, }, actionSecondary: { control: false, table: { type: { summary: 'CriticalBannerActionSecondary', detail: '{ label: ReactNode; onClick?: () => void; href?: string; target?: string }', }, }, }, id: { table: { category: 'Common' } }, className: { table: { category: 'Common' } }, 'data-testid': { table: { category: 'Common' } }, onToggle: { table: { disable: true } }, }, } satisfies Meta; export default meta; type Story = StoryObj; type PreviewStoryArgs = CriticalBannerProps & { previewMedia: CriticalBannerProps['media']; previewSecondaryAction: boolean; }; const MEDIA_OPTIONS: Record = { undefined, 'Custom image': { imgSrc: '../../wise-card.svg' }, 'Avatar: Photo': { avatar: { imgSrc: '../../avatar-rectangle-fox.webp' } }, 'Avatar: Business (GB)': { avatar: { profileType: 'BUSINESS', badge: { flagCode: 'GB' } }, }, 'Avatar: Personal (EU)': { avatar: { profileType: 'PERSONAL', badge: { flagCode: 'EU' } }, }, 'Avatar: Bank icon': { avatar: { asset: } }, 'Avatar: Star icon': { avatar: { asset: } }, 'Avatar: Travel icon': { avatar: { asset: } }, 'Avatar: Briefcase icon': { avatar: { asset: } }, 'Avatar: Initials': { avatar: { profileName: 'John Doe' } }, }; const previewArgTypes = { previewMedia: { name: 'Preview with `media`', control: 'select', options: Object.keys(MEDIA_OPTIONS), mapping: MEDIA_OPTIONS, table: { category: 'Storybook Preview' }, }, previewSecondaryAction: { name: 'Preview with `actionSecondary`', control: 'boolean', table: { category: 'Storybook Preview' }, }, } as const; const hiddenArgTypes = { sentiment: { table: { disable: true } }, title: { table: { disable: true } }, description: { table: { disable: true } }, action: { table: { disable: true } }, actionSecondary: { table: { disable: true } }, media: { table: { disable: true } }, expanded: { table: { disable: true } }, animateEntry: { table: { disable: true } }, onToggle: { table: { disable: true } }, id: { table: { disable: true } }, className: { table: { disable: true } }, 'data-testid': { table: { disable: true } }, } as const; const AnimatedEntryPreview = (args: CriticalBannerProps) => { const [bannerInstance, setBannerInstance] = useState(0); return ( <>
); }; /** Renders children inside an iframe that inherits the parent page's stylesheets. */ function IframeContainer({ width, children }: { width: number; children: ReactNode }) { const iframeRef = useRef(null); const [mountNode, setMountNode] = useState(null); useEffect(() => { const iframe = iframeRef.current; if (!iframe) return; const doc = iframe.contentDocument; if (!doc) return; doc.head.replaceChildren( ...Array.from(document.head.querySelectorAll('link[rel="stylesheet"], style')).map((node) => node.cloneNode(true), ), ); doc.body.style.margin = '0'; setMountNode(doc.body); }, []); return ( ); } const ContainerDecorator = (gap = '1rem'): Decorator => (Story) => (
); /** * Interactive playground with all controls. */ export const Playground: StoryObj = { args: { previewMedia: undefined, previewSecondaryAction: true, }, argTypes: { ...previewArgTypes, }, render: function Render(args: PreviewStoryArgs) { const { previewMedia, previewSecondaryAction, ...props } = args; const [expanded, setExpanded] = useState(args.expanded); useEffect(() => { setExpanded(args.expanded); }, [args.expanded]); return ( setExpanded(!expanded)} /> ); }, }; export const Sandbox = createSandboxStory({ code: `const App = () => { const [expanded, setExpanded] = React.useState(true); return ( console.log('verify') }} actionSecondary={{ label: 'Learn more', onClick: () => console.log('learn more') }} media={{ avatar: { asset: } }} expanded={expanded} onToggle={() => setExpanded((prev) => !prev)} /> ); };`, scope: globalScope, }); /** * There are four sentiments, with negative being the default. */ export const Sentiments: Story = { decorators: [ContainerDecorator('2rem')], parameters: { docs: { source: { code: ` `, }, }, }, argTypes: hiddenArgTypes, render: function Render() { return ( <> {(['negative', 'warning', 'neutral', 'success'] as const).map((sentiment) => ( ))} ); }, }; /** * Banners can have just a primary action, both primary and secondary, or no actions at all. */ export const Actions: Story = { decorators: [ContainerDecorator('2rem')], parameters: { docs: { source: { code: ` {} }} /> {} }} actionSecondary={{ label: 'Contact support', onClick: () => {} }} />`, }, }, }, argTypes: hiddenArgTypes, render: function Render() { return ( <> ); }, }; /** * Each sentiment has a default status icon. Override it with a custom image or an AvatarView. */ export const MediaTypes: Story = { args: { sentiment: 'neutral', }, decorators: [ContainerDecorator('2rem')], parameters: { docs: { source: { code: `{/* Change sentiment using the story controls */} {/* Default — sentiment icon */} {/* Custom image */} {/* Avatar with photo */} {/* Avatar with initials */} {/* Avatar with custom icon */} } }} ... /> {/* Business Profile with flag badge */} `, }, }, }, argTypes: { ...hiddenArgTypes, sentiment: { control: 'radio', options: ['negative', 'warning', 'neutral', 'success'], }, }, render: function Render({ sentiment }) { const [expandedStates, setExpandedStates] = useState>({ default: true, image: true, avatarPhoto: true, avatarInitials: true, avatarIcon: true, businessBadge: true, personalBadge: true, }); const toggle = (key: string) => setExpandedStates((prev) => ({ ...prev, [key]: !prev[key] })); return ( <> toggle('default')} /> toggle('image')} /> toggle('avatarPhoto')} /> toggle('avatarInitials')} /> } }} action={{ label: 'Connect bank', onClick: fn() }} expanded={expandedStates.avatarIcon} onToggle={() => toggle('avatarIcon')} /> toggle('businessBadge')} /> ); }, }; /** * The banner can be fully controlled — the parent can pass in the `expanded` state. * Clicking the chevron calls `onToggle`, and external buttons can also toggle expansion. * We recommend tracking the state that the user has set `expanded` to and keeping it there on page refresh. * * When the banner is shown on wider screens, it will always be fully expanded. */ export const Expanded: Story = { argTypes: hiddenArgTypes, render: function Render() { const [expanded, setExpanded] = useState(true); return ( <>
setExpanded(!expanded)} /> ); }, parameters: { docs: { source: { code: `const [expanded, setExpanded] = useState(true); setExpanded(!expanded)} />`, }, }, }, }; /** * When the banner has no title, the description remains visible when collapsed and * is clamped to 2 lines. With a title, only the title is shown when collapsed. */ export const CollapsedBehaviour: Story = { decorators: [ContainerDecorator()], argTypes: hiddenArgTypes, render: function Render() { const [expanded1, setExpanded1] = useState(false); const [expanded2, setExpanded2] = useState(false); return ( setExpanded1(!expanded1)} /> setExpanded2(!expanded2)} /> ); }, parameters: { docs: { canvas: { sourceState: 'hidden' }, }, }, }; /** * Above 768px the collapse toggle disappears and the banner is always fully expanded. The buttons are not block displayed. * Below 768px the user can collapse and expand the banner and the buttons are displayed as block elements. * Eventually the buttons stack. */ export const Responsiveness: Story = { parameters: { docs: { canvas: { sourceState: 'hidden' }, }, }, argTypes: hiddenArgTypes, render: function Render() { return (
{[200, 350, 800].map((width) => ( ))}
); }, }; /** * Mobile viewport regression test for the overhang. * The overhang is always applied below 600px. */ export const MobileOverhang: Story = { argTypes: hiddenArgTypes, render: function Render() { return (
); }, parameters: { docs: { canvas: { sourceState: 'hidden' }, }, }, }; /** The banner waits 500ms, then reveals from above when it enters the page. */ export const AnimatedEntry: Story = { args: { title: 'Your account is restricted.', description: 'Review your personal details to get your account active again.', action: { label: 'Review details', onClick: fn() }, }, argTypes: hiddenArgTypes, render: (args) => , parameters: { layout: 'fullscreen', docs: { canvas: { sourceState: 'hidden' }, }, }, }; /** The banner can mount immediately without replaying its entry animation. */ export const WithoutEntryAnimation: Story = { args: { animateEntry: false, title: 'Your account is restricted.', description: 'Review your personal details to get your account active again.', action: { label: 'Review details', onClick: fn() }, }, argTypes: hiddenArgTypes, parameters: { layout: 'fullscreen', docs: { canvas: { sourceState: 'hidden' }, }, }, }; /** * The text container is capped at 480px for optimal readability, even though * the banner itself stretches to the full available width. */ export const ParagraphWidth: Story = { parameters: { docs: { canvas: { sourceState: 'hidden' }, }, }, argTypes: hiddenArgTypes, render: function Render() { const [expanded, setExpanded] = useState(true); return ( setExpanded(!expanded)} /> ); }, };