leafyGreen UI Kit Toast
npm install @leafygreen-ui/toast``shell`
pnpm add @leafygreen-ui/toast
`shell`
yarn add @leafygreen-ui/toast
`shell`
npm install @leafygreen-ui/toast
`js
import Button from '@leafygreen-ui/button';
import { ToastProvider, useToast, Variant } from '@leafygreen-ui/toast';
const { pushToast, clearStack, getStack, updateToast } = useToast();
const stack = getStack();
const createToast = () => {
pushToast({
title: 'Toast Title',
description: 'Toast Description',
variant: Variant.Success,
timeout: null,
});
};
return (
);
`
useToast is a React hook that enables a consumer to interact with a Toast stack. It may only be used inside of a . The hook takes no arguments and returns the following functions:
| Name | Signature |
| ----------- | ------------------------------------------------------------------------ |
| pushToast | (payload: ToastProps) => ToastId |(payload: ToastId) => ToastProps \| undefined
| popToast | |(id: ToastId, props: Partial
| updateToast | |(id: ToastId) => ToastProps \| undefined
| getToast | |() => ToastStack \|undefined
| getStack | |() => void
| clearStack | |
| Prop | Type | Default | Description |
| --------------- | ------------------------- | ------- | ----------------------------------------------------- |
| initialValue | Map
| portalClassName | string | | Class name applied to the containing Portal component |
| Prop | Type | Default | Description |
| ---------------- | --------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| actionElement | React.ReactNode | | Optional action button, or other element. Only rendered if variant === 'progress'. This should be a shortcut only—and not the _only_ way to perform the action. The provided action element should have an aria-description attribute that describes how to alternatively perform the action. |boolean
| darkMode | | false | Renders the component with dark mode styles. |React.ReactNode
| description | | | Additional body text |boolean
| dismissible | | true | Determines whether to render the close button. If timeout === null, then dismissible is forced to true. |(event: ToastCloseEvent) => void
| onClose | | | Fired either when the close button is clicked, or when timeout has elapsed. When triggered by button click, event.type === "click". When triggered by timeout, event.type === "timeout". |boolean
| open _required_ | | | Required boolean that renders the Toast and makes it visible when true. Note: open is not a valid prop when rendering toasts programmatically |number
| progress | | | Optional number between 0 and 1 that sets the progress bar's progress. Note: the progress bar is only rendered when the Toast variant is set to 'progress'. |number \| null
| timeout | | 6000 | Dismiss the Toast after timeout milliseconds. If timeout is null or 0, the Toast will never dismiss automatically. Notes: variant="progress", timeout timer will not start until progress is complete.timeout timer will be paused when a user is interacting with a Toast. |React.ReactNode
| title _required_ | | | Primary text for the toast |'success' 'note' 'warning' 'progress' 'important'
| variant | | note` | Required style variant to render the Toast as. |