Skip to Content
ComponentsToast

Toast

Display a brief notification.

Documentation

Usage

Use toaster.create() to add toasts. The method takes two arguments message and options.

"use client"; import { Button, toaster } from "@optiaxiom/react"; export function App() { return ( <Button onClick={() => toaster.create("This is an example toast message.")}> Create Toast </Button> ); }

Appearance

Use the intent option in the second argument to select between the different toast types.

"use client"; import { Button, Flex, toaster } from "@optiaxiom/react"; export function App() { return ( <Flex flexDirection="row" flexWrap="wrap"> {( ["danger", "information", "neutral", "success", "warning"] as const ).map((intent) => ( <Button key={intent} onClick={() => toaster.create("This is an example toast message.", { intent }) } > {intent} toast </Button> ))} </Flex> ); }

Action

Use the action and onAction options to include a short action button associated with the toast.

Task in-progress

"use client"; import { Button, Flex, Text, toaster } from "@optiaxiom/react"; import { IconTrash } from "@tabler/icons-react"; import { useState } from "react"; export function App() { const [status, setStatus] = useState("in-progress"); return ( <Flex flexDirection="row"> <Text>Task {status}</Text> <Button appearance="danger-outline" disabled={status === "deleted"} icon={<IconTrash />} onClick={() => { setStatus("deleted"); toaster.create("Task Deleted", { action: "Undo", onAction: () => setStatus("in-progress"), }); }} > Delete </Button> <Button disabled={status !== "deleted"} onClick={() => setStatus("in-progress")} > Restore </Button> </Flex> ); }

Dismiss

Use toaster.remove() to remove toasts. The create() method returns a unique ID for the toast which can then be passed to the remove() method.

"use client"; import { Button, toaster } from "@optiaxiom/react"; export function App() { return ( <Button onClick={async () => { const id = toaster.create("Saving task"); // simulate network request await new Promise((resolve) => setTimeout(resolve, 3_000)); toaster.remove(id); toaster.create("Could not save task", { intent: "danger" }); }} > Create Toast </Button> ); }

Related

Alert

Show inline messages about important or time-sensitive changes.

Banner

Display a prominent message at the top of the screen.

Props

ToastProvider

Supports all Box props in addition to its own. Renders a <div> element.

Prop

asChild

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read the Composition guide for more details.

false | true

className

string

container

An optional container where the portaled content should be appended.

Element | DocumentFragment

duration

Time in milliseconds that each toast should remain visible for. @defaultValue 5000

number

hotkey

The keys to use as the keyboard shortcut that will move focus to the toast viewport. @defaultValue [‘F8’]

string[]

label

An author-localized label for each toast. Used to help screen reader users associate the interruption with a toast. @defaultValue ‘Notification’

string

position

Control where in the screen toasts will be placed.

"bottom" | "top" | "bottom-left" | "bottom-right" | "top-left" | "top-right"

Default: top-right

swipeDirection

Direction of pointer swipe that should close the toast. @defaultValue ‘right’

"left" | "right" | "up" | "down"

swipeThreshold

Distance in pixels that the swipe must pass before a close is triggered. @defaultValue 50

number

toaster

An instance of toaster returned from the createToaster method.

Toaster

Changelog

0.1.0

  • Added component
Last updated on