ComponentsToast

Toast

Display a brief notification.

Documentation

Usage

Use toaster.create() along with the Toast component to add toasts.

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

Appearance

Use the colorScheme prop to select between the different toast types.

import type { ComponentPropsWithRef } from "react";
 
import { Button, Toast, ToastTitle, toaster } from "@optiaxiom/react";
 
export function App({
  colorScheme,
}: Pick<ComponentPropsWithRef<typeof Toast>, "colorScheme">) {
  return (
    <Button
      onClick={() =>
        toaster.create(
          <Toast colorScheme={colorScheme}>
            <ToastTitle>This is an example toast message.</ToastTitle>
          </Toast>,
        )
      }
    >
      Create Toast
    </Button>
  );
}

Action

Use the ToastAction component to include a short action button associated with the toast.

Task is in-progress

import {
  Button,
  Flex,
  Text,
  Toast,
  ToastAction,
  ToastTitle,
  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 w="144">Task is {status}</Text>
 
      <Button
        appearance="danger-outline"
        disabled={status === "deleted"}
        icon={<IconTrash />}
        onClick={() => {
          setStatus("deleted");
          toaster.create(
            <Toast>
              <ToastTitle>Task Deleted</ToastTitle>
              <ToastAction
                altText="Undo"
                onClick={() => setStatus("in-progress")}
              >
                Undo
              </ToastAction>
            </Toast>,
          );
        }}
      >
        Delete
      </Button>
 
      <Button
        disabled={status !== "deleted"}
        onClick={() => setStatus("in-progress")}
      >
        Restore
      </Button>
    </Flex>
  );
}

Props

Toast

Supports all Box props in addition to its own.

Name

Type

asChild

false | true

colorScheme

"danger" | "information" | "neutral" | "success" | "warning" = "neutral"

defaultOpen

false | true

duration

number

Time in milliseconds that toast should remain visible for. Overrides value given to ToastProvider.

forceMount

true

Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.

onEscapeKeyDown

(event: KeyboardEvent) => void

onOpenChange

(open: boolean) => void

onPause

() => void

onResume

() => void

onSwipeCancel

(event: SwipeEvent) => void

onSwipeEnd

(event: SwipeEvent) => void

onSwipeMove

(event: SwipeEvent) => void

onSwipeStart

(event: SwipeEvent) => void

open

false | true

type

"background" | "foreground"

ToastTitle

Supports all Box props in addition to its own.

Name

Type

asChild

false | true

ToastAction

Supports all Box props in addition to its own.

Name

Type

altText*

string

A short description for an alternate way to carry out the action. For screen reader users who will not be able to navigate to the button easily/quickly.

asChild

false | true

ToastProvider

Name

Type

asChild

false | true

container

Element | DocumentFragment

duration

number

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

hotkey

string[]

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

label

string

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

swipeDirection

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

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

swipeThreshold

number

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

toaster

Toaster

An instance of toaster returned from the createToaster method.

Changelog

0.1.0

  • Added component

Copyright 2024 © Optimizely.