Skip to content

[toast] Render content passed via the render prop in Action - #5211

Closed
m2na7 wants to merge 3 commits into
mui:masterfrom
m2na7:fix/toast-action-render-prop
Closed

m2na7 wants to merge 3 commits into
mui:masterfrom
m2na7:fix/toast-action-render-prop

Conversation

@m2na7

@m2na7 m2na7 commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Toast.Action gates rendering on the presence of resolved children (toast.actionProps?.children ?? children). When the action is provided through the render prop with content but no children — <Toast.Action render={<button>Undo</button>} /> — that content was dropped: shouldRender was false and the element never mounted.

The fix gates on the evaluated element's resolved children, so a render that carries content mounts, while a childless, styling-only render={<MyButton />} — used purely to customize the element in a shared template, with the action content coming from toast.actionProps — stays conditional and does not force-mount an empty button for every toast. Whether resolved children count as content is decided by a shared isRenderableNode predicate matching React renderability (numeric/bigint children including 0 are content; nullish values, booleans, empty strings, and arrays containing only those are not), behind a React.isValidElement check so a render function returning null can't crash the children lookup.

Linked Issues

Related to #2809 (same root cause, for Title and Description).

Additional context

Follow-up to #5210, kept separate to scope that PR to the reported issue. The shared util packages/react/src/toast/utils/isRenderableNode.ts (+ tests) is included byte-identically on both PRs, so whichever merges second resolves it cleanly and its squash then effectively touches only the Toast.Action files. Tests cover the render prop as an element and as a function, the childless-render case, a render function returning null, numeric 0 as render-prop and plain child, and empty-array children.

@pkg-pr-new

pkg-pr-new Bot commented Jul 13, 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@5211
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5211
    

commit: 8a22715

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+131B(+0.03%) 🔺+44B(+0.03%)

Details of bundle changes

Performance

Total duration: 1,444.53 ms +160.20 ms(+12.5%) | Renders: 78 (+0) | Paint: 2,253.41 ms +260.74 ms(+13.1%)

Test Duration Renders
Select mount (200 instances) 173.85 ms 🔺+37.73 ms(+27.7%) 3 (+0)
Dialog mount (300 instances) 62.52 ms 🔺+18.05 ms(+40.6%) 1 (+0)
Combobox type — 500 items, all stay mounted (type "Row ") 40.45 ms 🔺+13.23 ms(+48.6%) 11 (+0)
Combobox type — 500 items, narrows to ~11 (type "Row 25") 46.05 ms 🔺+8.80 ms(+23.6%) 17 (+0)

10 tests within noise — details

Metric alarms

Test Metric Change
Select mount (200 instances) bench:paint 🔺 +51.80 ms
Dialog mount (300 instances) bench:paint 🔺 +30.76 ms
Combobox type — 500 items, all stay mounted (type "Row ") bench:paint#combobox-open 🔺 +27.90 ms
Combobox type — 500 items, all stay mounted (type "Row ") bench:paint 🔺 +27.90 ms

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

@netlify

netlify Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8a22715
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a549e1cfbb2d30009d7538d
😎 Deploy Preview https://deploy-preview-5211--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@atomiks

atomiks commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

render !== undefined isn't quite the right check, as render describes how to render, not whether. It's common to pass render purely to customize the element in a shared template:

<Toast.Root toast={toast}>
  <Toast.Action render={<MyButton />} /> {/* content comes from toast.actionProps */}
</Toast.Root>

With this change, an empty action button mounts for every toast, even those without an action.

A better check is to also treat the render element's own children as content, so render={<button>Undo</button>} mounts while childless render={<MyButton />} stays conditional. Components that encapsulate their own content still must specify children externally (<Toast.Action render={<MyButton />}>Undo</Toast.Action>)

-  const shouldRender = Boolean(computedChildren);
+  // The render prop's evaluated element is the source of truth for content:
+  // `render` element children override `computedChildren` in the merge, and a
+  // render function's returned element carries its own children.
+  const shouldRender = Boolean(
+    (element.props as { children?: React.ReactNode }).children,
+  );
+

@m2na7

m2na7 commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, you're right
render !== undefined over-renders, mounting an empty element for every toast without an action. Adopted your evaluated-element check (Boolean(element.props.children)) so render={<button>Undo</button>} mounts while a childless render={<MyButton />} stays conditional, and added a regression test for the childless case. Applying the same fix to #5210 for Title/Description.

@m2na7
m2na7 force-pushed the fix/toast-action-render-prop branch 2 times, most recently from 99fe7e1 to 73f4bac Compare July 13, 2026 06:53
@atomiks atomiks added type: bug It doesn't behave as expected. component: toast Changes related to the toast component. labels Jul 13, 2026
Toast.Action gated mounting on the presence of children, so content supplied
through the `render` prop with no children was dropped: `shouldRender` was
false and the component returned null before the render prop element or
function could mount.

This is the same oversight fixed for Toast.Title and Toast.Description in
mui#2809; apply the same fix so `render` behaves consistently across all three
optional Toast content parts.
@m2na7
m2na7 force-pushed the fix/toast-action-render-prop branch from 73f4bac to c1edef0 Compare July 13, 2026 07:09
m2na7 added 2 commits July 13, 2026 16:49
…ntent

Boolean(children) rejected renderable-but-falsy content (0, 0n, NaN) and
accepted empty-but-truthy content ([], [null]). Use a shared isRenderableNode
predicate so a numeric render prop child such as render={<button>{0}</button>}
mounts while an empty array stays conditional.
Cover the ordinary-children behavior the renderability gate also changes
(a plain {0} child now mounts, an empty-array child no longer mounts an
empty element) and guard against a render function returning null crashing
the children lookup. Lock nested-array recursion in the shared predicate.
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 13, 2026
@m2na7 m2na7 closed this Jul 14, 2026
@m2na7
m2na7 deleted the fix/toast-action-render-prop branch July 14, 2026 02:18
@m2na7

m2na7 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated into #5210 — after #5219 deduplicated the Title/Description logic into a shared helper, the same gate now covers all three parts, so the Action fix lives there.

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. PR: out-of-date The pull request has merge conflicts and can't be merged. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants