Documentation
useToasts

useToasts

The useToasts hook provides an easy and efficient way to manage toast notifications in your React application. This hook offers functionality to create, display, and remove toasts, with customizable layouts and behaviors.

Usage

First, you need to import the useToasts hook from the kitchn package.

import { useToasts } from "kitchn";

Example

Here is an example of how to use the useToasts hook in a component:

Parameters

The useToasts hook accepts an optional layout parameter:

NameTypeDescription
layoutToastLayout (optional)An object that defines the layout for the toasts, such as padding, margin, width, and placement.

ToastLayout

NameTypeDefaultDescription
paddingReact.CSSProperties["padding"]"12px 16px"Defines the padding inside the toast.
marginReact.CSSProperties["margin"]"8px 0"Defines the margin outside the toast.
widthReact.CSSProperties["width"]"420px"Sets the width of the toast.
maxWidthReact.CSSProperties["maxWidth"]"90vw"Sets the maximum width of the toast.
maxHeightReact.CSSProperties["maxHeight"]"75px"Sets the maximum height of the toast.
placement"topLeft" | "topRight" | "bottomLeft" | "bottomRight" | "bottomRight"Determines where the toast will appear on the screen.

Return Value

The useToasts hook returns an object with the following properties:

PropertyTypeDescription
toastsToast[]An array of current toast notifications.
setToast(toast: ToastInput) => voidAdds a new toast to the list.
removeAll() => voidRemoves all active toasts.
findOneToastByID(id: string) => Toast | undefinedFinds a toast by its ID, returning the toast if found, or undefined if not.
removeToastByID(id: string) => voidRemoves a specific toast by its ID, setting its visibility to false.

ToastInput

NameTypeDefaultDescription
textstring | React.ReactNode(required)The content of the toast, which can be a string or a React node.
typeToastTypes"primary"The type of the toast, determining its color based on AccentColors.
idstringGenerated IDAn optional unique ID for the toast. If not provided, an ID will be generated automatically.
delaynumber2000How long the toast should be visible (in milliseconds) before it is automatically removed.
actionsToastAction[][]An array of actions (buttons) that can be included in the toast.
preservebooleanfalseIf true, the toast will remain visible until manually closed. Automatically set to true if any non-passive action is present.

ToastAction

NameTypeRequiredDescription
namestringYesThe text displayed on the action button.
handler(event: React.MouseEvent<HTMLButtonElement>, cancel: () => void) => voidYesFunction called when the action is triggered. Receives the click event and a cancel function to close the toast.
passivebooleanNoIf false, the toast will be preserved until an action is taken. If true, the toast can auto-dismiss.