const createSourceCode = (args: ToastProps) => {
const props = Object.entries(args)
.filter(([, value]) => value !== undefined && value !== '')
.map(([key, value]) => {
if (key === 'action') {
return ` action: { label: 'Undo', onClick: () => alert('Action clicked!') }`
}
if (typeof value === 'string') {
return ` ${key}: '${value}'`
}
return ` ${key}: ${value}`
})
.join(',\n')
return `
const Component = () => {
const handleShowToast = () => {
toast({
${props}
});
};
return (
<>
>
);
}
`
}
export const AllVariants: Story = {
render: () => (
alert('Action clicked!') }}
/>
alert('Action clicked!') }}
/>
alert('Action clicked!') }}
/>
alert('Action clicked!') }}
/>
),
}
const allArgs: ToastProps = {
title: 'Success toast',
variant: 'success',
description: 'This toast has all elements.',
}
export const AllElements: Story = {
args: {
...allArgs,
action: { label: 'Undo', onClick: () => alert('Action clicked!') },
},
}
AllElements.parameters = {
docs: {
source: { code: createSourceCode(AllElements.args as ToastProps) },
},
}
export const WithoutAction: Story = {
args: {
...allArgs,
title: 'Warning toast',
variant: 'warning',
description: 'This toast has no action.',
},
}
WithoutAction.parameters = {
docs: {
source: { code: createSourceCode(WithoutAction.args as ToastProps) },
},
}
export const WithoutClose: Story = {
args: {
...allArgs,
title: 'Error toast',
variant: 'error',
close: false,
description: 'This toast has no close button.',
action: { label: 'Undo', onClick: () => alert('Action clicked!') },
},
}
WithoutClose.parameters = {
docs: {
source: { code: createSourceCode(WithoutClose.args as ToastProps) },
},
}
export const WithoutCountdown: Story = {
args: {
...allArgs,
title: 'Info toast',
variant: 'info',
countdown: false,
description: 'This toast has no countdown.',
},
}
WithoutCountdown.parameters = {
docs: {
source: { code: createSourceCode(WithoutCountdown.args as ToastProps) },
},
}