Skip to content

Commit 65fa39d

Browse files
committed
fix(ContextMenu): a context request commits its open before the next input, inside a shadow root too
The request comes from a listener on the trigger, and inside a shadow root window.event is unset there (the DOM sets it only for listeners outside shadow trees), so React cannot tell the update is a discrete input: it gave the open default priority and committed it in a later task. The menu already showed, but its press-outside listener comes with that commit, and a press in between found none (MenuPressShadowRoot.a11y:76 failed 10 of 12). flushSync commits the open in the request's own task, as React commits a request it dispatches itself; the press-outside case now passes 5 of 5 and :76 12 of 12.
1 parent e6576c8 commit 65fa39d

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎packages/core/src/ContextMenu/ContextMenu.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import React, {
7474
Suspense,
7575
} from 'react';
7676
import type {ReactNode} from 'react';
77+
import {flushSync} from 'react-dom';
7778
import * as stylex from '@stylexjs/stylex';
7879
import {Button} from '../Button';
7980
import {Heading} from '../Heading';
@@ -1080,7 +1081,16 @@ export function ContextMenuView({
10801081
hold: touchModes ? TOUCH_HOLD : undefined,
10811082
onHold: phase => onHoldRef.current(phase),
10821083
},
1083-
forwarded => onForwardedRef.current(forwarded),
1084+
forwarded => {
1085+
// The open commits in the request's own task, before the next input,
1086+
// as React commits a request it dispatches itself. A request comes
1087+
// from a listener on the trigger, and inside a shadow root
1088+
// `window.event` is unset there, so React cannot tell it is a
1089+
// discrete input: it would commit the open in a later task, and a
1090+
// press outside in between would find no listener.
1091+
// eslint-disable-next-line @eslint-react/dom-no-flush-sync -- the open must commit before the next input, inside a shadow root too
1092+
flushSync(() => onForwardedRef.current(forwarded));
1093+
},
10841094
);
10851095
return () => requests.destroy();
10861096
}, [attachment, isDisabled, touchModes, triggerAs]);

0 commit comments

Comments
 (0)