Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/overlay-dropdown-menu-group-host-props.md
Original file line number Diff line number Diff line change
@@ -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, 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`.
Comment thread
kentonquatman marked this conversation as resolved.
Resolved

@kentonquatman
43 changes: 43 additions & 0 deletions packages/core/src/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/DropdownMenu/DropdownMenuGroup.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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 为相关的复合菜单行提供同一个可见名称和无障碍名称。只有当名称能帮助理解这些菜单行时才使用分组。',
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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
* ```
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/MobileNav/MobileNav.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional wording: a caller aria-label does not reach the button, because Button replaces it with label (component:Button ORD4). Saying the accessible name comes from label, beside aria-expanded and aria-controls, would keep this list exact; the dense line at 308 has the same gap.

// 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
Expand Down Expand Up @@ -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: {
Expand Down
63 changes: 63 additions & 0 deletions packages/core/src/MobileNav/MobileNavToggle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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');
});
});
18 changes: 13 additions & 5 deletions packages/core/src/MobileNav/MobileNavToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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>;
/**
Expand Down Expand Up @@ -66,6 +71,8 @@ export function MobileNavToggle({
xstyle,
className,
style,
onClick,
...rest
}: MobileNavToggleProps) {
const t = useTranslator();
const label = labelFromProps ?? t('@astryx.mobileNav.toggle.open');
Expand All @@ -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'}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Overlay/Overlay.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.'},
Expand Down
Loading
Loading