// Each exported component in the package should have its own stories file import { SyntheticEvent, useEffect, useState } from 'react'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { Alert, AlertTitle } from '@availity/mui-alert'; import { Button } from '@availity/mui-button'; import { Box, Grid } from '@availity/mui-layout'; import { Collapse } from '@availity/mui-transitions'; import { Link } from '@mui/material'; import { Snackbar, SnackbarProps, SnackbarCloseReason } from './Snackbar'; import { styled } from '@mui/material/styles'; /** * Snackbars (also known as toasts) are used for brief notifications of processes that have been or will be performed. * * Availity does not currently use the Material `SnackbarContent` component for basic messages, * instead every `Snackbar` should contain an `Alert` to convey messages. */ const meta: Meta = { title: 'Components/Snackbar/Snackbar', component: Snackbar, tags: ['autodocs'], }; export default meta; const SnackbarStoryPreview = styled('div', { name: 'MuiSnackbar', slot: 'Root', overridesResolver: (props, styles) => styles.root, })({}); export const _Snackbar: StoryObj = { render: (args: SnackbarProps) => { const [open, setOpen] = useState(false); const handleClick = () => { setOpen(true); }; const handleClose = (event: SyntheticEvent | Event, reason?: SnackbarCloseReason) => { if (reason === 'clickaway') { return; } setOpen(false); }; const action = ( Action ); return ( Alert Text ); }, args: { anchorOrigin: { vertical: 'bottom', horizontal: 'left' }, autoHideDuration: 6000, }, }; export const _Actions: StoryObj = { render: () => { const [visible, setVisible] = useState(true); const onClose = () => { setVisible(false); setTimeout(() => setVisible(true), 1000); }; const actionButton = ( Action Button ); const actionLink = Action Link; return ( No Action, No Close Close Action Button Action Link Action Button & Close ); }, }; export const _Alerts: StoryObj = { render: () => { const [visible, setVisible] = useState(true); const onClose = () => { setVisible(false); setTimeout(() => setVisible(true), 1000); }; return ( Info Alert Snackbar Additional text. Success Alert Snackbar Additional text. Warning Alert Snackbar Additional text. Error Alert Snackbar Additional text. Don't make a snackbar more than a couple sentences! ); }, }; interface SnackbarMessage { message: string; key: number; } /** This demo shows how to display multiple Snackbars without stacking them by using a consecutive animation. */ export const _ConsecutiveSnackbars: StoryObj = { render: () => { const [snackPack, setSnackPack] = useState([]); const [open, setOpen] = useState(false); const [messageInfo, setMessageInfo] = useState(undefined); useEffect(() => { if (snackPack.length && !messageInfo) { // Set a new snack when we don't have an active one setMessageInfo({ ...snackPack[0] }); setSnackPack((prev) => prev.slice(1)); setOpen(true); } else if (snackPack.length && messageInfo && open) { // Close an active snack when a new one is added setOpen(false); } }, [snackPack, messageInfo, open]); const handleClick = (message: string) => () => { setSnackPack((prev) => [...prev, { message, key: new Date().getTime() }]); }; const handleClose = (event: SyntheticEvent | Event, reason?: SnackbarCloseReason) => { if (reason === 'clickaway') { return; } setOpen(false); }; const handleExited = () => { setMessageInfo(undefined); }; return ( {messageInfo?.message} ); }, };