Command palette

@yasinsensoy/web

Web · Components

Toaster

Places notifications shown with toast on the screen.

10 exports

Live example

Preview

Toasts stay in place

Toasts don't jump while the dialog opens and closes; you can show a new one from here too.

Code

import { Toaster, toast } from "@yasinsensoy/web";

// Once in the root layout:
<Toaster />

// Herhangi bir yerden:
toast.success("Payment received", { description: "The invoice was sent to your email." });

Events

  1. Events appear here when you interact with the preview.

Import

import { Toaster, toast } from "@yasinsensoy/web";

Toaster

componentClient component

Places notifications shown with toast on the screen. Add it once to the root layout.

Features:

  • Stack: Notifications stack by default and expand on mouse hover (see ToasterProps.stack). When a notification arrives, closes or changes height, the others move smoothly.
  • Always on top: It sits in the Popover API's top layer; it doesn't end up behind an open Dialog. While a modal Dialog is open, notifications stay clickable and are read by screen readers; when a Dialog opens or closes they stay in place, don't re-animate and their timers don't reset.
  • The timer pauses smartly: It doesn't run while the mouse is over, keyboard focus is inside or the tab is in the background; the user doesn't miss a notification they didn't see.
  • Mobile: Full width, respects notch and home bar insets, swipe to dismiss (bottom ones down, top ones up; toward the edge on wide screens).
  • Accessibility: Notifications are read to screen readers (errors immediately, others in turn). F8 jumps to the notification area.
  • Renders empty on the server; no hydration mismatch.
Toaster({ position, stack, duration, visibleToasts, hotkey, label: labelProp, className, style, effect, ...props }: ToasterProps): ReactPortal | null

Examples

// app/layout.tsx
<body>
  {children}
  <Toaster position="top-center" />
</body>

// Without stacking, always listed:
<Toaster stack={false} />

toast

constant

Shows a short notification (toast) and returns its id. Can be called from anywhere on the client (event handler, server action result, code outside components); for it to show, there must be a Toaster in the root layout. Does nothing if called on the server.

toast: ToastFunction

Examples

toast("Draft saved");
toast.success("Payment received", { description: "The invoice was sent to your email." });
toast.error("Connection lost", { action: { label: "Try again", onClick: retry } });

toast.promise(save(), {
  loading: "Saving…",
  success: (record) => `${record.name} saved`,
  error: "Couldn't save",
});

const id = toast.loading("Uploading…");
toast.success("Uploaded", { id }); // updates the same notification

Type of the notification; determines its color, icon and screen reader behavior.

type ToastVariant = "default" | "success" | "error" | "warning" | "loading"

ToastAction

interface

Action button in the notification.

NameTypeDescription
labelReactNodeButton text or content (e.g. icon and text).
onClick() => voidCalled when clicked; then the notification closes.

ToastOptions

interface

Options that can be given when showing a notification with toast.

NameTypeDefaultDescription
id?stringId of the notification. If called again with the same id, no new notification opens; the existing one is updated (e.g. "Loading" → "Saved").
description?ReactNodeDescription below the title.
variant?ToastVariant"default"Type of the notification.
icon?IconPropIcon before the title: name ("heart") or element. If omitted, the type's own icon is used; with false no icon is shown.
duration?numberAuto-close duration (ms). With Infinity it stays until the user closes it. If omitted, the Toaster setting is used; the "loading" type doesn't close by itself.
action?ToastActionAction button in the notification (e.g. "Undo").
onDismiss?() => voidCalled when the notification closes in any way.

ToastData

interface

Record of a notification shown on screen.

NameTypeDescription
idstringId of the notification.
titleReactNodeTitle of the notification.
variantToastVariantType of the notification.
closingbooleanIs the closing animation playing?
description?ReactNodeDescription below the title.
icon?IconPropIcon before the title: name ("heart") or element. If omitted, the type's own icon is used; with false no icon is shown.
duration?numberAuto-close duration (ms). With Infinity it stays until the user closes it. If omitted, the Toaster setting is used; the "loading" type doesn't close by itself.
action?ToastActionAction button in the notification (e.g. "Undo").
onDismiss?() => voidCalled when the notification closes in any way.

toast.promise messages; success and error messages can be produced from the result.

NameTypeDescription
loadingReactNodeTitle shown while the promise is pending.
successReactNode | ((value: T) => ReactNode)Title shown when the promise resolves; can also be produced from the result value.
errorReactNode | ((error: unknown) => ReactNode)Title shown when the promise rejects; can also be produced from the error.

ToastFunction

interface

Type of the toast function.

NameTypeDescription
successVariantShortcutShows a success notification.
errorVariantShortcutShows an error notification; read to screen readers immediately.
warningVariantShortcutShows a warning notification.
loadingVariantShortcutShows a "loading" notification that stays until the user closes or updates it.
dismiss(id?: string): voidCloses the notification. If no id is given, closes all of them.

On-screen position of the toasts.

type ToastPosition = "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right"

ToasterProps

interface

Props of the Toaster component.

Extends: Omit<ComponentProps<"ol">, "children">

NameTypeDefaultDescription
position?ToastPosition"bottom-right"Position on wide screens. On small screens notifications are always full width and only the top/bottom choice is kept.
stack?booleantrueStacks notifications: the newest in front, older ones slightly visible behind it. On mouse hover, when keyboard focus enters or when the stack is tapped on a touch screen, notifications expand into a list; they collapse again on leaving. With false they are always listed.
duration?number5000Default auto-close duration (ms).
visibleToasts?number3Maximum number of notifications visible at once. Extras queue up and are shown when space frees; their timers don't run while queued.
hotkey?string | null"F8"Shortcut key for jumping to the notification area with the keyboard (KeyboardEvent.key (https://developer.mozilla.org/docs/Web/API/KeyboardEvent/key)). Turned off with null.
label?stringthe `notifications` text (`"Bildirimler"`)Region name for screen readers.
effect?EffectPropEffect or effects of each notification (e.g. "beam", ["border", "shine"]). If omitted, UIProvider appearance.defaults.toast.effect. See BuiltinEffect.