Command palette

@yasinsensoy/native

React Native · Components

Toaster

Places notifications shown with toast on screen.

10 exports

Import

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

Toaster

component

Places notifications shown with toast on screen. Use it once at the app root, inside UIProvider. Same props as the web Toaster.

  • Always on top: Drawn above open Dialogs and Sheets; does not block the screen behind.
  • Stack: Newest in front; expands into a list on tap.
  • Timers pause smartly: Timers do not run while the stack is expanded or the app is in the background.
  • Swipe to dismiss: On phones towards the edge (bottom ones down, top ones up), sideways on tablets.
  • Accessibility: New notifications are read to screen readers.
  • Respects notch and bottom bar insets and the open keyboard; looks the same on iOS and Android.
Toaster({ position, stack, duration, visibleToasts, label: labelProp, }: ToasterProps): JSX.Element | null

Examples

<UIProvider>
  <App />
  <Toaster position="top-center" />
</UIProvider>

toast

constant

Shows a short notification (toast) and returns its id. Same API as toast on web. Can be called from anywhere; the app needs a Toaster for it to show.

toast: ToastFunction

Examples

toast("Draft saved");
toast.success("Payment received", { description: "The invoice was sent to your email." });
toast.error("Connection lost", { action: { label: "Retry", onClick: retry } });
toast.promise(save(), { loading: "Saving…", success: "Saved", error: "Could not save" });

Notification type; sets its color, icon and screen reader behavior.

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

ToastAction

interface

Action button in the notification.

NameTypeDescription
labelReactNodeButton text.
onClick() => voidCalled on press; then the notification closes (onClick on web).

ToastOptions

interface

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

NameTypeDefaultDescription
id?stringNotification id. Calling again with the same id does not open a new notification; it updates the existing one (e.g. "Loading" → "Saved").
description?ReactNodeDescription under the title.
variant?ToastVariant"default"Notification type.
icon?IconPropIcon at the start of the title. Defaults to the type's own icon; false shows no icon.
duration?numberAuto-dismiss duration (ms). Infinity keeps it until the user closes it. Defaults to the Toaster setting; the "loading" type never auto-dismisses.
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
idstringNotification id.
titleReactNodeNotification title.
variantToastVariantNotification type.
closingbooleanIs the exit animation playing?
versionnumberIncrements on every update; the timer restarts.
description?ReactNodeDescription under the title.
icon?IconPropIcon at the start of the title. Defaults to the type's own icon; false shows no icon.
duration?numberAuto-dismiss duration (ms). Infinity keeps it until the user closes it. Defaults to the Toaster setting; the "loading" type never auto-dismisses.
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 succeeds; can also be produced from the result value.
errorReactNode | ((error: unknown) => ReactNode)Title shown when the promise fails; 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 it or it is updated.
dismiss(id?: string): voidCloses the notification. Closes all of them if no id is given.

Position of notifications on screen (same as web).

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

ToasterProps

interface

Props of the Toaster component.

NameTypeDefaultDescription
position?ToastPosition"bottom-center"Position on wide screens (tablet). On phones notifications are always full width and only the top/bottom choice is kept.
stack?booleantrueStacks notifications: the newest in front, earlier ones peeking slightly behind. Tapping the stack expands them into a list (timers pause); tapping outside or tapping again collapses them. With false they are always listed one below another.
duration?number5000Default auto-dismiss duration (ms).
visibleToasts?number3Maximum number of notifications visible at once. The rest are queued and shown when space frees up; their timers do not run while queued.
label?stringthe `notifications` text (`"Bildirimler"`)Region name for screen readers.