From 3a413fa2b8e42b15cf7f4a6d5a976746c09f9eeb Mon Sep 17 00:00:00 2001 From: Kenton Daniel Quatman Date: Tue, 27 Oct 2026 17:48:15 -0400 Subject: [PATCH 1/4] fix(Overlay, DropdownMenuGroup): forward host attributes to the root element Overlay and DropdownMenuGroup each own one stable DOM element but typed their props as a Pick of the three styling props, so data-*, id, aria-* and DOM handlers passed to them never reached the page. Both now extend BaseProps and spread the remaining props onto that element, as other DOM-owning components do (architecture:public-component-api INV5). Overlay runs its touch toggle before a caller onClick or onMouseUp, so a caller handler cannot cancel it. DropdownMenuGroup keeps role="group" and, while it has a title, keeps that title as its name; an untitled group takes a caller's aria-labelledby. --- .../overlay-dropdown-menu-group-host-props.md | 7 ++ .../src/DropdownMenu/DropdownMenu.test.tsx | 43 +++++++++++ .../DropdownMenu/DropdownMenuGroup.doc.mjs | 4 +- .../src/DropdownMenu/DropdownMenuGroup.tsx | 18 +++-- packages/core/src/Overlay/Overlay.doc.mjs | 4 +- packages/core/src/Overlay/Overlay.test.tsx | 76 ++++++++++++++++++- packages/core/src/Overlay/Overlay.tsx | 26 +++++-- 7 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 .changeset/overlay-dropdown-menu-group-host-props.md 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 0000000000000..a48ddd8815182 --- /dev/null +++ b/.changeset/overlay-dropdown-menu-group-host-props.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] `Overlay` and `DropdownMenuGroup` now forward host attributes and DOM handlers (`id`, `data-*`, `aria-*`, event handlers) to their root element, as other DOM-owning components do. Before, each accepted only `xstyle`, `className`, `style` and `ref` and dropped everything else, so a test id or analytics attribute passed to either never reached the page. `Overlay`'s touch toggle still runs on every tap, and a caller `onClick` or `onMouseUp` runs after it. `DropdownMenuGroup` keeps its `role="group"` and, while it has a `title`, keeps that title as its name. + +@kentonquatman diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx index b154b7cfb6107..2b7ab6da0a3dc 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( + + + {}} /> + + , + ); + 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( + + Recent + + {}} /> + + , + ); + 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 6fa69a405b018..7b43cc1675ba0 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 46cf9d04e882b..f5c59392de4f7 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, + '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 (
{hasTitle && (
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(); + render( + c} + id="hero-overlay" + data-testid="hero" + data-analytics-id="hero-card" + aria-describedby="hero-caption"> +
b
+
, + ); + 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( + c} + data-testid="hero" + onClick={onClick}> +
b
+
, + ); + 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( + c} + data-testid="hero" + onMouseUp={onMouseUp}> +
b
+
, + ); + 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 426f8070eb6a9..81ea27ff783d7 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, - 'xstyle' | 'className' | 'style' -> { +export interface OverlayProps extends BaseProps { /** Ref forwarded to the container element. */ ref?: Ref; /** 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 (
+ // 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}
From 82b5b87b5232a9c457cc70edd62ffcd129ced15b Mon Sep 17 00:00:00 2001 From: Kenton Daniel Quatman Date: Tue, 27 Oct 2026 22:35:20 -0400 Subject: [PATCH 2/4] fix(MobileNavToggle): forward host attributes to the button MobileNavToggle owns one button but typed its props as a Pick of the three styling props plus data-testid, so other data-*, id, aria-* and DOM handlers never reached the page. It now extends BaseProps for the button, minus the aria-expanded and aria-controls it owns, and spreads the rest onto the Button before its own props (architecture:public-component-api INV5). The drawer toggle runs before a caller onClick, so a caller cannot cancel it (INV7). The changeset now covers all three components. --- .../overlay-dropdown-menu-group-host-props.md | 2 +- packages/core/src/MobileNav/MobileNav.doc.mjs | 4 +- .../src/MobileNav/MobileNavToggle.test.tsx | 63 +++++++++++++++++++ .../core/src/MobileNav/MobileNavToggle.tsx | 18 ++++-- 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/.changeset/overlay-dropdown-menu-group-host-props.md b/.changeset/overlay-dropdown-menu-group-host-props.md index a48ddd8815182..4313b9976f292 100644 --- a/.changeset/overlay-dropdown-menu-group-host-props.md +++ b/.changeset/overlay-dropdown-menu-group-host-props.md @@ -2,6 +2,6 @@ '@astryxdesign/core': patch --- -[fix] `Overlay` and `DropdownMenuGroup` now forward host attributes and DOM handlers (`id`, `data-*`, `aria-*`, event handlers) to their root element, as other DOM-owning components do. Before, each accepted only `xstyle`, `className`, `style` and `ref` and dropped everything else, so a test id or analytics attribute passed to either never reached the page. `Overlay`'s touch toggle still runs on every tap, and a caller `onClick` or `onMouseUp` runs after it. `DropdownMenuGroup` keeps its `role="group"` and, while it has a `title`, keeps that title as its name. +[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, each accepted only `xstyle`, `className`, `style` and `ref` (plus `data-testid` on `MobileNavToggle`) and dropped everything else, so a test id or analytics attribute passed to any of them never reached the page. 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/MobileNav/MobileNav.doc.mjs b/packages/core/src/MobileNav/MobileNav.doc.mjs index 26520441978e1..b60f705494546 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 fb1bf4825b600..31d4c091c6203 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 , 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 ( + + + + + + } + mobileNav={{breakpoint: 'md'}}> + + + ); + } + + it('forwards data, ARIA and id attributes to the button', () => { + render( + , + ); + 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( + , + ); + 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(); + 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 2a567ca1fcddf..ff40d7cd6b55c 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, + 'aria-expanded' | 'aria-controls' > { ref?: React.Ref; /** @@ -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 (