Repository navigation
Conversation
`useToast` now wraps sonner's `toast` and gives `toast.error`, the error state of `toast.promise`, and any toast with an `action` the defaults `duration: Infinity` and `closeButton: true`. Options passed by the caller win. `toast.custom` passes through, because sonner draws neither the action nor the close button for it. The toast docs described a position, count and durations that the `<Toaster />` never set (top right, one at a time, success 3 s, error sticky). They now describe sonner's real defaults (bottom right, up to 3, 4 s) plus the two defaults this change adds, and tell consumers not to make a toast the only way to do something. Part of tailor-inc/platform-planning#2349 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Code Metrics Report
Details | | main (923933d) | #687 (f13e605) | +/- |
|---------------------|----------------|----------------|-------|
+ | Coverage | 62.9% | 63.0% | +0.0% |
| Files | 294 | 294 | 0 |
| Lines | 13483 | 13592 | +109 |
+ | Covered | 8485 | 8564 | +79 |
+ | Test Execution Time | 4m3s | 2m58s | -1m5s |Code coverage of files in pull request scope (14.8% → 15.7%, patch 94.0%)
Reported by octocov |
Sonner merges an update by `id` into the stored toast, so the `duration: Infinity`
and `closeButton: true` that `useToast` gives an error or action toast survived
into a later `toast.success("Saved", { id })`, which then never closed. A call
without an `action` that updates by `id` now passes an explicit `undefined` for
both unless the caller sets them.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
interacsean
left a comment
There was a problem hiding this comment.
I think there’s an edge here with dismissible: false. Sonner still renders the close button when a toast isn’t dismissible, but the click does nothing (onClick: disabled || !dismissible ? () => {} : …).
I checked this in a quick test: the button renders, and after clicking it the toast is still there. So toast.error("…", { dismissible: false }) used to go away after 4s. Now it stays forever with a × that doesn’t work. The docs cover the “stays until toast.dismiss(id)” part, but a dead button is a bit odd for the user.
What do you think about defaulting the close button off when the caller opts out of dismissing?
closeButton: data?.closeButton ?? data?.dismissible !== false,
With `dismissible: false`, Sonner draws a close button that does nothing and ignores swipe and Escape, so a sticky default would leave `toast.dismiss(id)` as the only way to end the toast. Such toasts now keep Sonner's defaults (no close button, 4 s), as before. For `toast.promise`, only the error state's own `dismissible` counts, because Sonner resets the flag when the loading toast becomes the error toast. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
`useToast` now reads its per-kind defaults from the AppShell configuration. The
built-in defaults are unchanged (`error` and `action` toasts stay until the user
closes them, with a close button). An app replaces a kind as a whole with
`<AppShell toastDefaults={{ error: {} }}>`, which falls back to sonner's own
defaults for that kind, or sets its own `duration` / `closeButton`.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
# Conflicts: # docs-manifest.json
unicorn/consistent-function-scoping: it captures nothing from createToast. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
@interacsean Replying to your review about Thank you, I reproduced this. I went one step further than hiding the close button: when the caller passes The reason: the sticky defaults exist so that the user can read an error and close it. A toast the user is not allowed to close should not be kept open by them. Hiding the close button alone would still leave it open forever. For |
# Conflicts: # docs-manifest.json
…inds Following the review on #687: - The built-in default `{ duration: Infinity, closeButton: true }` is written out in AppShell instead of relying on Sonner's defaults. - A field set in `toastDefaults` replaces that field only; fields and kinds left out keep the built-in default. - An empty object for a kind (`error: {}`, `action: {}`) is a type error, through a generic `AtLeastOne<T>` utility. - docs/migrations.md gets a 1.21.0 entry with the steps to keep the previous behavior. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rm/app-shell into fix/toast-defaults
The previous commit was formatted with a print width that is not the repository's, which rewrapped unrelated lines in use-toast.test.tsx and appshell-context.tsx. This restores them. No behavior change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nto fix/toast-defaults # Conflicts: # docs-manifest.json
`toastDefaults={{ ... }}` written inline is a new object on every render.
The configurations memo depended on its identity, so every AppShell render
rebuilt the config context and `useToast` returned a new `toast`. An effect
with `toast` in its dependencies then ran again and could stack duplicate
sticky error toasts.
AppShell now keys `toastDefaults` by its values. A test renders the real
AppShell, re-renders it with an inline object, and checks that `toast` keeps
its identity until a value changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sonner copies a call-level `action` into the loading toast and merges the success state into it, so the success toast shows the action but closed after 4 s. `useToast` now gives such a call the action defaults. The error state is resolved first from the call as written, so it keeps the error default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the follow-up, and for the clear suggestion. I updated the PR.
While testing, I also fixed two related issues:
Could you take another look when you have time? |
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> # Conflicts: # docs-manifest.json
|
Looks good now! ✅ Leave the final approval to @interacsean |
Summary
The toast docs and the implementation did not match.
<Toaster />passes no position, count or duration, so Sonner 2.0.8 defaults apply (bottom right, up to 3 visible, 4 s for every type). The docs said top right, one at a time, success 3 s, and "error is sticky". These docs are also emitted into the consumer skill, so an AI agent could report a failed save only throughtoast.errorand the message would close after 4 s.This PR fixes the two sides separately, as agreed in tailor-inc/platform-planning#2349:
Docs follow the implementation for position and count.
patterns/interaction-toastandapi/use-toastnow describe bottom right, up to 3 visible, 4 s, and add one rule: do not make a toast the only way to do something (an Undo in a toast needs a way back on the page too).The implementation follows the docs for errors.
useToastwraps Sonner'stoastand givestoast.error, the error state oftoast.promise, and any toast with anactionthe defaultsduration: InfinityandcloseButton: true. AdurationorcloseButtonpassed by the caller wins.toast.custompasses through, because Sonner draws neither the action nor the close button for it. The return type staystypeof toast. A call without anactionthat updates one of these toasts byid(for exampletoast.success("Saved", { id })aftertoast.error) resetsdurationandcloseButtonto Sonner's defaults unless it passes its own, because Sonner merges an update byidinto the stored toast. Atoast.promisecall with anactionalso gets the action defaults, because Sonner shows that action in the loading and success states.The app can change these defaults field by field.
AppShelltakestoastDefaults?: { error?, action? }, next tolocaleandtimeZone. Each kind takes{ duration?, closeButton? }with at least one field set. The built-in default is written out in AppShell ({ duration: Infinity, closeButton: true }), so it does not depend on Sonner's defaults. A field set intoastDefaultsreplaces that field only; fields and kinds left out keep the built-in default. An empty object such aserror: {}is a type error. To keep the old behavior, an app sets both fields:{ duration: 4000, closeButton: false }. AppShell comparestoastDefaultsby value, so an inline object does not rebuild the config context or change thetoastthatuseToastreturns on every render. The changeset is minor, anddocs/migrations.mdhas a 1.21.0 entry with the steps to keep the old behavior.dismissible: falseis left alone. A toast the user is not allowed to close does not get the sticky defaults; it keeps Sonner's behavior (no close button, closes after 4 s), as before this PR. Fortoast.promise, only the error state's owndismissiblecounts.Not changed: position, visible count, success/info/warning toasts without an action, and the z-index from #668.
Part of tailor-inc/platform-planning#2349
Why
useToastand not<Toaster toastOptions>toastOptions.durationapplies to every type, so it cannot keep errors open while success toasts still close. Sonner resolvestoast.duration || toasterDuration || 4000andtoast.closeButton ?? toasterCloseButtonper toast, so the per-call defaults are the only place where this can be done without changing the visible count or position.Test plan
use-toast.test.tsx: error and action toasts getduration: InfinityandcloseButton: true; caller'sduration/closeButton(including an explicitundefined) win; success, info and plain toasts are unchanged; with fake timers only the error toast survives 5 s;toast.promiseerror state gets the defaults, the call's and the result's own options win, and the success state is unchanged; atoast.promisewith anactionkeeps its success toast, and its error state still uses the error default.toastDefaults: a field overrides only that field;{ duration: 4000, closeButton: false }restores the old behavior;error: {}andaction: {}are type errors (@ts-expect-error); a realAppShellre-rendered with an inlinetoastDefaultskeeps the sametoastuntil a value changes.pnpm type-check,pnpm lint,pnpm test(core: 3112 tests),turbo run check-dts,pnpm docs:sync,pnpm docs:check.toastDefaults. No setting: error, action andtoast.promisewith an action stay with a close button; success andtoast.promisewithout an action close after 4 s.{ duration: 4000, closeButton: false }for both kinds: every toast closes after 4 s without a close button.error: { duration: 10_000 }: errors close after 10 s and keep the close button; action toasts stay.Notes for review
toastimported fromsonnerdirectly does not get the new defaults. The docs say so.aria-labelis Sonner's default "Close toast" (English only). Wiring it todefineI18nLabelsis a separate change.toast.erroron every rejected commit; with sticky errors these now stack up to 3. The demo is out of scope here; a follow-up can give the call anidso it replaces the previous one.🤖 Generated with Claude Code