Skip to content

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

TypeScript

fix(Overlay, DropdownMenuGroup, MobileNavToggle): forward host attributes to the element each owns (#8755)

Kenton Quatman249501fb06cc
diff --git a/.changeset/overlay-dropdown-menu-group-host-props.md b/.changeset/overlay-dropdown-menu-group-host-props.md
new file mode 100644
index 0000000000..b88802b506
--- /dev/null
+++ b/.changeset/overlay-dropdown-menu-group-host-props.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Overlay`, `DropdownMenuGroup` and `MobileNavToggle` now forward host attributes and DOM handlers (`id`, `data-*`, `aria-*`, event handlers) to the element each one owns, as other DOM-owning components do. Before, `Overlay` and `DropdownMenuGroup` accepted only `xstyle`, `className`, `style` and `ref` and dropped everything else, so a test id or analytics attribute on either never reached the page; `MobileNavToggle` also forwarded `data-testid`, but no other `data-*`, `id`, `aria-*` or handler. Built-in behavior stays component-owned: `Overlay`'s touch toggle and `MobileNavToggle`'s drawer toggle run on every press, before a caller handler; `DropdownMenuGroup` keeps its `role="group"` and, while it has a `title`, keeps that title as its name; `MobileNavToggle` keeps its `aria-expanded` and `aria-controls`.
+
+@kentonquatman
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index b154b7cfb6..2b7ab6da0a 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -1779,6 +1779,49 @@ describe('DropdownMenuGroup (compound mode)', () => {
screen.getByRole('group', {name: 'T', hidden: true}),
);
});
+
+ it('forwards host attributes to the group while keeping its role and heading name', () => {
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuGroup
+ title="Version history"
+ id="history-group"
+ data-testid="history"
+ data-analytics-id="menu-history"
+ aria-describedby="history-hint"
+ aria-labelledby="elsewhere"
+ // @ts-expect-error role is component-owned
+ role="presentation">
+ <DropdownMenuItem label="Restore" onClick={() => {}} />
+ </DropdownMenuGroup>
+ </DropdownMenu>,
+ );
+ const group = screen.getByTestId('history');
+ expect(group).toHaveAttribute('data-analytics-id', 'menu-history');
+ expect(group).toHaveAttribute('id', 'history-group');
+ expect(group).toHaveAttribute('aria-describedby', 'history-hint');
+ expect(group).toHaveAttribute('role', 'group');
+ // The visible title keeps naming the group.
+ expect(
+ screen.getByRole('group', {name: 'Version history', hidden: true}),
+ ).toBe(group);
+ });
+
+ it('an untitled group takes a caller-supplied accessible name', () => {
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <span id="recent-label">Recent</span>
+ <DropdownMenuGroup aria-labelledby="recent-label" data-testid="recent">
+ <DropdownMenuItem label="Only" onClick={() => {}} />
+ </DropdownMenuGroup>
+ </DropdownMenu>,
+ );
+ const group = screen.getByTestId('recent');
+ expect(group).toHaveAttribute('aria-labelledby', 'recent-label');
+ expect(screen.getByRole('group', {name: 'Recent', hidden: true})).toBe(
+ group,
+ );
+ });
});
describe('DropdownMenu dividers', () => {
diff --git a/packages/core/src/DropdownMenu/DropdownMenuGroup.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenuGroup.doc.mjs
index 6fa69a405b..7b43cc1675 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuGroup.doc.mjs
+++ b/packages/core/src/DropdownMenu/DropdownMenuGroup.doc.mjs
@@ -13,7 +13,7 @@ export const docs = {
defaults: {title: 'Version history'},
},
description:
- 'A group of rows in a compound menu. A visible title names role="group" through aria-labelledby; an omitted title leaves the group unnamed. The heading is not a menuitem, so arrow keys and typeahead skip it. Keep title content non-interactive and place controls in DropdownMenu header or footer. The data-driven equivalent is the `{type: "section", title, items}` entry in `items`; both forms share the heading treatment and theme target.',
+ 'A group of rows in a compound menu. A visible title names role="group" through aria-labelledby; an omitted title leaves the group unnamed. The heading is not a menuitem, so arrow keys and typeahead skip it. Keep title content non-interactive and place controls in DropdownMenu header or footer. The data-driven equivalent is the `{type: "section", title, items}` entry in `items`; both forms share the heading treatment and theme target. Host attributes and DOM handlers (id, data-*, aria-*) reach the group element; its role, and its name while it has a title, stay component-owned.',
usage: {
description:
'Use DropdownMenuGroup to give related compound-menu rows one visible and programmatic name. Use an untitled group only when a separate name would not help people understand the rows.',
@@ -107,7 +107,7 @@ export const docsZh = {
displayName: 'Dropdown Menu Group',
isHiddenFromOverview: true,
description:
- '复合菜单中的一组菜单行。可见标题通过 aria-labelledby 为 role="group" 命名;省略标题时该组没有名称。标题不是 menuitem,因此方向键和预输入会跳过它。标题内容应保持非交互;控件应放在 DropdownMenu 的 header 或 footer 中。',
+ '复合菜单中的一组菜单行。可见标题通过 aria-labelledby 为 role="group" 命名;省略标题时该组没有名称。标题不是 menuitem,因此方向键和预输入会跳过它。标题内容应保持非交互;控件应放在 DropdownMenu 的 header 或 footer 中。宿主属性和 DOM 事件处理函数(id、data-*、aria-*)会传到分组元素上;其 role 以及有标题时的名称由组件控制。',
usage: {
description:
'使用 DropdownMenuGroup 为相关的复合菜单行提供同一个可见名称和无障碍名称。只有当名称能帮助理解这些菜单行时才使用分组。',
diff --git a/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx b/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
index 46cf9d04e8..f5c59392de 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
@@ -4,7 +4,8 @@
/**
* @file DropdownMenuGroup.tsx
- * @input Uses spacing/typography/color tokens, themeProps (heading target)
+ * @input Uses spacing/typography/color tokens, themeProps (heading target);
+ * forwards host props to the group element
* @output Exports DropdownMenuGroup component and DropdownMenuGroupProps
* @position Sub-component; used inside DropdownMenu
*
@@ -47,9 +48,9 @@ const styles = stylex.create({
},
});
-export interface DropdownMenuGroupProps extends Pick<
- BaseProps,
- 'xstyle' | 'className' | 'style'
+export interface DropdownMenuGroupProps extends Omit<
+ BaseProps<HTMLDivElement>,
+ 'role'
> {
/**
* Heading shown above the group's rows and used as the group's accessible
@@ -70,7 +71,9 @@ export interface DropdownMenuGroupProps extends Pick<
* `menuitem`, so arrow-key navigation and typeahead skip it. Keep heading
* content non-interactive; place controls in DropdownMenu's `header` or
* `footer`. The compound peer of
- * `{type: 'section', title, items}` in the `items` data API.
+ * `{type: 'section', title, items}` in the `items` data API. Host attributes
+ * (`id`, `data-*`, `aria-*`) and DOM handlers reach the group element; its
+ * role, and its name while it has a title, stay component-owned.
*
* @example
* ```
@@ -91,15 +94,18 @@ export function DropdownMenuGroup({
className,
style,
ref,
+ 'aria-labelledby': ariaLabelledBy,
+ ...rest
}: DropdownMenuGroupProps) {
const headingId = useId();
const hasTitle = title != null && title !== '' && title !== false;
return (
<div
+ {...rest}
ref={ref}
role="group"
- aria-labelledby={hasTitle ? headingId : undefined}
+ aria-labelledby={hasTitle ? headingId : ariaLabelledBy}
{...mergeProps(stylex.props(xstyle), className, style)}>
{hasTitle && (
<div
diff --git a/packages/core/src/MobileNav/MobileNav.doc.mjs b/packages/core/src/MobileNav/MobileNav.doc.mjs
index 2652044197..b60f705494 100644
--- a/packages/core/src/MobileNav/MobileNav.doc.mjs
+++ b/packages/core/src/MobileNav/MobileNav.doc.mjs
@@ -133,7 +133,7 @@ export const docs = {
name: 'MobileNavToggle',
displayName: 'Mobile Nav Toggle',
description:
- 'Hamburger button that opens/closes the mobile nav drawer. Reads open state from AppShell context automatically: does NOT accept isOpen or onOpenChange props. Renders nothing above the mobile breakpoint.',
+ 'Hamburger button that opens/closes the mobile nav drawer. Reads open state from AppShell context automatically: does NOT accept isOpen or onOpenChange props. Renders nothing above the mobile breakpoint. Host attributes and DOM handlers (id, data-*, aria-*) reach the button; its aria-expanded and aria-controls stay component-owned, and a caller onClick runs after the toggle.',
// The toggle renders null unless AppShell mobile context reports an
// enabled mobile viewport — the default context outside AppShell never
// does, so the Properties preview was an empty stage. appShellMobile
@@ -305,7 +305,7 @@ export const docsDense = {
{
name: 'MobileNavToggle',
description:
- 'Hamburger button that opens/closes mobile nav drawer. Reads open state from AppShell context automatically. Renders nothing above mobile breakpoint.',
+ 'Hamburger button that opens/closes mobile nav drawer. Reads open state from AppShell context automatically. Renders nothing above mobile breakpoint. Host attributes and handlers reach the button; aria-expanded/aria-controls stay owned.',
},
],
propDescriptions: {
diff --git a/packages/core/src/MobileNav/MobileNavToggle.test.tsx b/packages/core/src/MobileNav/MobileNavToggle.test.tsx
index fb1bf4825b..31d4c091c6 100644
--- a/packages/core/src/MobileNav/MobileNavToggle.test.tsx
+++ b/packages/core/src/MobileNav/MobileNavToggle.test.tsx
@@ -22,6 +22,7 @@ import {
import {render, screen, fireEvent} from '@testing-library/react';
import {AppShell} from '../AppShell/AppShell';
import {SideNav, SideNavItem, SideNavSection} from '../SideNav';
+import {MobileNavToggle, type MobileNavToggleProps} from './MobileNavToggle';
// jsdom doesn't implement showModal/close on <dialog>, so we mock them
beforeAll(() => {
@@ -114,3 +115,65 @@ describe('MobileNavToggle ARIA wiring', () => {
expect(target?.tagName).toBe('DIALOG');
});
});
+
+describe('MobileNavToggle host attributes', () => {
+ // architecture:public-component-api INV5 and INV7: host attributes and
+ // handlers reach the button; FR11's toggle and ARIA wiring stay owned.
+ function ShellWithToggle(props: MobileNavToggleProps) {
+ return (
+ <AppShell
+ sideNav={
+ <SideNav>
+ <SideNavSection title="Test" isHeaderHidden>
+ <SideNavItem label="Home" />
+ </SideNavSection>
+ </SideNav>
+ }
+ mobileNav={{breakpoint: 'md'}}>
+ <MobileNavToggle {...props} />
+ </AppShell>
+ );
+ }
+
+ it('forwards data, ARIA and id attributes to the button', () => {
+ render(
+ <ShellWithToggle
+ data-testid="page-menu"
+ data-analytics-id="open-nav"
+ id="page-menu-toggle"
+ aria-describedby="nav-hint"
+ />,
+ );
+ const toggle = screen.getByTestId('page-menu');
+ expect(toggle.tagName).toBe('BUTTON');
+ expect(toggle).toHaveAttribute('data-analytics-id', 'open-nav');
+ expect(toggle).toHaveAttribute('id', 'page-menu-toggle');
+ expect(toggle).toHaveAttribute('aria-describedby', 'nav-hint');
+ });
+
+ it('keeps its own aria-expanded and aria-controls over a caller value', () => {
+ render(
+ <ShellWithToggle
+ data-testid="page-menu"
+ aria-expanded="true"
+ aria-controls="elsewhere"
+ />,
+ );
+ const toggle = screen.getByTestId('page-menu');
+ expect(toggle).toHaveAttribute('aria-expanded', 'false');
+ const controls = toggle.getAttribute('aria-controls');
+ expect(controls).not.toBe('elsewhere');
+ expect(document.getElementById(controls!)?.tagName).toBe('DIALOG');
+ });
+
+ it('runs a caller onClick after the toggle, which a preventDefault does not cancel', () => {
+ const onClick = vi.fn((event: {preventDefault: () => void}) => {
+ event.preventDefault();
+ });
+ render(<ShellWithToggle data-testid="page-menu" onClick={onClick} />);
+ const toggle = screen.getByTestId('page-menu');
+ fireEvent.click(toggle);
+ expect(onClick).toHaveBeenCalledTimes(1);
+ expect(toggle).toHaveAttribute('aria-expanded', 'true');
+ });
+});
diff --git a/packages/core/src/MobileNav/MobileNavToggle.tsx b/packages/core/src/MobileNav/MobileNavToggle.tsx
index 2a567ca1fc..ff40d7cd6b 100644
--- a/packages/core/src/MobileNav/MobileNavToggle.tsx
+++ b/packages/core/src/MobileNav/MobileNavToggle.tsx
@@ -4,13 +4,17 @@
/**
* @file MobileNavToggle.tsx
- * @input Uses React, Button, Icon, AppShell mobile context
+ * @input Uses React, Button, Icon, AppShell mobile context; forwards host props
+ * to the Button
* @output Exports MobileNavToggle component
* @position Standalone toggle button; can be placed anywhere in the AppShell tree.
*
* Hamburger button that opens/closes the mobile nav drawer.
* Reads from AppShell mobile context — renders nothing above the mobile breakpoint.
* Can be placed anywhere in the component tree (TopNav, content area, custom toolbar, etc.).
+ * Host attributes and DOM handlers reach the button; its `aria-expanded` and
+ * `aria-controls` stay component-owned, and a caller `onClick` runs after the
+ * toggle.
*/
import React, {type ReactNode} from 'react';
@@ -19,10 +23,11 @@ import {Icon} from '../Icon';
import {useAppShellMobile} from '../AppShell/AppShellMobileContext';
import type {BaseProps} from '../BaseProps';
import {useTranslator} from '../i18n';
+import {composeEventHandlers} from '../utils';
-export interface MobileNavToggleProps extends Pick<
- BaseProps,
- 'xstyle' | 'className' | 'style'
+export interface MobileNavToggleProps extends Omit<
+ BaseProps<HTMLButtonElement>,
+ 'aria-expanded' | 'aria-controls'
> {
ref?: React.Ref<HTMLButtonElement>;
/**
@@ -66,6 +71,8 @@ export function MobileNavToggle({
xstyle,
className,
style,
+ onClick,
+ ...rest
}: MobileNavToggleProps) {
const t = useTranslator();
const label = labelFromProps ?? t('@astryx.mobileNav.toggle.open');
@@ -84,11 +91,12 @@ export function MobileNavToggle({
return (
<Button
+ {...rest}
ref={ref}
variant="ghost"
label={label}
icon={children ?? <Icon icon="menu" color="inherit" />}
- onClick={toggleMobileNav}
+ onClick={composeEventHandlers(toggleMobileNav, onClick)}
aria-expanded={isMobileNavOpen}
aria-controls={mobileNavId || undefined}
data-testid={testId ?? 'mobile-nav-toggle'}
diff --git a/packages/core/src/Overlay/Overlay.doc.mjs b/packages/core/src/Overlay/Overlay.doc.mjs
index f33e309294..1e37528a0a 100644
--- a/packages/core/src/Overlay/Overlay.doc.mjs
+++ b/packages/core/src/Overlay/Overlay.doc.mjs
@@ -12,7 +12,7 @@ export const docs = {
usage: {
description:
- 'Overlay layers action or supporting content over media, cards, video, or other bounded surfaces with an optional scrim and reveal behavior.',
+ 'Overlay layers action or supporting content over media, cards, video, or other bounded surfaces with an optional scrim and reveal behavior. Host attributes and DOM handlers (id, data-*, aria-*) reach the root element.',
bestPractices: [
{
guidance: true,
@@ -193,7 +193,7 @@ export const docsDense = {
description: 'layered content over media/card surfaces with scrim + hover/focus/controlled reveal',
usage: {
description:
- 'Overlay layers compact actions or supporting content over media, cards, or bounded surfaces with optional dark/light scrim and hover/focus/controlled reveal.',
+ 'Overlay layers compact actions or supporting content over media, cards, or bounded surfaces with optional dark/light scrim and hover/focus/controlled reveal. Host attributes and DOM handlers reach the root element.',
bestPractices: [
{guidance: true, description: 'Use for short contextual actions/labels tied to the underlying surface.'},
{guidance: true, description: 'Keep content compact and legible over the scrim.'},
diff --git a/packages/core/src/Overlay/Overlay.test.tsx b/packages/core/src/Overlay/Overlay.test.tsx
index ff5c51ecd1..a976bf9c73 100644
--- a/packages/core/src/Overlay/Overlay.test.tsx
+++ b/packages/core/src/Overlay/Overlay.test.tsx
@@ -10,8 +10,8 @@
* update tests to match new behavior
*/
-import {describe, it, expect, beforeEach, afterEach} from 'vitest';
-import {render, screen, renderHook} from '@testing-library/react';
+import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
+import {render, screen, renderHook, fireEvent} from '@testing-library/react';
import {createRef} from 'react';
import {Overlay} from './Overlay';
import {useOverlay} from './useOverlay';
@@ -31,7 +31,7 @@ function getScrim(container: HTMLElement): HTMLElement {
*/
function mockHoverNone(matches: boolean): () => void {
const original = window.matchMedia;
- window.matchMedia = ((query: string) => ({
+ window.matchMedia = (query: string) => ({
matches: query.includes('hover: none') ? matches : false,
media: query,
onchange: null,
@@ -40,7 +40,7 @@ function mockHoverNone(matches: boolean): () => void {
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
- }));
+ });
return () => {
window.matchMedia = original;
};
@@ -245,6 +245,74 @@ describe('Overlay', () => {
expect(root.style.opacity).toBe('0.5');
});
});
+
+ describe('host attributes', () => {
+ // architecture:public-component-api INV5: host attributes reach the root
+ // element that owns the ref, the theme target and the styling props.
+ it('forwards data, ARIA and id attributes to the root div', () => {
+ const ref = createRef<HTMLDivElement>();
+ render(
+ <Overlay
+ ref={ref}
+ content={<span>c</span>}
+ id="hero-overlay"
+ data-testid="hero"
+ data-analytics-id="hero-card"
+ aria-describedby="hero-caption">
+ <div>b</div>
+ </Overlay>,
+ );
+ const root = screen.getByTestId('hero');
+ expect(root).toBe(ref.current);
+ expect(root).toHaveAttribute('data-analytics-id', 'hero-card');
+ expect(root).toHaveAttribute('id', 'hero-overlay');
+ expect(root).toHaveAttribute('aria-describedby', 'hero-caption');
+ expect(root).toHaveClass('astryx-overlay');
+ });
+
+ describe('on a touch device', () => {
+ let restore: () => void;
+ beforeEach(() => {
+ restore = mockHoverNone(true);
+ });
+ afterEach(() => restore());
+
+ it('runs a caller onClick and still toggles the hover overlay on tap', () => {
+ const onClick = vi.fn((event: {preventDefault: () => void}) => {
+ event.preventDefault();
+ });
+ const {container} = render(
+ <Overlay
+ showOn="hover"
+ content={<span>c</span>}
+ data-testid="hero"
+ onClick={onClick}>
+ <div>b</div>
+ </Overlay>,
+ );
+ expect(getScrim(container).hasAttribute('inert')).toBe(true);
+ fireEvent.click(screen.getByTestId('hero'));
+ expect(onClick).toHaveBeenCalledTimes(1);
+ // A caller preventDefault() does not cancel the built-in toggle.
+ expect(getScrim(container).hasAttribute('inert')).toBe(false);
+ });
+
+ it('runs a caller onMouseUp alongside the built-in handler', () => {
+ const onMouseUp = vi.fn();
+ render(
+ <Overlay
+ showOn="hover"
+ content={<span>c</span>}
+ data-testid="hero"
+ onMouseUp={onMouseUp}>
+ <div>b</div>
+ </Overlay>,
+ );
+ fireEvent.mouseUp(screen.getByTestId('hero'));
+ expect(onMouseUp).toHaveBeenCalledTimes(1);
+ });
+ });
+ });
});
describe('useOverlay', () => {
diff --git a/packages/core/src/Overlay/Overlay.tsx b/packages/core/src/Overlay/Overlay.tsx
index 426f8070eb..81ea27ff78 100644
--- a/packages/core/src/Overlay/Overlay.tsx
+++ b/packages/core/src/Overlay/Overlay.tsx
@@ -4,7 +4,7 @@
/**
* @file Overlay.tsx
- * @input Uses React, useOverlay
+ * @input Uses React, useOverlay; forwards host props to the container
* @output Exports Overlay component
* @position Overlay system component; thin wrapper over useOverlay
*
@@ -17,7 +17,7 @@
import type {ReactNode, Ref} from 'react';
import * as stylex from '@stylexjs/stylex';
-import {mergeProps} from '../utils';
+import {composeEventHandlers, mergeProps} from '../utils';
import {useOverlay} from './useOverlay';
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
import {overlayScope, overlayContainerStyles} from './overlay.markers.stylex';
@@ -31,10 +31,7 @@ import type {
} from './OverlayScrim';
import {useMergedRefs} from '../hooks/useMergedRefs';
-export interface OverlayProps extends Pick<
- BaseProps<HTMLDivElement>,
- 'xstyle' | 'className' | 'style'
-> {
+export interface OverlayProps extends BaseProps<HTMLDivElement> {
/** Ref forwarded to the container element. */
ref?: Ref<HTMLDivElement>;
/** Base content (image, card, video, etc.). */
@@ -84,6 +81,9 @@ export function Overlay({
className,
style,
ref,
+ onClick: onClickProp,
+ onMouseUp: onMouseUpProp,
+ ...rest
}: OverlayProps) {
const overlay = useOverlay({
content,
@@ -114,6 +114,7 @@ export function Overlay({
return (
<div
+ {...rest}
ref={useMergedRefs(ref, overlay.containerRef)}
{...mergeProps(
themeProps('overlay'),
@@ -121,8 +122,17 @@ export function Overlay({
className,
style,
)}
- onClick={overlay.containerProps.onClick}
- onMouseUp={overlay.containerProps.onMouseUp}>
+ // The touch toggle runs before the caller's handler: Overlay promises no
+ // cancellation, so a caller preventDefault() cannot stop it
+ // (architecture:public-component-api INV7).
+ onClick={composeEventHandlers(
+ overlay.containerProps.onClick,
+ onClickProp,
+ )}
+ onMouseUp={composeEventHandlers(
+ overlay.containerProps.onMouseUp,
+ onMouseUpProp,
+ )}>
{children}
{overlay.element}
</div>