-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add transition durations for tailwind (#2036)
- add in durations via (transitionDuration) - add in new toast notification implementation example - use new medium durations in example - demo autodismiss behavior with stack of notifications - use state to track notifications
- Loading branch information
1 parent
db6203e
commit aed0f09
Showing
3 changed files
with
165 additions
and
63 deletions.
There are no files selected for viewing
63 changes: 0 additions & 63 deletions
63
src/components/ToastNotification/ToastNotification.stories.ts
This file was deleted.
Oops, something went wrong.
149 changes: 149 additions & 0 deletions
149
src/components/ToastNotification/ToastNotification.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { Transition } from '@headlessui/react'; | ||
import type { StoryObj, Meta } from '@storybook/react'; | ||
import React from 'react'; | ||
|
||
import type { ComponentProps } from 'react'; | ||
|
||
import { ToastNotification } from './ToastNotification'; | ||
|
||
import Button from '../Button'; | ||
|
||
export default { | ||
title: 'Components/ToastNotification', | ||
component: ToastNotification, | ||
parameters: { | ||
layout: 'centered', | ||
badges: ['intro-1.0', 'current-2.0'], | ||
}, | ||
argTypes: { | ||
onDismiss: { action: 'trigger dismiss' }, | ||
timeout: { table: { disable: true } }, | ||
}, | ||
args: { | ||
title: "You've got a temporary notification!", | ||
className: 'w-96', | ||
}, | ||
} as Meta<Args>; | ||
|
||
type Args = ComponentProps<typeof ToastNotification>; | ||
type Story = StoryObj<Args>; | ||
|
||
export const Default: Story = {}; | ||
|
||
export const Favorable: Story = { | ||
args: { | ||
status: 'favorable', | ||
}, | ||
}; | ||
|
||
/** | ||
* Notifications can have different status, to indicate errors or destructive actions have completed. | ||
*/ | ||
export const Critical: Story = { | ||
args: { | ||
status: 'critical', | ||
}, | ||
}; | ||
|
||
/** | ||
* We can restrict the ability to dismiss the notification by not specifying the `onDismiss` method. | ||
*/ | ||
export const NotDismissable: Story = { | ||
args: { | ||
...Default.args, | ||
onDismiss: undefined, | ||
}, | ||
}; | ||
|
||
/** | ||
* Tooltips can be instructed to auto-close after a certain period. After the timeout, the component will call the defined | ||
* `onDismiss` method. The behavior of the dissmisal is left up to the user, which allows for complete control. | ||
*/ | ||
export const AutoDismiss: Story = { | ||
args: { | ||
...Default.args, | ||
dissmissType: 'auto', | ||
timeout: 500, | ||
onDismiss: () => console.log('trigger onDismiss'), | ||
}, | ||
}; | ||
|
||
let toastId = 0; | ||
const ToastNotificationManager = (args: Args) => { | ||
const [toasts, setToasts] = React.useState< | ||
{ id: number | string; text: string; show?: boolean }[] | ||
>([]); | ||
|
||
// TODO: clean up `toasts` after .show is set to false (using useEffect? and .debounce) | ||
// - In a production implementation, you can filter out any toasts where show=false | ||
|
||
return ( | ||
<div className="flex h-[90vh] w-[90vw] items-center justify-center"> | ||
<Button | ||
onClick={() => { | ||
setToasts([ | ||
...toasts, | ||
{ | ||
id: toastId++, | ||
text: 'New Toast', | ||
show: true, | ||
}, | ||
]); | ||
}} | ||
> | ||
Trigger A Toast Notification | ||
</Button> | ||
<div | ||
className="dur absolute bottom-0 left-0 flex flex-col gap-size-2" | ||
id="toast-container" | ||
> | ||
{toasts.map((toast) => ( | ||
<Transition | ||
appear | ||
enter="transition-all duration-medium" | ||
enterFrom="opacity-0 transform-gpu scale-0" | ||
enterTo="opacity-100 transform-gpu scale-100" | ||
key={toast.id} | ||
leave="ease-in-out transition-all duration-medium" | ||
leaveFrom="opacity-100 transform-gpu translate-x-[0px]" | ||
leaveTo="opacity-0 transform-gpu translate-x-[-100%]" | ||
show={toast.show} | ||
> | ||
<ToastNotification | ||
{...args} | ||
dissmissType="auto" | ||
onDismiss={() => { | ||
setToasts( | ||
toasts.map((thisToast) => { | ||
return thisToast.id === toast.id | ||
? { ...thisToast, show: false } | ||
: thisToast; | ||
}), | ||
); | ||
}} | ||
title={'You got a new toast: ' + toast.text + toast.id} | ||
/> | ||
</Transition> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
/** | ||
* This implementation example shows how you can use toasts with state to handle multiple, stacking notifications. | ||
* | ||
* For a full, production-ready implementation, clean up any toasts with show=false after the animation has completed. | ||
* - Consider using lodash.debounce to time the re-render, and useEffect that watches the list of toasts | ||
* - Any debouncing should map to whatever duration is used in `Transition` | ||
* | ||
* Here, we use `<Transition>` provided by [HeadlessUI](https://github.com/chanzuckerberg/edu-design-system/blob/main/package.json#L91-L93). | ||
*/ | ||
export const ExampleDismissingToasts: Story = { | ||
render: (args) => <ToastNotificationManager {...args} />, | ||
parameters: { | ||
// For interactive use, low value in snap testing again since already covered in other stories. | ||
chromatic: { disableSnapshot: true }, | ||
snapshot: { skip: true }, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters