Skip to content

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

TypeScript

fix(core): close ContextMenu on Tab; require label on DropdownMenuRadioGroup (#4358)

Baraa Hamodia25411375d8f
diff --git a/.changeset/a11y-context-menu-tab.md b/.changeset/a11y-context-menu-tab.md
new file mode 100644
index 0000000000..6d9155a3a2
--- /dev/null
+++ b/.changeset/a11y-context-menu-tab.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': minor
+---
+
+[breaking] DropdownMenuRadioGroup now takes a required `label` prop that names the group for assistive tech (applied as aria-label), replacing the previous optional `aria-label`/`aria-labelledby` passthrough -- rename `aria-label="..."` to `label="..."` (pass `aria-labelledby` via base props instead when a visible label already exists). This also covers the ContextMenu/Breadcrumb re-exports (ContextMenuRadioGroup, BreadcrumbMenuRadioGroup). Also fixes ContextMenu to close the menu on Tab per the APG menu pattern.
+@bhamodi
diff --git a/apps/storybook/stories/ContextMenu.stories.tsx b/apps/storybook/stories/ContextMenu.stories.tsx
index ae68c0605f..2a3b31edb3 100644
--- a/apps/storybook/stories/ContextMenu.stories.tsx
+++ b/apps/storybook/stories/ContextMenu.stories.tsx
@@ -330,7 +330,7 @@ export const WithSelectableItems: Story = {
<ContextMenuRadioGroup
value={sort}
onChange={setSort}
- aria-label="Sort by">
+ label="Sort by">
<ContextMenuRadioItem value="name" label="Sort by name" />
<ContextMenuRadioItem value="date" label="Sort by date" />
<ContextMenuRadioItem value="size" label="Sort by size" />
diff --git a/apps/storybook/stories/DropdownMenu.stories.tsx b/apps/storybook/stories/DropdownMenu.stories.tsx
index e4b0ea2ab8..e8d7fc0f92 100644
--- a/apps/storybook/stories/DropdownMenu.stories.tsx
+++ b/apps/storybook/stories/DropdownMenu.stories.tsx
@@ -597,10 +597,7 @@ export const LabRadioGroup: Story = {
const [sort, setSort] = useState('newest');
return (
<DropdownMenu button={{label: 'Sort'}}>
- <DropdownMenuRadioGroup
- value={sort}
- onChange={setSort}
- aria-label="Sort by">
+ <DropdownMenuRadioGroup value={sort} onChange={setSort} label="Sort by">
<DropdownMenuRadioItem value="newest" label="Newest" />
<DropdownMenuRadioItem value="oldest" label="Oldest" />
<DropdownMenuRadioItem
@@ -629,19 +626,13 @@ export const LabSelectableSizes: Story = {
return (
<div style={{display: 'flex', gap: 24}}>
<DropdownMenu button={{label: 'Small menu', size: 'sm'}}>
- <DropdownMenuRadioGroup
- value={sm}
- onChange={setSm}
- aria-label="Small">
+ <DropdownMenuRadioGroup value={sm} onChange={setSm} label="Small">
<DropdownMenuRadioItem value="a" label="Option A" />
<DropdownMenuRadioItem value="b" label="Option B" />
</DropdownMenuRadioGroup>
</DropdownMenu>
<DropdownMenu button={{label: 'Large menu', size: 'lg'}}>
- <DropdownMenuRadioGroup
- value={lg}
- onChange={setLg}
- aria-label="Large">
+ <DropdownMenuRadioGroup value={lg} onChange={setLg} label="Large">
<DropdownMenuRadioItem value="a" label="Option A" />
<DropdownMenuRadioItem value="b" label="Option B" />
</DropdownMenuRadioGroup>
diff --git a/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx b/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx
index d47ee89b2c..ddc2d23ed2 100644
--- a/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx
+++ b/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx
@@ -519,7 +519,7 @@ describe('BreadcrumbItem menu', () => {
<BreadcrumbItem
menu={
<BreadcrumbMenuRadioGroup
- aria-label="Sort by"
+ label="Sort by"
value="name"
onChange={onChange}>
<BreadcrumbMenuRadioItem value="name" label="Name" />
diff --git a/packages/core/src/ContextMenu/ContextMenu.test.tsx b/packages/core/src/ContextMenu/ContextMenu.test.tsx
index 65faafdf0d..96d67575ee 100644
--- a/packages/core/src/ContextMenu/ContextMenu.test.tsx
+++ b/packages/core/src/ContextMenu/ContextMenu.test.tsx
@@ -136,6 +136,21 @@ describe('ContextMenu', () => {
expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled();
});
+ it('closes the menu when Tab is pressed inside it (APG menu pattern)', () => {
+ render(
+ <ContextMenu items={[{label: 'Item 1'}]}>
+ <div>Right-click me</div>
+ </ContextMenu>,
+ );
+
+ fireEvent.contextMenu(screen.getByText('Right-click me'));
+ expect(HTMLElement.prototype.showPopover).toHaveBeenCalled();
+
+ const menu = screen.getByRole('menu', {hidden: true});
+ fireEvent.keyDown(menu, {key: 'Tab'});
+ expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled();
+ });
+
it('ignores Escape during IME composition', () => {
render(
<ContextMenu items={[{label: 'Item 1'}]}>
@@ -552,7 +567,7 @@ describe('ContextMenu selectable items', () => {
<ContextMenuRadioGroup
value="name"
onChange={() => {}}
- aria-label="Sort by">
+ label="Sort by">
<ContextMenuRadioItem value="name" label="Sort by name" />
<ContextMenuRadioItem value="date" label="Sort by date" />
</ContextMenuRadioGroup>
@@ -587,7 +602,7 @@ describe('ContextMenu selectable items', () => {
<ContextMenuRadioGroup
value="name"
onChange={onSort}
- aria-label="Sort by">
+ label="Sort by">
<ContextMenuRadioItem value="name" label="Sort by name" />
<ContextMenuRadioItem value="date" label="Sort by date" />
</ContextMenuRadioGroup>
diff --git a/packages/core/src/ContextMenu/ContextMenu.tsx b/packages/core/src/ContextMenu/ContextMenu.tsx
index 8e83782ed3..6955b7e1a3 100644
--- a/packages/core/src/ContextMenu/ContextMenu.tsx
+++ b/packages/core/src/ContextMenu/ContextMenu.tsx
@@ -353,13 +353,22 @@ export function ContextMenu({
}
return;
}
+ // APG menu pattern: Tab closes the menu. Menu items are tabIndex={-1}
+ // so Tab would otherwise leak focus into the page while the menu stayed
+ // open (menus-5). Do NOT preventDefault — closing restores focus to the
+ // previously focused element, and the browser's default Tab then
+ // continues from there to the next element.
+ if (e.key === 'Tab') {
+ closeMenu();
+ return;
+ }
if (typeahead.onKeyDown(e)) {
e.preventDefault();
return;
}
listNavKeyDown(e);
},
- [listNavKeyDown, typeahead, ownsEvent],
+ [listNavKeyDown, closeMenu, typeahead, ownsEvent],
);
// Place the zero-size cursor anchor at a point in the trigger's local
diff --git a/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs
index 2189d05e96..8e9ce72b13 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs
+++ b/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs
@@ -22,16 +22,10 @@ export const docs = {
description: 'Callback fired when the selected value changes.',
},
{
- name: 'aria-label',
+ name: 'label',
type: 'string',
description:
- 'Accessible label for the group. Required (or aria-labelledby) so screen readers announce the radios as a named set, e.g. "Sort by".',
- },
- {
- name: 'aria-labelledby',
- type: 'string',
- description:
- 'The id of an element that labels the group, as an alternative to aria-label.',
+ 'Accessible name for the group, applied as aria-label so screen readers announce the radios as a named set, e.g. "Sort by". Required. Pass aria-labelledby (via base props) instead when the name already exists as a visible element.',
},
{
name: 'hasCloseOnSelect',
@@ -57,8 +51,7 @@ export const docsZh = {
propDescriptions: {
value: '组中当前选中的值。尚无选中项时传 undefined。',
onChange: '所选值变化时触发的回调。',
- 'aria-label': '组的无障碍标签。必需(或 aria-labelledby),以便屏幕阅读器将单选项作为命名集合朗读。',
- 'aria-labelledby': '标记该组的元素 id,作为 aria-label 的替代。',
+ label: '组的无障碍名称,作为 aria-label 应用,以便屏幕阅读器将单选项作为命名集合朗读,例如“Sort by”。必需。当名称已作为可见元素存在时,可改用 aria-labelledby(通过基础属性)。',
hasCloseOnSelect: '选择某值是否关闭菜单。默认关闭。',
children: '组成该组的 DropdownMenuRadioItem。',
},
@@ -72,8 +65,7 @@ export const docsDense = {
propDescriptions: {
value: 'selected value (undefined = none)',
onChange: 'fired when selected value changes',
- 'aria-label': 'accessible group name (or aria-labelledby); required',
- 'aria-labelledby': 'id of labelling element (alt to aria-label)',
+ label: 'accessible group name (applied as aria-label); required',
hasCloseOnSelect: 'close menu on select (default true)',
children: 'the DropdownMenuRadioItems',
},
diff --git a/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.tsx b/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.tsx
index 4809031bcb..52040b6c6a 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.tsx
@@ -49,16 +49,13 @@ export interface DropdownMenuRadioGroupProps extends Omit<
*/
onChange: (value: string) => void;
/**
- * Accessible label for the group. Required (together with `aria-labelledby`,
- * one of the two) so screen readers announce the radios as a named set,
- * e.g. "Sort by".
+ * Accessible name for the group, announced by screen readers so the radios
+ * read as a named set (e.g. "Sort by"). Applied as the group's `aria-label`.
+ * Required -- an unnamed radio group is an accessibility defect. Pass
+ * `aria-labelledby` (via base props) instead if the name already exists as a
+ * visible element on the page.
*/
- 'aria-label'?: string;
- /**
- * The id of an element that labels the group, as an alternative to
- * `aria-label`.
- */
- 'aria-labelledby'?: string;
+ label: string;
/**
* Whether selecting a value closes the menu. Radio items default to closing
* on selection (a single-choice commit), unlike checkbox items which stay
@@ -82,7 +79,7 @@ export interface DropdownMenuRadioGroupProps extends Omit<
* DropdownMenuRadioItem,
* } from '@astryxdesign/core/DropdownMenu';
* <DropdownMenu button={{label: 'Sort'}}>
- * <DropdownMenuRadioGroup value={sort} onChange={setSort} aria-label="Sort by">
+ * <DropdownMenuRadioGroup value={sort} onChange={setSort} label="Sort by">
* <DropdownMenuRadioItem value="newest" label="Newest" />
* <DropdownMenuRadioItem value="oldest" label="Oldest" />
* </DropdownMenuRadioGroup>
@@ -92,6 +89,7 @@ export interface DropdownMenuRadioGroupProps extends Omit<
export function DropdownMenuRadioGroup({
value,
onChange,
+ label,
hasCloseOnSelect = true,
children,
className,
@@ -107,6 +105,7 @@ export function DropdownMenuRadioGroup({
<div
{...rest}
role="group"
+ aria-label={label}
{...mergeProps(stylex.props(styles.group), {className, style})}>
<DropdownMenuRadioGroupContext value={contextValue}>
{children}
diff --git a/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx b/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
index 795691e804..c3770ac9b5 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
@@ -145,7 +145,7 @@ export interface DropdownMenuRadioItemProps extends Omit<
*
* @example
* ```
- * <DropdownMenuRadioGroup value={sort} onChange={setSort} aria-label="Sort by">
+ * <DropdownMenuRadioGroup value={sort} onChange={setSort} label="Sort by">
* <DropdownMenuRadioItem value="newest" label="Newest" />
* <DropdownMenuRadioItem value="oldest" label="Oldest" icon="clock" />
* </DropdownMenuRadioGroup>
diff --git a/packages/core/src/DropdownMenu/DropdownMenuSelectable.test.tsx b/packages/core/src/DropdownMenu/DropdownMenuSelectable.test.tsx
index f29d8e4434..f1c7b1d4d2 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuSelectable.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuSelectable.test.tsx
@@ -110,7 +110,7 @@ describe('DropdownMenuRadioGroup / RadioItem', () => {
<DropdownMenuRadioGroup
value="newest"
onChange={() => {}}
- aria-label="Sort by">
+ label="Sort by">
<DropdownMenuRadioItem value="newest" label="Newest" />
<DropdownMenuRadioItem value="oldest" label="Oldest" />
</DropdownMenuRadioGroup>
@@ -136,7 +136,7 @@ describe('DropdownMenuRadioGroup / RadioItem', () => {
<DropdownMenuRadioGroup
value="newest"
onChange={onChange}
- aria-label="Sort by">
+ label="Sort by">
<DropdownMenuRadioItem value="newest" label="Newest" />
<DropdownMenuRadioItem value="oldest" label="Oldest" />
</DropdownMenuRadioGroup>
@@ -149,6 +149,24 @@ describe('DropdownMenuRadioGroup / RadioItem', () => {
expect(onChange).toHaveBeenCalledWith('oldest');
});
+ it('names the group from the required label prop', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu button={{label: 'Sort'}}>
+ <DropdownMenuRadioGroup
+ value="newest"
+ onChange={() => {}}
+ label="Sort by">
+ <DropdownMenuRadioItem value="newest" label="Newest" />
+ </DropdownMenuRadioGroup>
+ </DropdownMenu>,
+ );
+ await user.click(screen.getByRole('button', {name: /Sort/}));
+ expect(
+ screen.getByRole('group', {name: 'Sort by', hidden: true}),
+ ).toBeInTheDocument();
+ });
+
it('throws when a radio item is used outside a group', () => {
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
expect(() =>