Repository navigation
[toast] Add upsert support to add method - #4440
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
945e9b7 to
cabaa67
Compare
flaviendelangle
left a comment
There was a problem hiding this comment.
PR Review: [toast] Add upsert support to add method (#4440)
Author: atomiks | Base: master | +651 / -8 across 10 files
Overview
This PR adds upsert semantics to toastManager.add(): when a caller passes an id that already exists, instead of creating a duplicate toast, the existing one is updated in place, its auto-dismiss timer is refreshed, and a new updateKey counter increments so renderers can replay visual effects (e.g. a CSS pulse animation). A closing toast with the same id is removed and replaced fresh. Documentation and a deduplicated toast demo are included.
Code Quality & Correctness
Positives
- The upsert logic in
packages/react/src/toast/store.tsis clean and well-structured. The three-way branch (toast ending -> remove & re-add, toast exists -> update in place, toast missing -> create new) handles all states correctly. - Stripping
transitionStatusfrom the upsert update (const { id: semanticId, transitionStatus: nextTransitionStatus, ...updates } = toast) is a smart defensive move — it prevents callers from accidentally putting a toast into'ending'state viaadd(). This is explicitly tested too. updateKeyis properly excluded from bothToastManagerAddOptionsandToastManagerUpdateOptionsOmit lists, preventing external callers from manually setting it.- Timer refresh via
resetTimer: truein theUpdateToastBehaviortype is a clean extension of the existing timer rescheduling logic. - Using
this.state.toastsinstead of the destructuredtoastsafter theremoveToastcall is correct — the earlier destructuring would have stale data after removal.
Concerns
-
Unused destructured variables in upsert path:
const { id: semanticId, transitionStatus: nextTransitionStatus, ...updates } = toast;
semanticIdandnextTransitionStatusare never used. While this is intentional destructuring-as-omission, the names are misleading. A simpler approach:const { id: _, transitionStatus: __, ...updates } = toast;
Or even better, a small helper. As-is, linters may flag unused variables depending on config.
-
updateKeyinitialization inconsistency: New toasts getupdateKey: 0, but the type definition saysupdateKey?: number | undefined. This is fine semantically, but it means the very first render will seeupdateKey: 0(falsy), and the demo relies onif (toast.updateKey)to decide whether to animate — so the first add correctly skips the pulse. This is clever but subtle. A brief code comment in the demo would help future readers understand the0-is-falsy trick. -
Missing test: upsert with
limitinteraction. The PR doesn't test what happens when you upsert a toast that was markedlimited: true. If a toast was pushed out by the limit and then upserted, doeslimitedget cleared? The current code would update it in place but wouldn't clearlimitedunless the update explicitly includeslimited: false. This could be an edge case worth covering. -
No Tailwind variant of the demo. The project structure (per AGENTS.md) mentions both CSS Modules and Tailwind CSS versions of demos. Only the CSS Modules version is included here. This may be intentional for now, but it's worth noting.
Tests
Excellent test coverage. The PR adds:
- createToastManager.test.tsx: Upsert test with timer refresh verification (timer resets on re-add at 900ms, doesn't expire at 200ms but does at 1000ms post-upsert).
- useToastManager.test.tsx: Four new test cases covering:
- Replacing a closing toast with same
id - Ignoring
transitionStatusduring upsert updateKeyincrementing onadd()upsertupdateKeyincrementing onupdate()
- Replacing a closing toast with same
The timer refresh test is particularly well-designed — it advances the clock to 900ms, upserts, then verifies the toast survives past 200ms more (proving the old timer was canceled) and dismisses after the full new timeout.
Documentation
- The
page.mdxadditions explain the behavior clearly and concisely. - The
types.mdupdates add proper JSDoc forupdateKeyand the upsertidbehavior onToastManagerAddOptions. - The demo is well-crafted — the alternating
PulseEven/PulseOddanimation class trick is a practical pattern for replaying CSS animations without remounting.
Minor Nits
- The two
@keyframesin the CSS (pulse-evenandpulse-odd) are identical. This is intentional (toggling animation name forces replay), but a CSS comment explaining why they're duplicates would save someone from "fixing" it later. - In the demo,
filter(Boolean).join(' ')for className construction works fine butclsxor similar is commonly used in this codebase (worth checking consistency).
Rating: 4 / 5
This is a well-executed feature PR. The core upsert logic is correct, the edge case of re-adding a closing toast is handled, timer refresh works properly, and the test coverage is thorough. The documentation and demo are high quality. I'm docking one point for the unused destructured variable naming, the missing limited interaction test, and the absence of a Tailwind demo variant. None of these are blocking — this is merge-ready with minor follow-ups.
Codex Review (GPT-5.4)I reviewed the full branch against a freshly fetched 1. Bugs / Issues (None)I do not see any remaining branch-relevant correctness defects after the follow-up fixes for stale replacement state, 2. API Design AssessmentStrengths:
3. Documentation / Examples AssessmentStrengths:
4. Test Coverage AssessmentStrengths:
Merge Confidence ScoreOverall merge confidence is 4/5. I’m highly confident in the core behavior and regression coverage for the shipped feature, and I would merge this version. The remaining risk is mostly around normal feature-level integration nuance rather than any concrete defect I can point to in the current branch. |
Closes #4228
https://deploy-preview-4440--base-ui.netlify.app/react/components/toast#deduplicated-toast
This adds built-in upsert behavior to
toastManager.add()when callers pass anid, so repeated adds update the existing toast instead of creating a duplicate. Matched toasts keep their identity, refresh their auto-dismiss timer, and a toast that is already ending is replaced with a fresh one instead of being revived mid-dismissal.It also adds
updateKeyto toast objects so custom renderers can replay styles when a toast is updated or upserted. The docs now include a deduplicated toast demo that usesupdateKeyto pulse the existing toast without remounting it.Changes
add({ id })as an upsert in the toast store and incrementupdateKeyon updates and upserts.updateKeychanges onadd()andupdate().idupsert behavior and add a deduplicated toast demo that replays a pulse viaupdateKey.