An open source design system that's fully customizable and agent ready
TypeScript
fix(Tooltip): drop a pending show when isEnabled turns false (#7612)
Cindy Zhangf8533a7a510d
diff --git a/.changeset/tooltip-disable-drops-pending-show.md b/.changeset/tooltip-disable-drops-pending-show.mdnew file mode 100644index 0000000000..d9ff57861c--- /dev/null+++ b/.changeset/tooltip-disable-drops-pending-show.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `useTooltip` (and every component built on it, such as `Button`'s `tooltip` prop) now drops a show that is still waiting out its delay when `isEnabled` turns `false`. Before, hovering a trigger and then disabling its tooltip within the delay let the timer fire anyway: the tooltip opened after its triggers were turned off, with no pointer-leave listener left to close it. A pending hide still runs, and an enabled tooltip is unchanged.++@cixzhangdiff --git a/packages/core/src/Tooltip/useTooltip.test.tsx b/packages/core/src/Tooltip/useTooltip.test.tsxindex 87a752f79f..70f79a93ad 100644--- a/packages/core/src/Tooltip/useTooltip.test.tsx+++ b/packages/core/src/Tooltip/useTooltip.test.tsx@@ -147,6 +147,19 @@ describe('useTooltip — show delay', () => {expect(onShow).not.toHaveBeenCalled();});++ it('drops a pending show when isEnabled turns false before the delay elapses', () => {+ const onShow = vi.fn();+ const {rerender} = render(<Harness onShow={onShow} />);++ fireEvent.mouseEnter(trigger());+ tick(100);+ rerender(<Harness isEnabled={false} onShow={onShow} />);+ tick(1000);++ expect(onShow).not.toHaveBeenCalled();+ expect(tooltipElement().matches(':popover-open')).toBe(false);+ });});describe('useTooltip — touch suppression', () => {diff --git a/packages/core/src/Tooltip/useTooltip.tsx b/packages/core/src/Tooltip/useTooltip.tsxindex b904ce2073..508a25899f 100644--- a/packages/core/src/Tooltip/useTooltip.tsx+++ b/packages/core/src/Tooltip/useTooltip.tsx@@ -352,6 +352,17 @@ export function useTooltip(options: TooltipOptions = {}): TooltipReturn {}, delay);}, [isEnabled, isOpen, clearTimeouts, showLayer, delay]);+ // A show scheduled while the triggers were enabled must not outlive them:+ // disabling also detaches the leave listener that would have cancelled it,+ // so the timer would open a tooltip that nothing ever closes. Only the+ // pending show is dropped; a pending hide still runs.+ useEffect(() => {+ if (!isEnabled && showTimeoutRef.current) {+ clearTimeout(showTimeoutRef.current);+ showTimeoutRef.current = null;+ }+ }, [isEnabled]);+// Schedule hide with delay (suppressed when isOpen is true).// A small hover bridge (when hideDelay is 0) lets the pointer travel from the// trigger onto the tooltip surface without the tooltip vanishing — required