- Label
- Icon label
- Field
- Form
- Frontmatter form
- Questionnaire
- Input
- Input group
- Input OTP
- Password input
- Password strength
- Code input
- Number field
- Tag input
- Textarea
- Checkbox
- Radio group
- Language toggle group
- Emoji appearance toggle group
- Emoji picker
- Avatar picker
- AI provider picker
- Select
- Native select
- Combobox
- Language combobox
- Slider
- Calendar
- Switch
- Alert
- Avatar
- Badge
- Card
- AI provider card
- Model info card
- Model list
- Empty
- Item
- Tree item
- File tree
- Icon media
- File type icon
- Font preview
- Image preview
- Kbd
- Table
- Data table
- Data table column header
- Chart
- Code Block
- Code card
- Markdown view
- Attachment
- Bubble
- Marker
- Message
- Chat message
- Chat suggestion item
- Message scroller
- Trace
- Progress
- Spinner
- Sonner
- Toast
- Unsaved changes toolbar
- Unsaved indicator
- Status indicator
- Permission card
- Separator
- Skeleton
- Animated grid pattern
- Meteors
Installation#
pnpm dlx shadcn@latest add @zui/sonnerThe CLI writes components/ui/sonner.tsx and lib/motion.ts, installs Icon,
Spinner, Text, Text children and Utils, and adds
lucide-react-native, react-native-reanimated, react-native-safe-area-context and react-native-worklets.
Sonner reads useSwipeToDismiss from lib/motion.ts, which older copies of that file lack. Adding sonner,
toast, trace or attachment again writes the current one, so let the CLI overwrite lib/motion.ts when it asks;
keeping the old file fails the bundle with a missing export.
Put one Toaster at the app's root, after its PortalHost, so toasts draw above the screen and its overlays:
import { PortalHost } from '@rn-primitives/portal';
import { Toaster } from '@/components/ui/sonner';
export default function RootLayout() {
return (
<>
<Stack />
<PortalHost />
<Toaster />
</>
);
}Usage#
import { toast } from '@/components/ui/sonner';<Button onPress={() => toast('Event has been created', { description: 'Monday, January 3rd at 6:00pm' })}>
<Text>Show Toast</Text>
</Button>API reference#
toast#
toast(message, options?) raises a toast and returns its id; toast.success, toast.info, toast.warning,
toast.error and toast.loading raise one with that type's icon, and toast.message one with none. Pass an
id to update the toast with that id in place, such as a loading toast once its work is done.
toast.dismiss(id?) closes the toast with id, or every toast. toast.promise(promise, { loading, success, error, description, finally }) shows loading while the promise runs, then success or error, each a node or
a function of the result, and returns { unwrap }, which gives the promise back. toast.custom((id) => node)
draws your node in place of the toast's parts. toast.getToasts() lists the toasts that are up.
Options, with sonner's meaning:
| Option | Type |
|---|---|
id | number | string |
description | ReactNode | () => ReactNode |
duration | number (ms; Infinity keeps it up) |
icon | ReactNode |
action, cancel | { label, onClick(event) } or a node |
onDismiss, onAutoClose | (toast) => void |
dismissible | boolean, true by default |
closeButton, richColors, invert | boolean |
position | the Toaster's position |
unstyled | boolean |
className, descriptionClassName, classNames | string, string, ToastClassnames |
testId | string |
A toast stays up for its duration, the Toaster's, or 4 seconds, and a loading toast until it is updated or
dismissed. Its time stops while the stack is spread open, and while the app is in the background. A press on
action or cancel runs its onClick and closes the toast. onAutoClose runs when its time is up, and
onDismiss when it is closed any other way.
Toaster#
| Prop | Type | Default |
|---|---|---|
position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 'bottom-right' |
expand | boolean | false |
visibleToasts | number | 3 |
duration | number | 4000 |
gap | number | 14 |
offset | number | string | { top, right, bottom, left } | 24 |
mobileOffset | number | string | { top, right, bottom, left } | 16 |
closeButton | boolean | false |
richColors | boolean | false |
invert | boolean | false |
swipeDirections | ('top' | 'right' | 'bottom' | 'left')[] | its edge and side |
toastOptions | { className, classNames, descriptionClassName, duration, closeButton, unstyled, closeButtonAriaLabel } | - |
icons | { success, info, warning, error, loading, close } | - |
hotkey | string[] | ['altKey', 'KeyT'] |
containerAriaLabel | string | 'Notifications' |
className | string | - |
Newer toasts stack in front of older ones, each older one 14px behind and 5% smaller, and only the first
visibleToasts show. A pointer over the stack on the web, or a touch on it on a device, spreads it into a list
gap apart; a touch keeps it spread for 4 seconds after the finger lifts. As upstream's, it closes up again
on Escape while focus is in it, and once one toast or none is left. On a screen 600px wide or less a
toast is as wide as the screen less mobileOffset either side; on a wider one it is 356px wide, offset from
its corner. The bottom and top offsets add the safe area's insets when a SafeAreaProvider is mounted. A toast
slides in from its edge over 400ms and fades out over 200ms, and appears and goes at once when the system asks
for reduced motion. A swipe of 45px toward one of swipeDirections, or a quick flick, closes it; a swipe any
other way pulls against it and springs back.
Screen readers read each new toast, and each change to one, as its title and description: iOS announces it, and
on Android and the web the toaster is a polite live region. On the web it is a region named by
containerAriaLabel and the hotkey, each toast is a status, and the hotkey spreads the stack and moves focus to
it. The stack stays spread when focus then leaves it, as upstream's does, until Escape or a pointer leaving it
closes it up.
useSonner#
Returns { toasts }, the toasts that are up, kept current.
Differences from upstream#
toastcomes from@/components/ui/sonner, where upstream imports it from thesonnerpackage: sonner draws HTML, so the toaster and its queue are written here, on React Native views and Reanimated.themeis left out: the toasts take the app's colour scheme through the theme'sdark:colours, as every native item does.- An action's
onClickgets React Native's press event, not a mouse event. dir,style,actionButtonStyleandcancelButtonStyleare left out, as React Native styles by class here, and so are the Toaster'sidand a toast'stoasterId, which pair toasts to one of several Toasters: an app has one.- A touch spreads the stack for 4 seconds after the finger lifts, where upstream's spreads while a mouse is over it: a finger leaves no pointer resting on the stack.
- iOS reads no live region, so a new toast is announced through
AccessibilityInfo.announceForAccessibility. - A toast is rounded by the style's
cn-toastslot (rounded-2xl, 16px), which upstream's Toaster also passes; on the web sonner's own stylesheet overrides it with 8px. There is no sonner stylesheet here, so the slot's radius is what draws.