Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tooltip-escape-dismissal-stays-closed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astryxdesign/core': patch
---

[fix] Tooltip stays dismissed with Escape while the pointer rests on its trigger (#11895)
@ravindrasinghshah
46 changes: 46 additions & 0 deletions packages/core/src/Tooltip/useTooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,52 @@ describe('useTooltip — Escape dismissal', () => {
expect(tooltipElement().matches(':popover-open')).toBe(false);
});

it('stays closed on a fresh mouseenter while the pointer never left after Escape', () => {
const onShow = vi.fn();
render(<Harness delay={0} onShow={onShow} />);
fireEvent.mouseEnter(trigger());
tick(0);
expect(tooltipElement().matches(':popover-open')).toBe(true);

fireEvent.keyDown(document, {key: 'Escape'});
expect(tooltipElement().matches(':popover-open')).toBe(false);

// The pointer never moved: the surface closing hands the trigger a fresh
// mouseenter, and that enter must not reopen the tooltip.
fireEvent.mouseEnter(trigger());
tick(1000);
expect(onShow).toHaveBeenCalledTimes(1);
expect(tooltipElement().matches(':popover-open')).toBe(false);

// Leaving and entering again is a genuine re-entry: it shows.
fireEvent.mouseLeave(trigger());
fireEvent.mouseEnter(trigger());
tick(0);
expect(onShow).toHaveBeenCalledTimes(2);
expect(tooltipElement().matches(':popover-open')).toBe(true);
});

it('shows again after focus leaves and returns following an Escape dismissal', () => {
const onShow = vi.fn();
render(<Harness delay={0} onShow={onShow} />);
const matches = trigger().matches.bind(trigger());
vi.spyOn(trigger(), 'matches').mockImplementation(
selector => selector === ':focus-visible' || matches(selector),
);
act(() => trigger().focus());
expect(onShow).toHaveBeenCalledTimes(1);
expect(tooltipElement().matches(':popover-open')).toBe(true);

fireEvent.keyDown(document, {key: 'Escape'});
expect(tooltipElement().matches(':popover-open')).toBe(false);

// Focus leaves and comes back: a new entry, so the tooltip shows.
act(() => trigger().blur());
act(() => trigger().focus());
expect(onShow).toHaveBeenCalledTimes(2);
expect(tooltipElement().matches(':popover-open')).toBe(true);
});

it('ignores an Escape that carries the IME keyCode 229', () => {
const onHide = vi.fn();
render(<Harness delay={0} onHide={onHide} />);
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/Tooltip/useTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ export function useTooltip(options: TooltipOptions = {}): TooltipReturn {
disabledRendersRef.current += 1;
}

// Set by an Escape dismissal. While set, a mouseenter does not schedule a
// show: the pointer never left the trigger, so the enter is the surface
// closing, not a new hover. Pointer leave or focus leave clears it.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
const dismissedByEscapeRef = useRef(false);

// Clear all timeouts
const clearTimeouts = useCallback(() => {
if (showTimeoutRef.current) {
Expand Down Expand Up @@ -537,11 +542,18 @@ export function useTooltip(options: TooltipOptions = {}): TooltipReturn {
if (isTouchPointerRef.current) {
return;
}
// A tooltip dismissed with Escape stays closed while the pointer rests
// on its trigger.
if (dismissedByEscapeRef.current) {
return;
}
scheduleShow();
}, [isTouchPointerRef, scheduleShow]);

const handleMouseLeave = useCallback(
(event: Event) => {
// The pointer left the trigger, so a later enter is a new hover.
dismissedByEscapeRef.current = false;
// On touch the synthesized mouseleave arrives with the next tap elsewhere,
// which the outside-tap dismissal already handles — and handling it here
// too would close the tooltip behind the tap-open bookkeeping's back.
Expand Down Expand Up @@ -625,6 +637,8 @@ export function useTooltip(options: TooltipOptions = {}): TooltipReturn {
if (!focusGate.focusOut(e, focusTrigger)) {
return;
}
// Focus left the trigger, so a later focus is a new entry.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
dismissedByEscapeRef.current = false;
scheduleHide();
},
[focusGate, focusTrigger, scheduleHide],
Expand Down Expand Up @@ -960,6 +974,7 @@ export function useTooltip(options: TooltipOptions = {}): TooltipReturn {
}
},
onDismiss: () => {
dismissedByEscapeRef.current = true;
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
clearTimeouts();
clearTapOpen();
dropHold();
Expand Down