Components

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

Do
  • Keep the message short: what happened, in a few words, like “Changes saved”.
  • Add an Undo action for actions that can be reversed, and give it a longer duration so there’s time to use it.
  • Pass an id for toasts from repeated actions, so pressing Save twice shows one toast.
  • Use promise for work that takes a moment, so one toast goes from loading to done or failed.
Don’t
  • 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.

PropTypeDefault
position
The corner or edge the stack sits at.
'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right''bottom-right'
maxVisible
How many toasts show at once. Older ones wait until there is room.
number3
expanded
Keeps the stack fanned out.
booleanfalse
children
Renders one toast. Leave it out for the built-in layout.
(toast: ToastObject) => ReactNode—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ToastProvider

Gives the Toaster and useToast inside it a queue of their own. Renders no element.

PropTypeDefault
toaster
A 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.

PropTypeDefault
toast
Shows a toast and returns its id. An id that is still showing updates that toast instead.
(title | options, options?) => string—
success / error / warning / info / loading
The same, with the type set.
(title, options?) => string—
update
Changes a toast’s options while it shows.
(id, options) => boolean—
dismiss
Closes one toast, or all of them without an id.
(id?) => void—
promise
Shows 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.

PropTypeDefault
title
The headline.
ReactNode—
description
The message under the title, or on its own.
ReactNode—
type
Picks the icon. A loading toast stays until it is updated.
'default' | 'success' | 'error' | 'warning' | 'info' | 'loading''default'
action
The action button. Closes the toast unless dismiss is false.
{ label, onAction?, href?, dismiss? }—
duration
Milliseconds before it closes. Infinity keeps it open.
number5000
priority
How screen readers announce it. Errors are assertive.
'polite' | 'assertive'—
id
Names the toast; adding the same id again updates it in place.
string—
data
Your own values, read in a custom layout.
unknown—
onDismiss
Called 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.

PropTypeDefault
sx
StyleX 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.

PropTypeDefault
children
Your own glyph, such as an avatar.
ReactNode—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ToastTitle

Shows the toast’s title. Renders a <div>, or nothing without one.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ToastDescription

Shows the toast’s description. Renders a <div>, or nothing without one.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ToastAction

A small Button driven by the toast’s action. Renders nothing without one.

PropTypeDefault
variant
The button’s treatment.
'primary' | 'secondary' | 'ghost' | 'danger''secondary'
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ToastClose

Closes the toast. Renders a <button>.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—

ToastCloseButton

A ghost icon Button with an X, shown on hover or focus.

PropTypeDefault
aria-label
The accessible name of the button.
string'Dismiss'
sx
StyleX styles merged after the built-in ones.
StyleXStyles—