Capítulo 43 de 51

Chapter 43: Toast

Core Idea

A stacked (or anchored) notification system driven by a useToastManager() hook / global createToastManager() instance rather than declarative per-toast JSX — toasts are queued imperatively (add, close, update, promise) and rendered through a shared Provider/Viewport, with a rich CSS-variable/data-attribute set for stacking, swipe, and expand animations.

Key Concepts

  • Anatomy: ProviderPortalViewport → (stacked) RootContentTitle, Description, Action, Close; or (anchored) PositionerRootArrow + Content.
  • Imperative API: toasts aren't written as JSX per-toast — call add/close/update/promise from Toast.useToastManager() (inside React) or a Toast.createToastManager() instance (usable outside the React tree, e.g. in a plain function) passed to Provider's toastManager prop so both share one renderer.
  • promise method: ties a toast's pending/success/error states directly to a Promise — the standard "Saving…" → "Saved!"/"Failed" pattern without manual state juggling.
  • Global landmark navigation: F6 jumps keyboard focus into the toast viewport region — built-in, no extra wiring.
  • Swipe-to-dismiss: automatic on interactive elements; add data-base-ui-swipe-ignore manually to opt a specific descendant out (same mechanism as Drawer, ch022).
  • Stacking CSS variables: --toast-index (0 = frontmost, drives z-index/scale), --toast-offset-y (vertical offset when data-expanded, i.e. viewport hovered/focused), --toast-frontmost-height (clamp collapsed-stack height to the front toast), --toast-swipe-movement-x/-y (live swipe translation).
  • data-behind: marks a stacked toast's Content that sits behind the frontmost one — pair with data-expanded to fade content back in when the stack expands.
  • data-limited: toast exceeded the Provider's limit — stays mounted with native inert rather than unmounting, so it can still animate out or be hidden distinctly.
  • updateKey: increments on toast update/upsert — use to replay an attention animation (swap animation name, or use as a React key if remounting is acceptable).

Reference Tables

PartNotable propsNotable data attributes
Providerlimit, toastManager, timeout
Roottoast (required), swipeDirectiondata-expanded, data-limited, data-swiping, data-swipe-direction, data-type, animation attrs
Contentdata-behind, data-expanded
Viewportdata-expanded
manager methodsadd, close, update, promise

Key Takeaways

  1. Create one toastManager (via createToastManager()) and pass it to Provider whenever toasts need to be triggered from outside React (utility functions, non-component modules) — the hook-only API can't be called there.
  2. Use promise() for any async action reporting success/failure via toast, instead of manually calling add() then update()/close() on your own timers.
  3. Build the collapsed→expanded stack visual from --toast-index/--toast-frontmost-height/data-behind/data-expanded together — they're designed as one coordinated system, not independent knobs.

Connects To

  • ch022 (Drawer): shares the data-base-ui-swipe-ignore swipe-opt-out convention.
  • ch004 (Releases): v1.4.0 added update-by-ID for existing toasts.