Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(SideNav): keep a hover-opened collapsed heading menu open on press (#7190)

Cindy Zhange67fd1e57bdb
diff --git a/.changeset/side-nav-heading-hover-press.md b/.changeset/side-nav-heading-hover-press.md
new file mode 100644
index 0000000000..fd7b986e48
--- /dev/null
+++ b/.changeset/side-nav-heading-hover-press.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] In a collapsed SideNav, pressing a SideNavHeading menu trigger right after hovering it now keeps the hover-opened menu open, as the click guard intends. The browser was dismissing the menu on the press because the trigger sat outside the panel; the collapsed trigger is now the panel's native invoker, the same wiring TopNavMenu uses.
+
+@cixzhang
diff --git a/packages/core/src/SideNav/SideNav.test.tsx b/packages/core/src/SideNav/SideNav.test.tsx
index 165f6a4168..699576baeb 100644
--- a/packages/core/src/SideNav/SideNav.test.tsx
+++ b/packages/core/src/SideNav/SideNav.test.tsx
@@ -3040,6 +3040,38 @@ describe('SideNavHeading hover/click guard', () => {
expect(trigger).toHaveAttribute('aria-expanded', 'false');
expect(trigger).toHaveFocus();
});
+
+ // jsdom has no light dismiss, so the guard tests above cannot fail for the
+ // collapsed heading. In a browser, the collapsed icon button sits outside the
+ // auto popover, and only the invoker relationship stops the press that
+ // should confirm a hover-open from dismissing it first.
+ it('makes the collapsed trigger the native invoker of its flyout', () => {
+ render(
+ <CollapsedWrapper>
+ <SideNavHeading
+ heading="My App"
+ icon={<span>🏠</span>}
+ menu={menuItems}
+ />
+ </CollapsedWrapper>,
+ );
+ const trigger = screen.getByRole('button', {name: 'My App'});
+ const panelId = trigger.getAttribute('aria-controls');
+ expect(panelId).toBeTruthy();
+ expect(trigger).toHaveAttribute('popovertarget', panelId);
+ });
+
+ // Expanded, the panel covers the heading, and the trigger is the heading
+ // root itself: an invoker there would default-prevent clicks on its links.
+ it('adds no invoker in the expanded layouts', () => {
+ const {container} = render(
+ <>
+ <SideNavHeading heading="Whole" menu={menuItems} />
+ <SideNavHeading heading="Mixed" headingHref="/home" menu={menuItems} />
+ </>,
+ );
+ expect(container.querySelector('[popovertarget]')).toBeNull();
+ });
});
// =============================================================================
diff --git a/packages/core/src/SideNav/SideNavHeading.tsx b/packages/core/src/SideNav/SideNavHeading.tsx
index 69d337dfec..7e8a062e95 100644
--- a/packages/core/src/SideNav/SideNavHeading.tsx
+++ b/packages/core/src/SideNav/SideNavHeading.tsx
@@ -4,7 +4,8 @@
/**
* @file SideNavHeading.tsx
- * @input Uses React, useRef, useCallback, ReactNode, StyleX, usePopover
+ * @input Uses React, useRef, useCallback, ReactNode, StyleX, usePopover,
+ * useMenuHover
* @output Exports SideNavHeading component and SideNavHeadingProps
* @position Core implementation; used inside SideNav header slot
*
@@ -397,6 +398,12 @@ export function SideNavHeading({
isOpen: popover.isOpen,
isEnabled: !!menu,
showDelay: 0,
+ // Collapsed, the flyout opens beside the icon button, so a press on the
+ // button lands outside the auto popover and the browser would light-dismiss
+ // a hover-open before the click guard can confirm it. Expanded, the panel
+ // overlaps the heading, and the trigger is a <div> whose click must not be
+ // default-prevented: it holds the heading's own links.
+ popoverId: isCollapsed ? popover.id : undefined,
});
const closeMenuCtx = useMemo(() => ({closeMenu}), [closeMenu]);