Repository navigation
fix(useMenuPress): hit-test in the menu's own shadow root - #7914
Conversation
Inside a shadow root document.elementFromPoint answers with the shadow host, so a released pointer over a Selector, DropdownMenu or ContextMenu row resolved to a point outside the menu: the click closed the menu and selected nothing (module:DropdownMenu/useMenuPress FR1). The hit test now runs on the menu's root node when that is a shadow root, and on the document otherwise.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewModified ComponentsContextMenu (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Visual Regression46 of 490 shot(s) changed. A change here is a question, not a failure: check whether the after is the
and 26 more. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
There was a problem hiding this comment.
Thanks for tracking this down: hit-testing in the menu's own shadow root restores mouse picks in Selector and DropdownMenu inside a shadow root, but a few things need to change before this can land.
ContextMenuinside a shadow root still closes without running the clicked item, because its own documentmousedownoutside-click handler inContextMenu.tsxcomparesmenu.contains(e.target)against the retargeted shadow host; checkinge.composedPath().includes(menu)fixes it, or the changeset andFixes #7908should stop claimingContextMenu.- The new story fails the Storybook typecheck (TS2322 at
MenuPressShadowRootA11y.stories.tsx:35, a nullablevaluewithouthasClear), which turns thetypecheckandbuildchecks red;useState<string>()withonChange(next)compiles. - The new
useMenuPress.test.tsxcase should restoredocument.elementFromPointin afinally, so a regression fails one test instead of fourteen.
[Automated review]
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [fix] Selector, DropdownMenu and ContextMenu: clicking an option inside a shadow root selects it. The menu used to hit-test through the document, which answers with the shadow host, so the click closed the menu without selecting anything. |
There was a problem hiding this comment.
ContextMenu is not fixed by this change. Its outside-click effect in packages/core/src/ContextMenu/ContextMenu.tsx (lines 465-479) closes the menu on any document mousedown where !menu.contains(e.target), and inside a shadow root that target is the shadow host, so the menu closes on mouse-down before useMenuPress sees the release. Checking the composed path makes the click act:
if (menu && !e.composedPath().includes(menu)) {
closeMenu();
}Otherwise, drop ContextMenu from this note and avoid closing #7908, which names it.
| const [value, setValue] = useState<string | null>(null); | ||
| return ( | ||
| <Selector |
There was a problem hiding this comment.
This fails the Storybook typecheck (TS2322 at line 35): without hasClear, Selector takes value?: string and onChange?: (value: string) => void, so a string | null state does not fit, and CI's typecheck and build checks fail. This compiles:
const [value, setValue] = useState<string>();
// ...
onChange={next => {
setValue(next);
onChange(next);
}}| Object.assign(document, {elementFromPoint: () => host}); | ||
| Object.assign(shadow, {elementFromPoint: () => inShadow('C')}); | ||
| const events = {...mouse(), composed: true, bubbles: true}; | ||
| fireEvent.pointerDown(inShadow('A'), events); | ||
| fireEvent.pointerUp(inShadow('A'), events); | ||
| expect(onSelect).toHaveBeenCalledWith('C'); | ||
| expect(onSelect).toHaveBeenCalledTimes(1); | ||
| delete (document as {elementFromPoint?: unknown}).elementFromPoint; | ||
| host.remove(); |
There was a problem hiding this comment.
The stub on document is removed only when both assertions pass. Against the base hook this file then reports 14 failures, 13 of them later cases that inherit the stub; with the cleanup in finally it reports only this one:
Object.assign(document, {elementFromPoint: () => host});
Object.assign(shadow, {elementFromPoint: () => inShadow('C')});
try {
// pointerDown, pointerUp and the two expectations
} finally {
delete (document as {elementFromPoint?: unknown}).elementFromPoint;
host.remove();
}…lose it ContextMenu's outside-press check read the event target, which the document sees as the shadow host, so it closed the menu on mouse-down before the release could act. It now checks the composed path. Also fixes the shadow-root story's Selector types and makes the unit stub clean up on failure.
There was a problem hiding this comment.
Thanks for the follow-up: hit-testing in the menu's own root and the composed-path check restore mouse picks in Selector, DropdownMenu and ContextMenu inside an open shadow root, and in Selector and DropdownMenu inside a closed one, but one gap remains before this can land. Inside a closed shadow root ContextMenu still closes on mouse-down without running the clicked item, because e.composedPath() read by a listener on document leaves out every node of a closed tree (ContextMenu.tsx:473), while the changeset says a click inside a shadow root now selects in all three menus. Judging the press in the menu's own tree (for example, also listening on menu.getRootNode() when it is a ShadowRoot and skipping document presses whose composed path includes its host) covers both modes; otherwise, narrow the changeset's ContextMenu claim to open shadow roots.
[Automated review]
| if (menu && !menu.contains(e.target as Node)) { | ||
| // The composed path, not the target: inside a shadow root the document | ||
| // sees the shadow host as the target of a press on the menu. | ||
| if (menu && !e.composedPath().includes(menu)) { |
There was a problem hiding this comment.
Inside a closed shadow root this still closes the menu on mouse-down: composedPath() read by a listener on document leaves out every node of a closed tree, so it never includes menu, and the clicked item never runs. Selector and DropdownMenu work in the same closed root through getRootNode(). Judging the press in the menu's own tree covers both modes, for example:
const menuRoot = listRef.current?.getRootNode();
const shadowRoot = menuRoot instanceof ShadowRoot ? menuRoot : null;
const handleClickOutside = (e: Event) => {
const menu = listRef.current;
if (menu == null) {
return;
}
// A press inside the menu's shadow root is judged by the root's own
// listener, which sees the real path.
if (
shadowRoot != null &&
e.currentTarget === document &&
e.composedPath().includes(shadowRoot.host)
) {
return;
}
if (!e.composedPath().includes(menu)) {
closeMenu();
}
};
document.addEventListener('mousedown', handleClickOutside);
shadowRoot?.addEventListener('mousedown', handleClickOutside);with the matching removals in cleanup; otherwise the changeset's ContextMenu claim should say open shadow roots.
… root A document listener's composed path leaves out a closed shadow tree, so a press on a ContextMenu inside one still closed it. Presses in the menu's own shadow tree are now judged by a listener on that root; the document listener ignores presses retargeted to its shadow host. A press elsewhere in the same tree still closes the menu. Tests cover open and closed roots.
There was a problem hiding this comment.
Thanks for the update: hit-testing in the menu's own root now fixes mouse picks in Selector and DropdownMenu inside a shadow root, and the new root-level listener fixes ContextMenu in a single open or closed shadow root. Two ContextMenu cases still need a change before this can land, both from the host guard at ContextMenu.tsx:476:
- A mouse press on the shadow host's own box (its padding or border) no longer closes an open
ContextMenu, which it did before this change. - In a shadow root nested inside another, a press on a row still closes the menu without running it, although the changeset says clicks inside any shadow root now work.
Having the root's listener mark the event it judged, and the document listener skip that same event instead of comparing the target with the host, fixes both; a test for each would keep them fixed, and the PR description still describes the earlier composedPath() check.
[Automated review]
| const shadow = tree instanceof ShadowRoot ? tree : null; | ||
| const handleClickOutside = (e: Event) => { | ||
| const menu = listRef.current; | ||
| if (e.currentTarget === document && e.target === shadow?.host) { |
There was a problem hiding this comment.
This guard skips every document press whose target is the menu root's host, which misses two cases:
- A press on the host's own box (its padding or border) never passes through the shadow root, so neither listener closes the menu; before this change it closed.
- In a shadow root nested inside another, the document sees the outermost host, not
shadow.host, so a press on a row still closes the menu on mouse-down and the row never acts.
Judging each press once covers both: let the root's listener mark the event, and have the document listener skip that same event.
let judgedInTree: Event | null = null;
const judge = (e: Event) => {
const menu = listRef.current;
if (menu && !menu.contains(e.target as Node)) {
closeMenu();
}
};
const handleTreePress = (e: Event) => {
judgedInTree = e;
judge(e);
};
const handleClickOutside = (e: Event) => {
if (e === judgedInTree) {
return;
}
judge(e);
};
document.addEventListener('mousedown', handleClickOutside);
shadow?.addEventListener('mousedown', handleTreePress);with the matching removals in cleanup.
| expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(['open', 'closed'] as const)( |
There was a problem hiding this comment.
These cases cover a press on a row only. A case that presses the shadow host itself (fireEvent.mouseDown(host), expecting hidePopover to be called) and one that renders the menu in a shadow root nested inside another (expecting a row press to keep it open) would cover the two paths the host guard gets wrong.
The shadow-host guard kept a press on the host itself from closing the menu, and a menu in a shadow root nested in another still closed on mouse-down. The menu's own shadow root now judges presses inside its tree and marks them; the document listener judges only presses no root has judged. Tests cover open, closed and nested roots and a press on the host.
There was a problem hiding this comment.
Thanks for the fix: hit-testing in the menu's own shadow root, and judging each ContextMenu press once from the innermost tree, make mouse clicks and finger taps act in Selector, DropdownMenu and ContextMenu inside open, closed and nested shadow roots, while a press outside the menu, including on the shadow host, still closes ContextMenu. The points raised on earlier revisions are all addressed, the new tests fail without the fix and pass with it, and this looks ready to merge. One optional note: the description mentions only mouse users, but finger taps on menu rows inside a shadow root were broken the same way and are fixed by this change too.
[Automated review]
… it (#7308) ## User impact On a phone, a `ContextMenu` a long press opens closes at once: as the finger lifts, Chromium sends the held element a compatibility `mousedown`, read as a press outside. ## Problem and solution fit - Why it occurs: the menu closes on any document `mousedown` outside it, and a touch's compatibility events target the held trigger. - Why it harms: no touch path. - Why it matters: iOS opens one only by a long press. ## Expected behavior and authority `spec:AST-094` (current) FR33: with a finger or pen, "the long press that opens a ContextMenu MUST NOT close it at the lift". `component:ContextMenu` FR5 routes the long-press lift there, and `architecture:interaction-modality` (current) INV4 asks for an operable path per modality. This PR implements that clause only; FR33's outside press (FR32), lift and handoff stay with their own work, and it changes no record row. #7303 proposes FR10 (focus on open); #7840, FR12. Overlap search over open PRs and records (`component:ContextMenu`, `layer-runtime.md`; "outside press", "long press", "compatibility"): #7303 and #7840 touch the same listener. ## Smallest restoration `main`'s outside `mousedown` listener stays, shadow-root handling included. It skips the compatibility mouse events of the finger press that opened the menu: the long press, or a touch `contextmenu` where Chromium sends one, recorded by the shared gesture counter (`Layer/gestureCounter`), which advances on every `pointerdown` and `keydown`. Any later press is a new gesture, so every other outside press keeps its outcome: a mouse or pen closes at the press, a tap on its compatibility `mousedown`, while a scroll, a cancelled or stopped `pointerdown`, an unpanned drag or a second finger leaves it open. A mouse or keyboard open records no gesture. No listener, API or default is added; `architecture:layer-runtime` names the skip. ## Evidence - Before, in real Chromium (CDP touch): the menu opens at about 510 ms; on lift come `pointerup`, `touchend`, then trusted `mousedown`, `mouseup` and `click` on the trigger, and it closes. - After: the menu stays open through that `click`, in a shadow root too; a tap or mouse click outside still closes it. - iOS Safari reportedly sends the `mousedown` mid-hold; the same gesture is skipped, but WebKit was not run here. - Regression proof: the long-press, touch `contextmenu` and moved-menu unit cases and both Chromium long-press cases fail on `main`; the mouse, pen and no-`mousedown` cases fail on the previous head and pass on `main`. ## Writing rubric results R1 narration: the layer-runtime line is present tense. R2 research: none. R3 evidence: none in the record; the tests below verify the line. R4 rejected alternatives: none. R5 change references: none. R6 open questions: none. ## Scope - [x] One defect is restored. - [x] Separable contradictory or unsettled tagalongs were removed or split. - [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label. - [x] Public text and artifacts contain no internal Meta context. ## Testing - Maintainer merge 690291d brought in `main`: `ContextMenu.spec.md` takes `main`'s table, whose FR5 already routes the long-press lift to `spec:AST-094` FR33, so this PR no longer proposes FR11; the changeset names only the Chromium lift the tests show. The code, tests and `layer-runtime.md` line are unchanged. - Review round (163af16, rebased onto `main` 8b74911): narrowed to the long-press lift per the owner's requested change. `main`'s outside `mousedown` listener returns with one skip, the opening finger press's compatibility events (shared gesture counter); the pointer listeners, the nested-root levels and the five other outcome changes are gone. FR11, `layer-runtime.md` and the changeset say only that. ContextMenu, DropdownMenu, hooks, Layer and CLI suites: 1422 tests; long-press, shadow-root and ContextMenu audit Chromium specs: 23; native types, typecheck, `eslint` (strict), `check:knowledge` and the Storybook build pass. - Rebased onto `main` 39b5981 (486cec8), patch unchanged: the `typecheck` job's native platform types step runs a script `main` added in #8024, which 1fe5329's older base lacked. ContextMenu, DropdownMenu, hooks and CLI suites: 1216 tests; the long-press and shadow-root Chromium specs: 5; the native typecheck, build and platform-types steps, typecheck, `eslint` (strict), `check:knowledge` and the Storybook build pass. - Review round (1fe5329): the document `pointerup`/`pointercancel` listeners come on with an outside finger or pen press and off once none is live, instead of staying for the whole open menu (`architecture:react-component-runtime` INV5); `layer-runtime.md` says so. A new case shows none attached while the menu is open at rest and none after the lift; it fails on the previous head. ContextMenu, DropdownMenu, hooks and CLI suites: 1151 tests; the long-press and shadow-root Chromium specs: 5; typecheck, `eslint` (strict), `check:knowledge` and the Storybook build pass. ``` Rebased onto main 9f3ab05 (a5784a5, one commit): #7914 judged presses inside a shadow root with a `mousedown` listener on the menu's root, run before the document's. Capture reaches the document first, so each root from the document down to the menu's own now listens in the capture phase and defers a press on the host of the next root down; a press on such a host itself is judged by that host, whatever the host's own listeners do. #7914's open, closed and nested cases fire pointer events and also lift a finger on the menu; a new case stops presses in each root and on a host. They fail with the document judging every press (#7914's cases), with roots listening as the press bubbles, and with only the menu's own root listening (the new case). The `a11y/Menu press in a shadow root` stories gain a ContextMenu; the shadow-root spec checks in Chromium that a clicked item is picked (red with the document judging every press), that a press or a tap on the host closes the menu, and that its opening long press does not. `layer-runtime.md` names the extra listeners. vitest ContextMenu, DropdownMenu, Selector, hooks, Breadcrumbs Tests 1193 passed playwright MenuPressShadowRoot, ContextMenuLongPress, ContextMenuAudit --project=chromium 23 passed typecheck, lint:strict, check:knowledge, storybook:build ✓ Review round 3: rebased onto main 95e1674 as one commit; the rule is FR11, since #7303 proposes FR10 in the same record: vitest ContextMenu, DropdownMenu, useLongPress, docProp* Tests 376 passed playwright ContextMenu/__tests__ --project=chromium 23 passed typecheck, lint:strict, check:knowledge, storybook:build ✓ Review round 2: the listeners run in the capture phase, so a control that stops the press (ResizeHandle, Table's column-resize handle) still closes the menu; new case "closes on a press whose control stops it" fails with bubble-phase listeners: vitest ContextMenu, DropdownMenu, useLongPress, docProp* Tests 374 passed (DropdownMenuLazySheet's import hook timed out at 10 s under load; 2 passed alone) playwright ContextMenu/__tests__ --project=chromium 23 passed typecheck, lint:strict, check:knowledge, storybook:build ✓ Review round: the three changed presses are named (changeset, body, commit message), a new case pins the cancelled press, the unpanned drag and a second finger's tap (it fails on `main`), and docs/architecture/layer-runtime.md names the new listeners: vitest ContextMenu, DropdownMenu, useLongPress, docProp* Tests 375 passed playwright ContextMenu/__tests__ --project=chromium 23 passed typecheck, lint:strict, check:knowledge, storybook:build ✓ Rebased onto main 22c592a as one commit. Main's record now has FR9 (overlay dismissal), so this rule was proposed FR10 (now FR11, beside #7303's FR10): vitest ContextMenu, DropdownMenu, hooks, docProp* Tests 854 passed playwright ContextMenu/__tests__ --project=chromium 23 passed typecheck, lint:strict, check:knowledge, storybook:build ✓ red on main: `ContextMenuLongPress.a11y.chromium.spec.ts` against a `main` build: 1 failed (the long press and lift), 1 passed (the guard) pnpm exec vitest run packages/core/src/ContextMenu/ContextMenu.test.tsx -t "ContextMenu outside dismissal" main's ContextMenu.tsx: 3 failed, 3 passed (tap/scroll, mouse and mid-tap re-render guards) · with the change: 6 passed (the pen case fails on main, where a pen closes it at once; the re-render case failed on an earlier head) pnpm exec vitest run packages/core/src/ContextMenu packages/core/src/DropdownMenu packages/core/src/hooks/useLongPress.test.tsx \ packages/core/src/hooks/useMenuPress.test.tsx packages/core/src/docPropLiterals.test.ts \ packages/core/src/docPropReferences.test.ts --maxWorkers=8 Test Files 9 passed (9) Tests 324 passed (324) pnpm storybook:build (this branch; main for the red arm) ASTRYX_STORYBOOK_DIR=<main build> pnpm exec playwright test \ packages/core/src/ContextMenu/__tests__/ContextMenuLongPress.a11y.chromium.spec.ts --project=chromium 1 failed, 1 passed (long press: the menu is gone after the lift's click; mouse guard passes) pnpm exec playwright test packages/core/src/ContextMenu/__tests__/ContextMenuLongPress.a11y.chromium.spec.ts --project=chromium 2 passed · --repeat-each=2: 4 passed pnpm -F @astryxdesign/core typecheck, pnpm lint:strict, check:knowledge ✓ ```
User impact
When an app renders Astryx inside a shadow root (for example a notebook or a widget host that isolates each widget that way), clicking an option in a
Selector,DropdownMenuorContextMenucloses the list without selecting it. Mouse users cannot pick an option; keyboard selection still works.Problem and solution fit
useMenuPressfinds the element under the released pointer withdocument.elementFromPoint. For a point inside a shadow root, the document answers with the shadow host, not the option. The release then looks like it landed outside the menu, so the menu dismisses instead of activating the row.The hit test now runs on the menu's own root node when that is a shadow root (
ShadowRootimplementselementFromPoint). Outside a shadow rootgetRootNode()is the document, so behavior there is unchanged.Expected behavior and authority
module:DropdownMenu/useMenuPressFR1 (current): "A row MUST act when a press is released over it." The claim has no host exception, and inside a shadow root the row under the release never acts.Smallest restoration
useMenuPress.ts:resolvePointcallselementFromPointon the menu's root node when it is a shadow root, and on the document otherwise. ThehitTestseam and the jsdom fallback are unchanged; the seam's doc comment states the new default.ContextMenu.tsx: its own outside-press check (a documentmousedownlistener) reade.target, which the document sees as an outer shadow host. It closed the menu on mouse-down before the release could act.Evidence
Selectorrendered in an open shadow root (the newa11y/Menu press in a shadow rootstory): open it and click "Banana" with the mouse. The list closes and the value stays empty (3/3 runs).useMenuPresssuites pass unchanged (18 files, 646 tests). Outside a shadow root the root node is the document.main:MenuPressShadowRoot.a11y.chromium.spec.ts(real Chromium, real mouse), run in thepr-a11yjob.useMenuPress.test.tsxcase where the document answers with the shadow host and only the shadow root knows the row.ContextMenu.test.tsxcases for an open, a closed and a nested shadow root: a press on a menu item does not close the menu, and a press on the host does.Scope
Testing
pnpm exec vitest run --project ui packages/core/src/hooks/useMenuPress.test.tsx packages/core/src/Selector packages/core/src/DropdownMenu packages/core/src/ContextMenu: 646 tests pass. The new case fails againstmain'suseMenuPress.ts.main'suseMenuPress.ts: passes 3/3 here, fails 3/3 onmain.pnpm -F @astryxdesign/core typecheck, strict ESLint, Prettier,pnpm check:changesets: pass.Fixes #7908