An open source design system that's fully customizable and agent ready
TypeScript
fix(useMenuHover): a trigger disabled mid-hover stays closed (#6904)
Cindy Zhang5ee81e1329af
diff --git a/.changeset/menu-hover-disabled-midhover.md b/.changeset/menu-hover-disabled-midhover.mdnew file mode 100644index 0000000000..cc586497b7--- /dev/null+++ b/.changeset/menu-hover-disabled-midhover.md@@ -0,0 +1,12 @@+---+'@astryxdesign/core': patch+---++[fix] A nav or menu trigger disabled while a hover is in flight no longer opens its surface.++Hover intent schedules an open after a short delay. Disabling the trigger in+that window left the scheduled open to land anyway, on a surface whose+handlers were already inert — so it opened and nothing could dismiss it. The+pending intent is now abandoned when the integration is disabled.++@cixzhangdiff --git a/packages/core/src/hooks/useMenuHover.test.tsx b/packages/core/src/hooks/useMenuHover.test.tsxindex 1212c002ac..3ede3f452b 100644--- a/packages/core/src/hooks/useMenuHover.test.tsx+++ b/packages/core/src/hooks/useMenuHover.test.tsx@@ -10,9 +10,10 @@* SYNC: When useMenuHover changes, update tests to match new behavior*/import {describe, it, expect, vi, afterEach} from 'vitest';-import {render, screen, act} from '@testing-library/react';+import {render, screen, act, fireEvent} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {TopNavMenu} from '../TopNav/TopNavMenu';+import {useMenuHover} from './useMenuHover';const items = [{title: 'Analytics', description: 'Track user behavior', href: '/analytics'},@@ -362,3 +363,68 @@ describe('useMenuHover — native invoker wiring', () => {);});});++// =============================================================================+// Disabling a trigger with a hover already in flight+// =============================================================================++describe('useMenuHover — disabled during the hover delay', () => {+ afterEach(() => {+ vi.useRealTimers();+ vi.restoreAllMocks();+ });++ /**+ * The hook directly: a consumer that can flip `isEnabled` without also+ * changing what it renders. Through a real consumer the two move together —+ * `SideNavItem` only enables the flyout while the rail is collapsed — so+ * the disable cannot be observed apart from the re-render it causes.+ */+ function Harness({isEnabled, show}: {isEnabled: boolean; show: () => void}) {+ const {triggerProps} = useMenuHover<HTMLDivElement>({+ show,+ hide: () => {},+ isOpen: false,+ isEnabled,+ });+ return (+ <button type="button" {...triggerProps}>+ Products+ </button>+ );+ }++ it('does not open a surface the hover scheduled before it was disabled', () => {+ vi.spyOn(window, 'matchMedia').mockImplementation(+ (query: string) =>+ ({+ matches: query.includes('hover: hover'),+ media: query,+ onchange: null,+ addListener: () => {},+ removeListener: () => {},+ addEventListener: () => {},+ removeEventListener: () => {},+ dispatchEvent: () => false,+ }) as MediaQueryList,+ );+ vi.useFakeTimers();+ const show = vi.fn();+ const {rerender} = render(<Harness isEnabled show={show} />);++ fireEvent.mouseEnter(screen.getByRole('button', {name: 'Products'}));+ act(() => {+ vi.advanceTimersByTime(100);+ });+ expect(show).not.toHaveBeenCalled();++ // Disabled with the open still pending. Inert handlers alone do not+ // settle it: the scheduled open would land on a surface whose handlers+ // can no longer dismiss it.+ rerender(<Harness isEnabled={false} show={show} />);+ act(() => {+ vi.advanceTimersByTime(500);+ });+ expect(show).not.toHaveBeenCalled();+ });+});diff --git a/packages/core/src/hooks/useMenuHover.ts b/packages/core/src/hooks/useMenuHover.tsindex aa1560c9c5..2591e63f7c 100644--- a/packages/core/src/hooks/useMenuHover.ts+++ b/packages/core/src/hooks/useMenuHover.ts@@ -211,6 +211,16 @@ export function useMenuHover<T extends HTMLElement = HTMLElement>(return () => clearTimeouts();}, [clearTimeouts]);+ // A trigger disabled while a hover is in flight must stay closed. Returning+ // inert handlers below is not enough on its own: the open this hover already+ // scheduled would still fire, and it would open a surface whose handlers can+ // no longer close it.+ useEffect(() => {+ if (!isEnabled) {+ clearTimeouts();+ }+ }, [isEnabled, clearTimeouts]);+const focusMenu = useCallback(() => {// An empty or still-loading menu has no focusable item; focus the container// so keyboard ownership still transfers off the trigger's list.