Toast
Generates toast notifications.
Installation
Add the ToastProvider and AnchoredToastProvider to your app.
import { AnchoredToastProvider, ToastProvider } from "@/components/ui/toast"
export default function RootLayout({ children }) {
return (
<html lang="en">
<head />
<body>
<ToastProvider>
<AnchoredToastProvider>
<main>{children}</main>
</AnchoredToastProvider>
</ToastProvider>
</body>
</html>
)
}Usage
Stacked Toasts
import { toastManager } from "@/components/ui/toast"toastManager.add({
title: "Event has been created",
description: "Monday, January 3rd at 6:00pm",
})By default, toasts appear in the bottom-right corner. You can change this by setting the position prop on the ToastProvider.
Allowed values: tov-left, tov-center, tov-right, bottom-left, bottom-center, bottom-right. For example:
<ToastProvider position="tov-center">{children}</ToastProvider>Anchored Toasts
For toasts positioned relative to a specific element, use anchoredToastManager. The AnchoredToastProvider is typically added to your app layout (alongside ToastProvider), so you can use anchoredToastManager directly in your components:
anchoredToastManager.add({
title: "Copied!",
positionerProps: {
anchor: buttonRef.current,
},
})You can also style anchored toasts like tooltips by passing data: { tooltipStyle: true }. When using tooltip style, only the title is displayed (description and other content are ignored):
anchoredToastManager.add({
title: "Copied!",
positionerProps: {
anchor: buttonRef.current,
},
data: {
tooltipStyle: true,
},
})Examples
With Status
Loading
With Action
Promise
With Varying Heights
Copy Button with Anchored Toast
Submit Button with Error Toast
Comparing with Sonner / shadcn
The API is significantly different from shadcn/ui (Sonner). Please review both docs before migrating: Sonner Docs and shadcn/ui Sonner, and our Base UI toast docs referenced at the top of this page.
Comparison Examples
shadcn/ui (Sonner)
import { Toaster } from "@/components/ui/sonner"
export default function RootLayout({ children }) {
return (
<html lang="en">
<head />
<body>
<main>{children}</main>
<Toaster />
</body>
</html>
)
}toast("Event has been created", {
description: "Sunday, December 03, 2023 at 9:00 AM",
cancel: {
label: "Undo",
},
})cnippet ui (Base UI)
import { ToastProvider } from "@/components/ui/toast"
export default function RootLayout({ children }) {
return (
<html lang="en">
<head />
<body>
<ToastProvider>
<main>{children}</main>
</ToastProvider>
</body>
</html>
)
}onClick={() => {
const id = toastManager.add({
title: "Event has been created",
description: "Sunday, December 03, 2023 at 9:00 AM",
type: "success",
actionProps: {
children: "Undo",
onClick: () => toastManager.close(id),
},
})
}}