Skip to content

Commit 49226e7

Browse files
morgmartCarlclaude
committed
fix(design-system): keep pressed triggers from reopening hints on focus return
When a menu opened from a hinted trigger closes, focus returns to the trigger. Base UI only suppresses that focus hint if the hint had already opened on hover, which no longer happens before a quick click now that hints wait for a resting pointer. The returned hint then swallowed the next Escape. A press now suppresses focus hints until the user tabs. Signed-off-by: morgmart <[email protected]> Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 45dfa47 commit 49226e7

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

‎src/shared/design-system/ui/Tooltip.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Tooltip as BaseTooltip } from "@base-ui/react/tooltip";
22
import {
33
cloneElement,
44
useId,
5+
useEffect,
6+
useRef,
57
useState,
68
type AriaAttributes,
79
type ReactElement,
@@ -49,6 +51,28 @@ export function Tooltip({
4951
const id = useId();
5052
const [trigger, setTrigger] = useState<HTMLElement | null>(null);
5153
const [localOpen, setLocalOpen] = useState(false);
54+
const pressed = useRef(false);
55+
// Focus returning to a pressed trigger, such as when its menu closes, is not
56+
// a request for the hint. Base UI blocks this only once the hint has opened,
57+
// which a resting hover may not have done yet. Tabbing ends the press.
58+
useEffect(() => {
59+
if (!trigger) return;
60+
const win = trigger.ownerDocument.defaultView ?? window;
61+
const release = (event: KeyboardEvent) => {
62+
if (event.key !== "Tab") return;
63+
pressed.current = false;
64+
win.removeEventListener("keydown", release, true);
65+
};
66+
const press = () => {
67+
pressed.current = true;
68+
win.addEventListener("keydown", release, true);
69+
};
70+
trigger.addEventListener("pointerdown", press);
71+
return () => {
72+
trigger.removeEventListener("pointerdown", press);
73+
win.removeEventListener("keydown", release, true);
74+
};
75+
}, [trigger]);
5276
const open = controlledOpen ?? localOpen;
5377
const expanded =
5478
children.props["aria-expanded"] === true ||
@@ -60,7 +84,11 @@ export function Tooltip({
6084
return (
6185
<BaseTooltip.Root
6286
open={open}
63-
onOpenChange={(next) => {
87+
onOpenChange={(next, details) => {
88+
if (next && details.reason === "trigger-focus" && pressed.current) {
89+
details.cancel();
90+
return;
91+
}
6492
setLocalOpen(next);
6593
onOpenChange?.(next);
6694
}}

0 commit comments

Comments
 (0)