Skip to content

[toast] Add upsert support to add method - #4440

Merged
atomiks merged 9 commits into
mui:masterfrom
atomiks:codex/toast-add-upsert
Mar 27, 2026
Merged

atomiks merged 9 commits into
mui:masterfrom
atomiks:codex/toast-add-upsert

Conversation

@atomiks

@atomiks atomiks commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

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 an id, 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 updateKey to toast objects so custom renderers can replay styles when a toast is updated or upserted. The docs now include a deduplicated toast demo that uses updateKey to pulse the existing toast without remounting it.

Changes

  • Treat add({ id }) as an upsert in the toast store and increment updateKey on updates and upserts.
  • Add regression coverage for manager and hook flows, including timer refresh, re-adding while a toast is closing, and updateKey changes on add() and update().
  • Document the id upsert behavior and add a deduplicated toast demo that replays a pulse via updateKey.

@atomiks atomiks added type: new feature Expand the scope of the product to solve a new problem. component: toast Changes related to the toast component. labels Mar 25, 2026
@pkg-pr-new

pkg-pr-new Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4440
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4440
    

commit: d33a692

@mui-bot

mui-bot commented Mar 25, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+365B(+0.08%) 🔺+128B(+0.09%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit d33a692
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69c6017399a4f60009827da4
😎 Deploy Preview https://deploy-preview-4440--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the codex/toast-add-upsert branch from 945e9b7 to cabaa67 Compare March 26, 2026 00:50
@atomiks
atomiks marked this pull request as ready for review March 26, 2026 02:16

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ts is 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 transitionStatus from 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 via add(). This is explicitly tested too.
  • updateKey is properly excluded from both ToastManagerAddOptions and ToastManagerUpdateOptions Omit lists, preventing external callers from manually setting it.
  • Timer refresh via resetTimer: true in the UpdateToastBehavior type is a clean extension of the existing timer rescheduling logic.
  • Using this.state.toasts instead of the destructured toasts after the removeToast call is correct — the earlier destructuring would have stale data after removal.

Concerns

  1. Unused destructured variables in upsert path:

    const { id: semanticId, transitionStatus: nextTransitionStatus, ...updates } = toast;

    semanticId and nextTransitionStatus are 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.

  2. updateKey initialization inconsistency: New toasts get updateKey: 0, but the type definition says updateKey?: number | undefined. This is fine semantically, but it means the very first render will see updateKey: 0 (falsy), and the demo relies on if (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 the 0-is-falsy trick.

  3. Missing test: upsert with limit interaction. The PR doesn't test what happens when you upsert a toast that was marked limited: true. If a toast was pushed out by the limit and then upserted, does limited get cleared? The current code would update it in place but wouldn't clear limited unless the update explicitly includes limited: false. This could be an edge case worth covering.

  4. 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 transitionStatus during upsert
    • updateKey incrementing on add() upsert
    • updateKey incrementing on update()

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.mdx additions explain the behavior clearly and concisely.
  • The types.md updates add proper JSDoc for updateKey and the upsert id behavior on ToastManagerAddOptions.
  • The demo is well-crafted — the alternating PulseEven/PulseOdd animation class trick is a practical pattern for replaying CSS animations without remounting.

Minor Nits

  • The two @keyframes in the CSS (pulse-even and pulse-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 but clsx or 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.

@atomiks

atomiks commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

Codex Review (GPT-5.4)

I reviewed the full branch against a freshly fetched upstream/master. This is a feature PR with docs, tests, and public-api secondary tags; after the recent edge-case follow-ups, I did not find any remaining correctness issues in the upsert or updateKey paths.

1. Bugs / Issues (None)

I do not see any remaining branch-relevant correctness defects after the follow-up fixes for stale replacement state, transitionStatus filtering, limited-toast coverage, and onRemove timing during replacement.

2. API Design Assessment

Strengths:

  • Treating add({ id }) as an upsert fits the existing toast manager surface naturally and avoids introducing another public method.
  • updateKey is a small additive primitive that gives custom renderers a reliable replay signal without exposing more internal store mechanics.
  • Excluding updateKey from both add/update options and filtering transitionStatus out of the upsert path keeps internal lifecycle fields owned by the store.

3. Documentation / Examples Assessment

Strengths:

  • The docs now explain both the semantic id upsert behavior and the updateKey replay mechanism in a way that matches the implementation.
  • The deduplicated toast demo shows a realistic pattern for replaying a visual effect while keeping the same toast mounted.
  • The added comments in the demo make the alternating animation-name trick and the initial updateKey: 0 behavior easier to reason about.

4. Test Coverage Assessment

Strengths:

  • The regression suite now covers manager and hook flows for timer refresh, replacing an ending toast, ignoring transitionStatus during upsert, incrementing updateKey on both add() and update(), preserving limited-state behavior, and preserving onRemove timing during replacement.
  • I reran pnpm test:jsdom useToastManager --no-watch, pnpm test:jsdom createToastManager --no-watch, and pnpm typescript on the current branch, and they passed.

Merge Confidence Score

Overall 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.

@atomiks
atomiks merged commit 463b2ca into mui:master Mar 27, 2026
7 checks passed
@atomiks
atomiks deleted the codex/toast-add-upsert branch March 27, 2026 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: toast Changes related to the toast component. type: new feature Expand the scope of the product to solve a new problem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[toast] Built-in support for deduplication?

3 participants