Skip to content

[toast] Introduce a store - #3464

Merged
flaviendelangle merged 28 commits into
mui:masterfrom
flaviendelangle:store-toasts
Feb 9, 2026
Merged

flaviendelangle merged 28 commits into
mui:masterfrom
flaviendelangle:store-toasts

Conversation

@flaviendelangle

@flaviendelangle flaviendelangle commented Dec 8, 2025 •

Copy link
Copy Markdown
Member

Fixes #3922

This should not be part of the stable release it can be reviewed after 👍

  • Introduce a store in ToastProvider
  • Use a memoized selectors to compute the index, visible index and offset of each toast (instead of looping on the list of each item)
  • Move all the actions to a store class to reduce the usage of useStableCallback

@flaviendelangle flaviendelangle self-assigned this Dec 8, 2025
@flaviendelangle flaviendelangle added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. component: toast Changes related to the toast component. labels Dec 8, 2025
@pkg-pr-new

pkg-pr-new Bot commented Dec 8, 2025 •

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@3464
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@3464
    

commit: 40845ef

@mui-bot

mui-bot commented Dec 8, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+2.84KB(+0.68%) 🔺+630B(+0.47%)

Details of bundle changes


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

@netlify

netlify Bot commented Dec 8, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 40845ef
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/698988f1c19ae10008dd015a
😎 Deploy Preview https://deploy-preview-3464--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.

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Dec 8, 2025
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Dec 8, 2025
return (
<ToastViewportContext.Provider value={contextValue}>
{numToasts > 0 && prevFocusElement && <FocusGuard onFocus={handleFocusGuard} />}
<React.Fragment>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This context was never used.
But if we need viewportRef, we can now use useToastContext since it's stable accross render 👌

resumeTimers,
viewportRef,
windowFocusedRef,
setFocused,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For me the logic still works here but I would like a double check 👀

@flaviendelangle
flaviendelangle marked this pull request as ready for review December 9, 2025 09:24
@michaldudak

Copy link
Copy Markdown
Member

I'd rather not include this in v1. Introducing a Store to other components usually came with a couple of subtle bugs, so let's test it without rushing after the release.

@flaviendelangle

Copy link
Copy Markdown
Member Author

I'd rather not include this in v1.

Clearly, I'm not in favor of rushing anything before the stable, unless the gains are huge 👍

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Dec 9, 2025
@flaviendelangle

Copy link
Copy Markdown
Member Author

@greptileai

@flaviendelangle

Copy link
Copy Markdown
Member Author

@michaldudak this PR is ready for another round of review 🙏

@michaldudak

Copy link
Copy Markdown
Member

Review by Codex:

The new updateToast implementation only mutates the toast list and no longer adjusts timers when update() changes timeout or type. This breaks existing behaviors where updating a toast from timeout: 0 to a positive value should start auto-dismiss, or updating a loading toast to a non-loading type should schedule a timer; conversely, setting timeout: 0 won’t cancel an already running timer. This is a regression from the previous provider logic and will cause updated toasts to never dismiss or to dismiss unexpectedly based on stale timers.

@flaviendelangle

Copy link
Copy Markdown
Member Author

@michaldudak I think the did the review before this morning's fix.
Here is Claude Code answer (we really live in a wonderful world 😆 )

Analysis
The Original Problem
The commit 4da0b69 ("Move actions to the store") had a simple updateToast implementation that only mutated the toast list:

updateToast(id: string, updates: ToastManagerUpdateOptions) {
this.set('toasts', this.state.toasts.map((toast) =>
toast.id === id ? { ...toast, ...updates } : toast
));
}
This was a regression because the original ToastProvider code had timer adjustment logic in the update function.

The Fix (Already Applied)
The commit b39a218 ("Fix tests + review: Michal") from today added the timer management logic back to updateToast at store.ts:174-218. The fix:

Cancels timers when timeout: 0 is set or type changes to loading
Schedules new timers when:
Timeout changes from 0 to a positive value
Type changes from loading to another type
Timeout value is explicitly updated
Pauses timers appropriately when viewport is expanded/out of focus
The promiseToast method was also simplified to delegate timer handling to updateToast by passing timeout in the update options.

Verdict
No action required - the issue identified by Codex has been addressed in the latest commit. The current implementation in store.ts:174-218 properly handles all the timer scenarios:

timeout: 0 → positive value: starts auto-dismiss timer
type: 'loading' → 'success'/'error': schedules timer
timeout: X → timeout: 0: cancels running timer

@michaldudak

Copy link
Copy Markdown
Member

Yup, Codex agrees, it's fixed :)

@michaldudak michaldudak 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.

Just one nit. Besides that it looks good to me

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Codex review:

ToastManagerUpdateOptions now allows internal-only fields (ref, height, transitionStatus, limited) because it’s Partial<Omit<ToastObject, 'id'>>. That expands the public API and lets consumers put the toast into invalid states. Consider keeping the public type narrow (as before) and using an internal update type/method for ToastRoot height updates.

Some optimizations as well we could do:

  • Update createToastManager to use Set instead of an array
  • Limit handling still does multiple passes and closeToast does indexOf inside map (O(n²)). This is a hot path and was one of the perf fixes we made; it can be a single pass with an active index counter
  • updateToast always calls setToasts before checking whether the toast exists. If an update races with removal (or uses a stale id), this becomes a no‑op state update that still forces subscriptions to re-run. Move the prevToast null check ahead of setToasts or gate on toastIndex !== -1.

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 2, 2026
@atomiks atomiks mentioned this pull request Feb 5, 2026
1 task done
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 6, 2026
@flaviendelangle
flaviendelangle merged commit 26185fb into mui:master Feb 9, 2026
7 checks passed
@flaviendelangle
flaviendelangle deleted the store-toasts branch February 9, 2026 07:21
@oliviertassinari oliviertassinari changed the title [toasts] Introduce a store [toast] Introduce a store Feb 28, 2026
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: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[toast] Critical toast properties calculations are based on object referential equality

4 participants