Toast
A brief notice that stacks in a corner of the screen, fans out on hover and swipes away. Built on ui-sdk’s Toast.
Usage
Put a Toaster near the root of the app once to draw the stack.
import { Toaster } from 'nyctoui/toast'
<>
<App />
<Toaster />
</>Then show a toast from any component with useToast, or from any module with toast.
import { useToast } from 'nyctoui/toast'
const toast = useToast()
toast.success('Issue created', {
description: 'ENG-214 Offline drafts was added to Backlog.',
})Up to three toasts show at once. The newest sits in front and the older ones tuck in behind it; hover or focus the stack to fan them out, which also pauses their timers. A toast closes after five seconds, when swiped toward its edge, or from the X that appears on hover. F6 moves keyboard focus into the stack.
Good for
- “Message sent” after sending an email.
- “Moved to trash” with an Undo action in a file manager.
- An export that runs in the background, from loading to done.
- A failed sync in a notes app, with a Retry action.
Anatomy
Toaster lays out each toast from these parts. Pass it a render function to compose your own.
<Toaster>
{(toast) => (
<Toast>
<ToastIcon />
<ToastTitle />
<ToastDescription />
<ToastAction />
<ToastCloseButton />
</Toast>
)}
</Toaster>Examples
Types
type picks the icon: success, error, warning, info or loading. Without one, the toast is text only. A description with no title reads as the whole message.
Action
action adds a button, such as Undo. toast returns the toast’s id, which update and dismiss take.
Promise
promise shows a spinning loading toast that turns into a success or an error when the promise settles. Each state takes a string, a set of options, or a function of the result.
Persistent
duration: Infinity keeps a toast up until it is closed. Errors are announced to screen readers at once rather than when they are idle. Adding a toast with an id that is already showing updates it in place, so pressing the button twice shows one toast.
Position
position puts the stack in a corner or at the middle of the top or bottom edge. Toasts swipe away toward that edge, and toward the side for a corner.
Custom layout
A render function passed to Toaster replaces the built-in layout. Pass your own values through data; here it carries the initials for ToastIcon.
Outside React
toast works from any module, such as a fetch wrapper, and lands in the app’s stack. For a second stack with toasts of its own, wrap it and its callers in a ToastProvider, as the position example does.
// api.ts
import { toast } from 'nyctoui/toast'
toast.error('Request failed')Best practices
- Keep the message short: what happened, in a few words, like “Changes saved”.
- Add an Undo
actionfor actions that can be reversed, and give it a longerdurationso there’s time to use it. - Pass an
idfor toasts from repeated actions, so pressing Save twice shows one toast. - Use
promisefor work that takes a moment, so one toast goes from loading to done or failed.
- Use a toast for an error that blocks people. Show it on the page, where it stays.
- Show form validation errors in a toast. Put them on the fields.
- Put long text in a toast. It closes on its own before people finish reading.
API reference
Toaster
The stack. Renders a <section> landmark in the top layer.
| Prop | Type | Default |
|---|---|---|
positionThe corner or edge the stack sits at. | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 'bottom-right' |
maxVisibleHow many toasts show at once. Older ones wait until there is room. | number | 3 |
expandedKeeps the stack fanned out. | boolean | false |
childrenRenders one toast. Leave it out for the built-in layout. | (toast: ToastObject) => ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ToastProvider
Gives the Toaster and useToast inside it a queue of their own. Renders no element.
| Prop | Type | Default |
|---|---|---|
toasterA queue from createToaster. Leave it out for a new one. | ToasterQueue | — |
useToast, toast
Show toasts: useToast returns the nearest provider’s queue, toast the app’s.
| Prop | Type | Default |
|---|---|---|
toastShows a toast and returns its id. An id that is still showing updates that toast instead. | (title | options, options?) => string | — |
success / error / warning / info / loadingThe same, with the type set. | (title, options?) => string | — |
updateChanges a toast’s options while it shows. | (id, options) => boolean | — |
dismissCloses one toast, or all of them without an id. | (id?) => void | — |
promiseShows a loading toast that follows the promise to success or error. | (promise, { loading, success, error }) => string | — |
Toast options
What a toast shows and how long it stays.
| Prop | Type | Default |
|---|---|---|
titleThe headline. | ReactNode | — |
descriptionThe message under the title, or on its own. | ReactNode | — |
typePicks the icon. A loading toast stays until it is updated. | 'default' | 'success' | 'error' | 'warning' | 'info' | 'loading' | 'default' |
actionThe action button. Closes the toast unless dismiss is false. | { label, onAction?, href?, dismiss? } | — |
durationMilliseconds before it closes. Infinity keeps it open. | number | 5000 |
priorityHow screen readers announce it. Errors are assertive. | 'polite' | 'assertive' | — |
idNames the toast; adding the same id again updates it in place. | string | — |
dataYour own values, read in a custom layout. | unknown | — |
onDismissCalled when the toast closes, with why. | (details) => void | — |
Toast
One toast in the stack. Renders an <li> that holds the parts in a row. Swipes away toward the stack’s edge.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ToastIcon
The type’s glyph, tinted to match. Renders a <span>, or nothing without a known type or children.
| Prop | Type | Default |
|---|---|---|
childrenYour own glyph, such as an avatar. | ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ToastTitle
Shows the toast’s title. Renders a <div>, or nothing without one.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ToastDescription
Shows the toast’s description. Renders a <div>, or nothing without one.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ToastAction
A small Button driven by the toast’s action. Renders nothing without one.
| Prop | Type | Default |
|---|---|---|
variantThe button’s treatment. | 'primary' | 'secondary' | 'ghost' | 'danger' | 'secondary' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ToastClose
Closes the toast. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
ToastCloseButton
A ghost icon Button with an X, shown on hover or focus.
| Prop | Type | Default |
|---|---|---|
aria-labelThe accessible name of the button. | string | 'Dismiss' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |