Toast
Generates toast notifications.
View as MarkdownAnatomy
Import the component and assemble its parts:
import { Toast } from '@base-ui/react/toast';
<Toast.Provider>
<Toast.Portal>
<Toast.Viewport>
{/* Stacked toasts */}
<Toast.Root>
<Toast.Content>
<Toast.Title />
<Toast.Description />
<Toast.Action />
<Toast.Close />
</Toast.Content>
</Toast.Root>
{/* Anchored toasts */}
<Toast.Positioner>
<Toast.Root>
<Toast.Arrow />
<Toast.Content>
<Toast.Title />
<Toast.Description />
<Toast.Action />
<Toast.Close />
</Toast.Content>
</Toast.Root>
</Toast.Positioner>
</Toast.Viewport>
</Toast.Portal>
</Toast.Provider>General usage
<Toast.Provider>can be wrapped around your entire app, ensuring all toasts are rendered in the same viewport.- F6 lets users jump into the toast viewport landmark region to navigate toasts with keyboard focus.
- The
data-swipe-ignoreattribute can be manually added to elements inside of a toast to prevent swipe-to-dismiss gestures on them. Interactive elements are automatically prevented.
Global manager
A global toast manager can be created by passing the toastManager prop to the <Toast.Provider>.
This enables you to queue a toast from anywhere in the app (such as in functions outside the React tree) while still using the same toast renderer.
The created toastManager object has the same properties and methods as the Toast.useToastManager() hook.
const toastManager = Toast.createToastManager();<Toast.Provider toastManager={toastManager}>Stacking and animations
The --toast-index CSS variable can be used to determine the stacking order of the toasts.
The 0th index toast appears at the front.
.Toast {
z-index: calc(1000 - var(--toast-index));
transform: scale(1 - calc(0.1 * var(--toast-index)));
}The --toast-offset-y CSS variable can be used to determine the vertical offset of the toasts when positioned absolutely with a translation offset — this is usually used with the data-expanded attribute, present when the toast viewport is being hovered or has focus.
.Toast[data-expanded] {
transform: translateY(var(--toast-offset-y));
}<Toast.Content> is used to hide overflow from taller toasts while the stack is collapsed.
The data-behind attribute marks content that sits behind the frontmost toast and pairs with the data-expanded attribute so the content fades back in when the viewport expands:
.ToastContent {
overflow: hidden;
transition: opacity 0.25s;
}
.ToastContent[data-behind] {
opacity: 0;
}
.ToastContent[data-expanded] {
opacity: 1;
}The --toast-swipe-movement-x and --toast-swipe-movement-y CSS variables are used to determine the swipe movement of the toasts in order to add a translation offset.
.Toast {
transform: scale(1 - calc(0.1 * var(--toast-index))) translateX(var(--toast-swipe-movement-x))
translateY(calc(var(--toast-swipe-movement-y) + (var(--toast-index) * -20%)));
}The data-swipe-direction attribute can be used to determine the swipe direction of the toasts to add a translation offset upon dismissal.
&[data-ending-style] {
opacity: 0;
&[data-swipe-direction='up'] {
transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
}
&[data-swipe-direction='down'] {
transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
}
/* Note: --offset-y is defined locally in these examples and derives from
--toast-offset-y, --toast-index, and swipe movement values */
&[data-swipe-direction='left'] {
transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
}
&[data-swipe-direction='right'] {
transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
}
}The data-limited attribute indicates that the toast was removed from the list due to exceeding the limit option.
This is useful for animating the toast differently when it is removed from the list.
Examples
Anchored toasts
Toasts can be anchored to a specific element using <Toast.Positioner> and the positionerProps option when adding a toast. This is useful for showing contextual feedback like transient “Copied” toasts that appear near the button that triggered the action.
Anchored toasts should be rendered in a separate <Toast.Provider> from stacked toasts. A global toast manager can be created for each to manage them separately throughout your app:
const anchoredToastManager = Toast.createToastManager();
const stackedToastManager = Toast.createToastManager();
function App() {
return (
<React.Fragment>
<Toast.Provider toastManager={anchoredToastManager}>
<AnchoredToasts />
</Toast.Provider>
<Toast.Provider toastManager={stackedToastManager}>
<StackedToasts />
</Toast.Provider>
{/* App content */}
</React.Fragment>
);
}
function AnchoredToasts() {
const { toasts } = Toast.useToastManager();
return (
<Toast.Portal>
<Toast.Viewport>
{toasts.map((toast) => (
<Toast.Positioner key={toast.id} toast={toast}>
<Toast.Root toast={toast}>{/* ... */}</Toast.Root>
</Toast.Positioner>
))}
</Toast.Viewport>
</Toast.Portal>
);
}
function StackedToasts() {
const { toasts } = Toast.useToastManager();
return (
<Toast.Portal>
<Toast.Viewport>
{toasts.map((toast) => (
<Toast.Root key={toast.id} toast={toast}>
{/* ... */}
</Toast.Root>
))}
</Toast.Viewport>
</Toast.Portal>
);
}Custom position
The position of the toasts is controlled by your own CSS.
To change the toasts’ position, you can modify the .Viewport and .Root styles.
A more general component could accept a data-position attribute, which the CSS handles for each variation.
The following shows a top-center position:
Undo action
When adding a toast, the actionProps option can be used to define props for an action button inside of it—this enables the ability to undo an action associated with the toast.
Promise
An asynchronous toast can be created with three possible states: loading, success, and error.
The type string matches these states to change the styling.
Each of the states also accepts the method options object for more granular control.
Custom
A toast with custom data can be created by passing any typed object interface to the data option.
This enables you to pass any data (including functions) you need to the toast and access it in the toast’s rendering logic.
Varying heights
Toasts with varying heights are stacked by ensuring that the <Toast.Content> element has overflow: hidden set, along with all toasts’ heights matching the frontmost toast at index 0.
This prevents taller toasts from overflowing the stack when collapsed.
API reference
Provider
Provides a context for creating and managing toasts.
limitnumber3
number- Name
- Description
The maximum number of toasts that can be displayed at once. When the limit is reached, the oldest toast will be removed to make room for the new one.
- Type
number | undefined- Default
3
toastManagerToastManager—
ToastManager- Name
- Description
A global manager for toasts to use outside of a React component.
- Type
ToastManager | undefined
timeoutnumber5000
number- Name
- Description
The default amount of time (in ms) before a toast is auto dismissed. A value of
0will prevent the toast from being dismissed automatically.- Type
number | undefined- Default
5000
childrenReact.ReactNode—
React.ReactNode- Name
- Type
React.ReactNode | undefined
Additional Types
Toast.Provider.Props
Re-Export of Provider props as ToastProviderProps
Viewport
A container viewport for toasts.
Renders a <div> element.
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Viewport.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Viewport.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Viewport.State, ) => ReactElement) | undefined
data-expanded
--toast-frontmost-height
Indicates the height of the frontmost toast.
Additional Types
Toast.Viewport.Props
Re-Export of Viewport props as ToastViewportProps
Toast.Viewport.State
type ToastViewportState = { expanded: boolean };Portal
A portal element that moves the viewport to a different part of the DOM.
By default, the portal element is appended to <body>.
Renders a <div> element.
containerUnion—
Union- Name
- Description
A parent element to render the portal element into.
- Type
| HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null | undefined
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: any) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | ((state: any) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | ((props: HTMLProps, state: any) => ReactElement) | undefined
Additional Types
Toast.Portal.Props
Re-Export of Portal props as ToastPortalProps
Toast.Portal.State
type ToastPortalState = {};Root
Groups all parts of an individual toast.
Renders a <div> element.
swipeDirectionUnion['down', 'right']
Union- Name
- Description
Direction(s) in which the toast can be swiped to dismiss.
- Type
| 'up' | 'down' | 'left' | 'right' | ('left' | 'right' | 'up' | 'down')[] | undefined- Default
['down', 'right']
toast*Toast.Root.ToastManagerAddOptions<any>—
Toast.Root.ToastManagerAddOptions<any>- Name
- Description
The toast to render.
- Type
Toast.Root.ToastManagerAddOptions<any>
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Root.State, ) => ReactElement) | undefined
data-expanded
data-limited
data-swipe-direction
data-swiping
data-type
data-starting-style
data-ending-style
--toast-height
Indicates the measured natural height of the toast in pixels.
--toast-index
Indicates the index of the toast in the list.
--toast-offset-y
Indicates the vertical pixels offset of the toast in the list when expanded.
--toast-swipe-movement-x
Indicates the horizontal swipe movement of the toast.
--toast-swipe-movement-y
Indicates the vertical swipe movement of the toast.
Additional Types
Toast.Root.Props
Re-Export of Root props as ToastRootProps
Toast.Root.State
type ToastRootState = {
transitionStatus: TransitionStatus;
expanded: boolean;
limited: boolean;
type: string | undefined;
swiping: boolean;
swipeDirection: 'up' | 'down' | 'left' | 'right' | undefined;
};Toast.Root.ToastObject
type ToastRootToastObject = {
id: string;
ref?: React.RefObject<HTMLElement | null>;
title?: React.ReactNode;
type?: string;
description?: React.ReactNode;
timeout?: number;
priority?: 'low' | 'high';
transitionStatus?: 'starting' | 'ending';
limited?: boolean;
height?: number;
onClose?: () => void;
onRemove?: () => void;
actionProps?: Omit<
React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>,
'ref'
>;
positionerProps?: ToastManagerPositionerProps;
data?: {};
};Content
A container for the contents of a toast.
Renders a <div> element.
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Content.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Content.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Content.State, ) => ReactElement) | undefined
data-behind
data-expanded
Additional Types
Toast.Content.Props
Re-Export of Content props as ToastContentProps
Toast.Content.State
type ToastContentState = { expanded: boolean; behind: boolean };Title
A title that labels the toast.
Renders an <h2> element.
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Title.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Title.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Title.State, ) => ReactElement) | undefined
data-type
Additional Types
Toast.Title.Props
Re-Export of Title props as ToastTitleProps
Toast.Title.State
type ToastTitleState = { type: string | undefined };Description
A description that describes the toast.
Can be used as the default message for the toast when no title is provided.
Renders a <p> element.
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Description.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Description.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Description.State, ) => ReactElement) | undefined
data-type
Additional Types
Toast.Description.Props
Re-Export of Description props as ToastDescriptionProps
Toast.Description.State
type ToastDescriptionState = { type: string | undefined };Action
Performs an action when clicked.
Renders a <button> element.
nativeButtonbooleantrue
boolean- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (for example,<div>).- Type
boolean | undefined- Default
true
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Action.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Action.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Action.State, ) => ReactElement) | undefined
data-type
Additional Types
Toast.Action.Props
Re-Export of Action props as ToastActionProps
Toast.Action.State
type ToastActionState = { type: string | undefined };Close
Closes the toast when clicked.
Renders a <button> element.
nativeButtonbooleantrue
boolean- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (for example,<div>).- Type
boolean | undefined- Default
true
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Close.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Close.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Close.State, ) => ReactElement) | undefined
data-type
Additional Types
Toast.Close.Props
Re-Export of Close props as ToastCloseProps
Toast.Close.State
type ToastCloseState = { type: string | undefined };Positioner
Positions the toast against the anchor.
Renders a <div> element.
disableAnchorTrackingbooleanfalse
boolean- Description
Whether to disable the popup from tracking any layout shift of its positioning anchor.
- Type
boolean | undefined- Default
false
toast*ToastManagerAddOptions<any>—
ToastManagerAddOptions<any>- Name
- Description
The toast object associated with the positioner.
- Type
alignAlign'center'
Align- Name
- Description
How to align the popup relative to the specified side.
- Type
'start' | 'center' | 'end' | undefined- Default
'center'
alignOffsetnumber | OffsetFunction0
number | OffsetFunction- Name
- Description
Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a
dataobject parameter with the following properties:data.anchor: the dimensions of the anchor element with propertieswidthandheight.data.positioner: the dimensions of the positioner element with propertieswidthandheight.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side.
- Type
| number | ((data: { side: | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'; align: 'start' | 'center' | 'end'; anchor: { width: number; height: number }; positioner: { width: number; height: number }; }) => number) | undefined- Default
0- Example
<Positioner alignOffset={({ side, align, anchor, positioner }) => { return side === 'top' || side === 'bottom' ? anchor.width : anchor.height; }} />
sideSide'top'
Side- Name
- Description
Which side of the anchor element to align the toast against. May automatically change to avoid collisions.
- Type
| 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | undefined- Default
'top'
sideOffsetnumber | OffsetFunction0
number | OffsetFunction- Name
- Description
Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a
dataobject parameter with the following properties:data.anchor: the dimensions of the anchor element with propertieswidthandheight.data.positioner: the dimensions of the positioner element with propertieswidthandheight.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side.
- Type
| number | ((data: { side: | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'; align: 'start' | 'center' | 'end'; anchor: { width: number; height: number }; positioner: { width: number; height: number }; }) => number) | undefined- Default
0- Example
<Positioner sideOffset={({ side, align, anchor, positioner }) => { return side === 'top' || side === 'bottom' ? anchor.height : anchor.width; }} />
arrowPaddingnumber5
number- Name
- Description
Minimum distance to maintain between the arrow and the edges of the popup.
Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
- Type
number | undefined- Default
5
anchorElement | null—
Element | null- Name
- Description
An element to position the toast against.
- Type
Element | null | undefined
collisionAvoidanceCollisionAvoidance—
CollisionAvoidance- Description
Determines how to handle collisions when positioning the popup.
- Type
CollisionAvoidance | undefined- Example
<Positioner collisionAvoidance={{ side: 'shift', align: 'shift', fallbackAxisSide: 'none', }} />
collisionBoundaryBoundary'clipping-ancestors'
Boundary- Description
An element or a rectangle that delimits the area that the popup is confined to.
- Type
Boundary | undefined- Default
'clipping-ancestors'
collisionPaddingPadding5
Padding- Name
- Description
Additional space to maintain from the edge of the collision boundary.
- Type
Padding | undefined- Default
5
stickybooleanfalse
boolean- Name
- Description
Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.
- Type
boolean | undefined- Default
false
positionMethod'absolute' | 'fixed''absolute'
'absolute' | 'fixed'- Name
- Description
Determines which CSS
positionproperty to use.- Type
'absolute' | 'fixed' | undefined- Default
'absolute'
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Positioner.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Positioner.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Positioner.State, ) => ReactElement) | undefined
data-anchor-hidden
data-align
data-side
--anchor-height
The anchor’s height.
--anchor-width
The anchor’s width.
--available-height
The available height between the anchor and the edge of the viewport.
--available-width
The available width between the anchor and the edge of the viewport.
--transform-origin
The coordinates that this element is anchored to. Used for animations and transitions.
Additional Types
Toast.Positioner.Props
Re-Export of Positioner props as ToastPositionerProps
Toast.Positioner.State
type ToastPositionerState = {
side:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'inline-end'
| 'inline-start';
align: 'start' | 'center' | 'end';
anchorHidden: boolean;
}Arrow
Displays an element positioned against the toast anchor.
Renders a <div> element.
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toast.Arrow.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toast.Arrow.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toast.Arrow.State, ) => ReactElement) | undefined
data-uncentered
data-align
data-side
Additional Types
Toast.Arrow.Props
Re-Export of Arrow props as ToastArrowProps
Toast.Arrow.State
type ToastArrowState = {
side:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'inline-end'
| 'inline-start';
align: 'start' | 'center' | 'end';
uncentered: boolean;
}useToastManager
Manages toasts, called inside of a <Toast.Provider>.
const toastManager = Toast.useToastManager();Return value
toastsToastManagerAddOptions<{}>[]—
ToastManagerAddOptions<{}>[]- Name
- Type
ToastManagerAddOptions<{}>[]
addfunction—
function- Name
- Type
((options: ToastManagerAddOptions<{}>) => string)
closefunction—
function- Name
- Type
((toastId: string) => void)
updatefunction—
function- Name
- Type
((toastId: string, options: ToastManagerUpdateOptions<{}>) => void)
promisefunction—
function- Name
- Type
((promise: Promise<Value>, options: ToastManagerPromiseOptions<Value, {}>) => Promise<Value>)
add method
Creates a toast by adding it to the toast list.
Returns a toastId that can be used to update or close the toast later.
const toastId = toastManager.add({
description: 'Hello, world!',
});function App() {
const toastManager = Toast.useToastManager();
return (
<button
type="button"
onClick={() => {
toastManager.add({
description: 'Hello, world!',
});
}}
>
Add toast
</button>
);
}For high priority toasts, the title and description strings are what are used to announce the toast to screen readers.
Screen readers do not announce any extra content rendered inside <Toast.Root>, including the <Toast.Title> or <Toast.Description> components, unless they intentionally navigate to the toast viewport.
update method
Updates the toast with new options.
toastManager.update(toastId, {
description: 'New description',
});close method
Closes the toast, removing it from the toast list after any animations complete.
toastManager.close(toastId);promise method
Creates an asynchronous toast with three possible states: loading, success, and error.
const promise = toastManager.promise(
new Promise((resolve) => {
setTimeout(() => resolve('world!'), 1000);
}),
{
// Each are a shortcut for the `description` option
loading: 'Loading…',
success: (data) => `Hello ${data}`,
error: (err) => `Error: ${err}`,
},
);Each state also accepts the method options object to granularly control the toast for each state:
const promise = toastManager.promise(
new Promise((resolve) => {
setTimeout(() => resolve('world!'), 1000);
}),
{
loading: {
title: 'Loading…',
description: 'The promise is loading.',
},
success: {
title: 'Success',
description: 'The promise resolved successfully.',
},
error: {
title: 'Error',
description: 'The promise rejected.',
actionProps: {
children: 'Contact support',
onClick() {
// Redirect to support page
},
},
},
},
);See full promise method options
Additional Types
Additional Types
ToastObject
type ToastObject = {
id: string;
ref?: React.RefObject<HTMLElement | null>;
title?: React.ReactNode;
type?: string;
description?: React.ReactNode;
timeout?: number;
priority?: 'low' | 'high';
transitionStatus?: 'starting' | 'ending';
limited?: boolean;
height?: number;
onClose?: () => void;
onRemove?: () => void;
actionProps?: Omit<
React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>,
'ref'
>;
positionerProps?: ToastManagerPositionerProps;
data?: {};
};ToastManager
type ToastManager = {
' subscribe': (listener: (data: ToastManagerEvent) => void) => () => void;
add: (options: ToastManagerAddOptions<{}>) => string;
close: (id: string) => void;
update: (id: string, updates: ToastManagerUpdateOptions<{}>) => void;
promise: (
promiseValue: Promise<Value>,
options: ToastManagerPromiseOptions<Value, {}>,
) => Promise<Value>;
};ToastManagerAddOptions
type ToastManagerAddOptions = {
id?: string;
title?: React.ReactNode;
type?: string;
description?: React.ReactNode;
timeout?: number;
priority?: 'low' | 'high';
transitionStatus?: 'starting' | 'ending';
onClose?: () => void;
onRemove?: () => void;
actionProps?: Omit<
React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>,
'ref'
>;
positionerProps?: ToastManagerPositionerProps;
data?: {};
};ToastManagerEvent
type ToastManagerEvent = { action: 'add' | 'close' | 'update' | 'promise'; options: any };ToastManagerPositionerProps
type ToastManagerPositionerProps = {
anchor?: Element | null;
style?:
| React.CSSProperties
| ((
state: Toast.Positioner.State,
) => React.CSSProperties | undefined);
className?:
| string
| ((
state: Toast.Positioner.State,
) => string | undefined);
side?:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'inline-end'
| 'inline-start';
positionMethod?: 'absolute' | 'fixed';
sideOffset?:
| number
| ((data: {
side:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'inline-end'
| 'inline-start';
align: 'start' | 'center' | 'end';
anchor: { width: number; height: number };
positioner: { width: number; height: number };
}) => number);
align?: 'start' | 'center' | 'end';
alignOffset?:
| number
| ((data: {
side:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'inline-end'
| 'inline-start';
align: 'start' | 'center' | 'end';
anchor: { width: number; height: number };
positioner: { width: number; height: number };
}) => number);
collisionBoundary?: Boundary;
collisionPadding?: Padding;
sticky?: boolean;
arrowPadding?: number;
disableAnchorTracking?: boolean;
collisionAvoidance?: CollisionAvoidance;
render?:
| ReactElement
| ((
props: HTMLProps,
state: Toast.Positioner.State,
) => ReactElement);
}ToastManagerPromiseOptions
type ToastManagerPromiseOptions = {
loading: string | ToastManagerUpdateOptions<{}>;
success:
| string
| ToastManagerUpdateOptions<{}>
| ((result: Value) => string | ToastManagerUpdateOptions<{}>);
error:
| string
| ToastManagerUpdateOptions<{}>
| ((error: any) => string | ToastManagerUpdateOptions<{}>);
};ToastManagerUpdateOptions
type ToastManagerUpdateOptions = {
title?: React.ReactNode;
type?: string;
description?: React.ReactNode;
timeout?: number;
priority?: 'low' | 'high';
onClose?: () => void;
onRemove?: () => void;
actionProps?: Omit<
React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>,
'ref'
>;
positionerProps?: ToastManagerPositionerProps;
data?: {};
};UseToastManagerReturnValue
type UseToastManagerReturnValue = {
toasts: ToastObject<{}>[];
add: (options: ToastManagerAddOptions<{}>) => string;
close: (toastId: string) => void;
update: (toastId: string, options: ToastManagerUpdateOptions<{}>) => void;
promise: (
promise: Promise<Value>,
options: ToastManagerPromiseOptions<Value, {}>,
) => Promise<Value>;
};