Skip to content

[popups] Don't let a lone trigger claim a popup opened without a trigger - #5641

Merged
michaldudak merged 3 commits into
mui:masterfrom
michaldudak:claude/base-ui-issue-5632-562e2c
Sep 9, 2026
Merged

michaldudak merged 3 commits into
mui:masterfrom
michaldudak:claude/base-ui-issue-5632-562e2c

Conversation

@michaldudak

Copy link
Copy Markdown
Member

Closes #5632

Problem

handle.openWithPayload(value) opens a Dialog, Alert Dialog, or Drawer without associating a trigger, so activeTriggerId is null. When exactly one <Dialog.Trigger> is registered for the handle, useImplicitActiveTrigger in the Root claims it as the active trigger. That flips the trigger's isMountedByTrigger selector, and the trigger's data-forwarding effect then writes its own payload prop into the store, replacing the programmatic one. The lone trigger also receives aria-expanded="true", aria-controls, and data-popup-open for a popup it did not open, and would receive focus on close instead of the element that was focused before the open. With zero or two or more triggers the claim never runs, which is why only the single-trigger case misbehaves.

This is the same class of bug as #5031 (a trigger mounting after an imperative open), which went stale and was closed unreviewed after the store refactor in #5109.

Fix

The store could not distinguish "opened by a handle with no trigger" from "opened by a controlled open prop or defaultOpen", and only the latter should claim a lone trigger. This PR adds an openedWithoutTrigger flag to the shared popup state:

  • createPopupOpenState records it as "open and no trigger passed". Every setOpen lands there, so it covers open(null) and openWithPayload(); controlled and default opens never call it and keep the previous behavior.
  • useImplicitActiveTrigger skips the single-trigger claim while the flag is set, and clears it on close so a controlled close followed by a controlled open can claim a lone trigger again.
  • The registration-time claim in useTriggerDataForwarding skips while the flag is set, which covers the late-mounting trigger case from [popups] Don't let a late trigger claim a programmatically-opened popup #5031.
  • The triggerPopupId selector no longer hands aria-controls to a lone trigger while the flag is set.

Every other open path (click, hover, focus, list navigation, drawer swipe) passes a trigger element. The Context Menu opens without one but registers no triggers, so it does not observe the flag.

Behavior note

DialogHandle.open(id) for a trigger that is not mounted yet already warns that the popup opens unassociated. Previously that trigger could still be claimed if it mounted later as the only trigger; it now stays unassociated, matching the warning. Recording the pending id so the trigger is claimed on mount (as #5031 did) is left as a possible follow-up.

Tests

  • New Dialog tests cover the issue's single-trigger scenario, that clicking the trigger afterwards still forwards its payload, and a trigger mounting while the popup is open. The first test failed before the fix with the exact wrong payload from the report.

  • New unit tests in popupStoreUtils.test.tsx cover the flag in createPopupOpenState, both claim paths, the controlled-close reset, and the triggerPopupId selector.

  • Existing suites for Dialog, Alert Dialog, Drawer, Menu, Context Menu, Menubar, Popover, Tooltip, and Preview Card pass in jsdom; the two changed suites also pass in Chromium.

  • I have followed (at least) the PR section of the contributing guide.

🤖 Generated with Claude Code

`DialogHandle.openWithPayload()` and `open(null)` open the popup without
an active trigger. When exactly one trigger was registered, the Root's
`useImplicitActiveTrigger` claimed it, which flipped the trigger's
`isMountedByTrigger` selector and made its data-forwarding effect
overwrite the programmatic payload with the trigger's own `payload`
prop. The trigger also received `aria-expanded`, `aria-controls`, and
`data-popup-open` for a popup it did not open.

Record an `openedWithoutTrigger` flag in the shared popup state when an
open request carries no trigger, and skip the implicit single-trigger
claim, the registration-time claim, and the lone-trigger
`aria-controls` association while it is set. Controlled and default
opens never pass through `createPopupOpenState`, so they keep claiming a
lone trigger; the Root clears the flag on close so a controlled close
followed by a controlled open behaves as before.

Closes mui#5632

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-09T06:22:28.897558Z 406163c New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

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

commit: 406163c

@code-infra-dashboard

code-infra-dashboard Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+228B(+0.05%) 🔺+54B(+0.04%)

Details of bundle changes

Performance

Total duration: 1,011.27 ms -60.28 ms(-5.6%) | Renders: 76 (+0) | Paint: 1,640.45 ms -88.97 ms(-5.1%)

Test Duration Renders
Checkbox mount (500 instances) 56.40 ms ▼-25.08 ms(-30.8%) 1 (+0)
Mixed surface mount (app-like density) 67.47 ms ▼-17.38 ms(-20.5%) 5 (+0)

13 tests within noise — details


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

@michaldudak michaldudak added component: dialog Changes related to the dialog component. component: alert dialog Changes related to the alert dialog component. component: drawer Changes related to the drawer component. type: bug It doesn't behave as expected. labels Sep 7, 2026
@netlify

netlify Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 406163c
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6aa0fa45469e4c0008ea62df
😎 Deploy Preview https://deploy-preview-5641--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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1d03253f04

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// An open request without a trigger (a handle's `open(null)` or `openWithPayload()`) must not
// be reassociated with a lone registered trigger later on. Controlled and default opens never
// pass through here, so they keep claiming a lone trigger.
openedWithoutTrigger: open && trigger == null,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear rejected triggerless requests before controlled opens

In a controlled Dialog with open={false}, calling handle.openWithPayload() or handle.open(null) sets this flag even when the consumer rejects the request by leaving open false. Because the selected open, active trigger, and active element can all remain unchanged, useImplicitActiveTrigger is not rerendered and its closed-state cleanup never runs. If the consumer later opens the Dialog by changing the controlled prop, the stale flag prevents the lone trigger from being associated, contrary to the intended controlled-open behavior. The flag must be cleared after an unaccepted controlled request or otherwise tied to an open transition that actually occurs.

Useful? React with 👍 / 👎.

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.

The mechanics here are right: if a controlled parent leaves open false after openWithPayload() or open(null), the flag is set, nothing re-renders the Root, and the close-time reset does not run. A later controlled open for an unrelated reason then skips the lone-trigger claim. I verified that in a test.

I am keeping the behavior as is, for two reasons.

  1. The suggested remedy reintroduces the bug in a case that matters more. The same parent can accept the request asynchronously (after a confirmation or a fetch) and then set open true. From the store's point of view a rejected request and a pending one are identical until the parent decides, so clearing the flag on a "rejected" request would also clear it for the pending one, and the lone trigger would then claim the accepted open and overwrite the programmatic payload, which is exactly what [popups] openWithPayload() payload is overridden by the trigger's payload when only one trigger is rendered #5632 reports. With the current code the payload survives an asynchronously accepted open (also verified).

  2. It matches existing precedent. A rejected trigger click on a controlled dialog already records that trigger as the active one, and a later unrelated controlled open then shows aria-expanded="true" on it and forwards its payload (verified as well). The store already treats the most recent request as the association for the next controlled open, accepted or not. The flag follows the same rule for trigger-less requests, and like the trigger association it clears on the next close.

So the edge is real but narrow, consistent with how trigger associations already persist, and self-healing on close.

@jjenzz jjenzz Sep 7, 2026 •

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.

I asked Codex to review and it pointed out this same issue, so I gave it your reply and it said:

That makes sense for a request that may still be accepted asynchronously. There’s one narrower case I reproduced on the latest commit:

  1. Call handle.openWithPayload('programmatic') while keeping open={false}.
  2. Call handle.close() while it is still closed.
  3. Later set the controlled open prop to true.

The flag survives the explicit close, so the later open still uses the programmatic payload and skips the lone-trigger association.

Could we clear the flag when a close request is not canceled and the dialog is already effectively closed? That would let consumers abandon a pending request while preserving the association for deferred opens and for dialogs that decline a close and remain open.

is that a problem? doesn't sound like it to me, seems very unlikely someone would do that.

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.

It seems very unlikely to happen and self heals. It needs a controlled parent that ignores a trigger-less open, then a close call on a dialog that is already closed, then an unrelated controlled open. The flag resets on the next effective close, so it cannot persist beyond one open.

I would leave it as is, not to introduce additional complexity

`createPopupOpenState` cleared `openedWithoutTrigger` on every close
request, but `open` there is the requested state. A controlled root can
decline the request and keep `open={true}`, after which a trigger that
mounted (or the lone registered trigger) could claim the still-open
popup and replace the programmatic payload.

Keep the flag through close requests and rely on the Root's existing
reset, which runs once the effective open state is false. Add a
controlled-dialog regression test for the declined-close sequence.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@michaldudak
michaldudak merged commit d1a6f41 into mui:master Sep 9, 2026
27 of 28 checks passed
@michaldudak
michaldudak deleted the claude/base-ui-issue-5632-562e2c branch September 9, 2026 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: alert dialog Changes related to the alert dialog component. component: dialog Changes related to the dialog component. component: drawer Changes related to the drawer component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[popups] openWithPayload() payload is overridden by the trigger's payload when only one trigger is rendered

2 participants