Skip to content

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

TypeScript

fix(DropdownMenu): a renderTrigger menu names its drill-in Back row after its trigger (#7304)

Christopher Chedeauf0ed0c3080a1
diff --git a/.changeset/dropdown-menu-trigger-back-row.md b/.changeset/dropdown-menu-trigger-back-row.md
new file mode 100644
index 0000000000..f58abeb987
--- /dev/null
+++ b/.changeset/dropdown-menu-trigger-back-row.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A DropdownMenu hung off its own control through `renderTrigger` names a drilled-in sub-menu's Back row after the menu: "Back to <the menu's aria-label>", or the name of the control it hangs off, instead of "Back to Menu".
+
+@vjeux
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.spec.md b/packages/core/src/DropdownMenu/DropdownMenu.spec.md
index 203f3499ce..ec60514578 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.spec.md
+++ b/packages/core/src/DropdownMenu/DropdownMenu.spec.md
@@ -106,7 +106,7 @@ released default.
| FR7 | `menuMaxHeight` MUST replace the 300px term of the menu's and its popover viewport's block-size cap while the viewport gutters still bound both; the touch sheet ignores it. | `component:DropdownMenu/DEC-3` | Proposed; verified in jsdom; owner to confirm DEC-3 |
| FR8 | The Trigger button opens the pointer menu on a mouse press-down and on a finger held for the long-press delay, with the module's settle rule on the opening release; pressing the trigger of an open menu closes it without reopening in the same gesture; a tap and the keyboard open as before (`module:DropdownMenu/useMenuPress` proposed FR8, FR9). | Proposed `module:DropdownMenu/useMenuPress/DEC-4`; `DropdownMenu.test.tsx` press model suite | Proposed; verified in jsdom, pending owner review |
| Trigger source | `button` (Button props) or `renderTrigger` (render prop) | Which control the menu hangs off. `trigger` receives `DropdownMenuTriggerProps` — the press model, keyboard opens, toggle click and ARIA wiring — and names the menu. | Both presentations | `button` | `component:DropdownMenu` | stable | Both given: `trigger` wins and a dev warning fires. |
-| FR9 | With `renderTrigger`, the rendered control MUST carry `aria-haspopup`, `aria-expanded`, `aria-controls` and the `id` the menu's `aria-labelledby` points at, and MUST receive the same press-model, keyboard-open and toggle-click handlers as the Button path; the menu is named by that control. `button` and `trigger` are mutually exclusive: both given, `trigger` wins and a dev warning fires, in both presentations. | `component:DropdownMenu/DEC-2` | Proposed; verified in jsdom; owner to confirm DEC-2 |
+| FR9 | With `renderTrigger`, the rendered control MUST carry `aria-haspopup`, `aria-expanded`, `aria-controls` and the `id` the menu's `aria-labelledby` points at, and MUST receive the same press-model, keyboard-open and toggle-click handlers as the Button path; the menu is named by that control. `button` and `trigger` are mutually exclusive: both given, `trigger` wins and a dev warning fires, in both presentations. A drilled-in Back row (FR11) returns to the menu by that same name: the caller's `aria-label` when given, else the control's accessible name, read as the menu opens. | `component:DropdownMenu/DEC-2` | Proposed; verified in jsdom; owner to confirm DEC-2 |
| Row destination | `href` (+ `target`, `rel`) on an action item | The row's act is navigation: the row itself is the link (`family:navigation-destinations`). | Pointer action row and Touch action row, compound and data mode | none (an action row) | `component:DropdownMenu` | stable | Blocked schemes follow the family rule; a disabled row has none. |
| FR10 | An action row with `href` MUST render as one anchor carrying `role="menuitem"` (the pointer menu) or the touch sheet's link row, routed through `LinkProvider`; a plain click MUST run `onClick`, then close the menu (`hasCloseOnSelect`), then let the browser navigate; a modified click or middle click MUST be left to the browser and MUST NOT run `onClick`; Enter and Space MUST activate through a synthesized click that carries the key's modifiers. A disabled link row has no `href`. | `component:DropdownMenu/DEC-2`; `family:navigation-destinations` | Proposed; verified in jsdom; owner to confirm DEC-1 |
| Sub-menu presentation | `presentation` on a sub-menu row: `flyout`, `drill-in`, `adaptive` | Whether a sub-menu opens beside its row or replaces the menu's rows with its own and a Back row, in the same box. `adaptive` drills in when a coarse pointer opened the menu. | Pointer presentation, compound and data mode | `adaptive` | `component:DropdownMenu` | stable | An unknown value reads as `adaptive`. |
@@ -272,7 +272,7 @@ than adding a DropdownMenu-owned heading target.
| FR7 | `menuMaxHeight` MUST replace the 300px term of the menu's and its popover viewport's block-size cap while the viewport gutters still bound both; the touch sheet ignores it. | `component:DropdownMenu/DEC-3` | Proposed; verified in jsdom; owner to confirm DEC-3 |
| Theming anatomy map | `scripts/check-knowledge.mjs` | Canonical anatomy and current target inventory | Missing, extra, prefixed, stale, or unclassified mappings fail repository validation. | `audit:DropdownMenu/theming` |
| FR8 | `DropdownMenu.test.tsx` press model suite (trigger cases) | Press-open + drag-release, unsettled opening release, trigger toggle, held touch, tap | A menu that does not open on a mouse press, an unsettled release that acts, or a trigger that reopens in the same gesture fails. | `audit:DropdownMenu/behavior` |
-| FR9 | `DropdownMenu.test.tsx` custom trigger suite | Icon button, chip, list row; open, toggle, keyboard open, name | A rendered control missing `aria-haspopup`, `aria-expanded` or `aria-controls`, an unnamed menu, or a lost keyboard open, fails. | `audit:DropdownMenu/anatomy` |
+| FR9 | `DropdownMenu.test.tsx` custom trigger suite; `DropdownMenuSubMenu.test.tsx` custom-trigger Back row cases | Icon button, chip, list row; open, toggle, keyboard open, name; drill-in Back row | A rendered control missing `aria-haspopup`, `aria-expanded` or `aria-controls`, an unnamed menu, a lost keyboard open, or a "Back to Menu" row, fails. | `audit:DropdownMenu/anatomy` |
| FR8, AR2 | `DropdownMenu.test.tsx` "DropdownMenuItem href" suite (pointer menu and bottom sheet) | Plain click, ⌘-click, Enter with modifiers, disabled link row | A row rendering an inner anchor, running `onClick` on a modified click, or a synthesized click dropping the modifiers fails. | `audit:DropdownMenu/behavior` |
| FR12, AR3 | `DropdownMenuSubMenu.test.tsx` "drill-in on a phone" suite; `__tests__/DropdownMenuDrillIn.a11y.chromium.spec.ts` (real Chromium, coarse pointer) | Drill in, Back, Escape, ArrowLeft, nested level, scoped typeahead, forced presentations, close resets | A flyout opening on a coarse pointer, a missing Back row, sibling rows staying in the order, or focus not returning to the row fails. | `audit:DropdownMenu/behavior` |
| FR10 | `DropdownMenuSubMenu.test.tsx` safe-triangle suite; `useMenuHover.test.tsx` `isPointInSafeTriangle` and click-guard suites | diagonal path toward the flyout, a pause inside it, path away, click-opened and hover-opened guard windows | A flyout that closes while the pointer is inside the triangle or paused in it, one that never closes after leaving it, or a changed guard-window rule, fails. | `audit:DropdownMenu/behavior` |
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index 0e5596afb9..11bbaee7c9 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -451,6 +451,48 @@ function holdInvokerThroughPress(
doc.addEventListener('pointercancel', onPressEnd, true);
}
+/** Text an assistive technology reads from `element`, `aria-hidden` left out. */
+function visibleText(element: Element): string {
+ const copy = element.cloneNode(true) as Element;
+ for (const hidden of copy.querySelectorAll('[aria-hidden="true"]')) {
+ hidden.remove();
+ }
+ return (copy.textContent ?? '').replace(/\s+/g, ' ').trim();
+}
+
+/**
+ * The name a custom trigger gives the menu it labels (`aria-labelledby` on
+ * the menu points at it): its own `aria-labelledby` targets, its
+ * `aria-label`, its text, or its `title`, in that order.
+ */
+function readAccessibleName(element: HTMLElement | null): string | undefined {
+ if (element == null) {
+ return undefined;
+ }
+ const labelledBy = element.getAttribute('aria-labelledby');
+ if (labelledBy != null && labelledBy.trim() !== '') {
+ const text = labelledBy
+ .split(/\s+/)
+ .map(id => element.ownerDocument.getElementById(id))
+ .filter((node): node is HTMLElement => node != null)
+ .map(visibleText)
+ .join(' ')
+ .trim();
+ if (text !== '') {
+ return text;
+ }
+ }
+ const label = element.getAttribute('aria-label')?.trim();
+ if (label) {
+ return label;
+ }
+ const text = visibleText(element);
+ if (text !== '') {
+ return text;
+ }
+ return element.getAttribute('title')?.trim() || undefined;
+}
+
function DropdownMenuBottomSheet({
button: buttonFromProps,
renderTrigger,
@@ -722,6 +764,9 @@ function DropdownMenuPopover({
const menuSize = button.size ?? 'md';
const buttonRef = useRef<HTMLElement>(null);
+ // The name a custom trigger gives the menu, read off it as the menu opens.
+ const [triggerName, setTriggerName] = useState<string | undefined>(undefined);
+
// Open state
const [internalIsOpen, setInternalIsOpen] = useState(false);
const isControlled = controlledIsOpen !== undefined;
@@ -793,6 +838,9 @@ function DropdownMenuPopover({
const handleLayerShow = useCallback(() => {
acceptedOpenRef.current = true;
resetFocusReturn();
+ if (renderTrigger != null) {
+ setTriggerName(readAccessibleName(buttonRef.current));
+ }
if (notifyClickOnShowRef.current) {
notifyClickOnShowRef.current = false;
onClick?.();
@@ -801,7 +849,7 @@ function DropdownMenuPopover({
if (!isControlled) {
setInternalIsOpen(true);
}
- }, [isControlled, onClick, onOpenChange, resetFocusReturn]);
+ }, [isControlled, onClick, onOpenChange, renderTrigger, resetFocusReturn]);
const popover = usePopover({
onHide: handleLayerHide,
@@ -1138,8 +1186,19 @@ function DropdownMenuPopover({
// The drill-in view stack for sub-menus on a phone.
const {drillIn, wrapContent} = useMenuDrillIn(isOpen);
- // The name a drilled-in view's Back row returns to.
- const menuLabel = typeof button.label === 'string' ? button.label : undefined;
+ // The name a drilled-in view's Back row returns to: the menu's own name.
+ // The design system's button names the menu by its label. A custom trigger
+ // names it by reference (or the caller's `aria-label` does), so that name
+ // is read off the control as the menu opens.
+ const callerMenuLabel = rest['aria-label'];
+ const menuLabel =
+ renderTrigger != null
+ ? typeof callerMenuLabel === 'string' && callerMenuLabel.trim() !== ''
+ ? callerMenuLabel
+ : triggerName
+ : typeof button.label === 'string'
+ ? button.label
+ : undefined;
const resolvedMaxHeight = menuMaxHeight == null ? null : `${menuMaxHeight}px`;
// Context for compound items
diff --git a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
index 73d9cd5f94..f4e4481ce8 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
@@ -14,6 +14,7 @@ import * as stylex from '@stylexjs/stylex';
import {DropdownMenu} from './DropdownMenu';
import {DropdownMenuItem} from './DropdownMenuItem';
import {DropdownMenuSubMenu} from './DropdownMenuSubMenu';
+import {IconButton} from '../IconButton';
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';
import {rtlStyles} from '../utils';
@@ -1273,4 +1274,78 @@ describe('DropdownMenuSubMenu drill-in on a phone', () => {
);
expect(visibleRows()).toEqual(['Rename', 'Move to', 'Delete']);
});
+
+ describe('a menu hung off a custom trigger', () => {
+ async function drillIntoMoveTo(triggerName: RegExp | string) {
+ const user = userEvent.setup();
+ const trigger = screen.getByRole('button', {name: triggerName});
+ await user.click(trigger);
+ await waitFor(() =>
+ expect(trigger).toHaveAttribute('aria-expanded', 'true'),
+ );
+ fireEvent.click(await waitFor(() => shownRow('Move to')), {detail: 1});
+ await waitFor(() => shownMenu('Move to'));
+ return visibleRows()[0];
+ }
+
+ it('names its Back row after the icon button it hangs off', async () => {
+ stubCompactTouch(false);
+ render(
+ <DropdownMenu
+ renderTrigger={props => (
+ <IconButton
+ icon="moreHorizontal"
+ label="Session actions"
+ {...props}
+ />
+ )}>
+ <DropdownMenuSubMenu label="Move to" presentation="drill-in">
+ <DropdownMenuItem label="Folder A" onClick={() => {}} />
+ </DropdownMenuSubMenu>
+ </DropdownMenu>,
+ );
+ expect(await drillIntoMoveTo('Session actions')).toBe(
+ 'Back to Session actions',
+ );
+ });
+
+ it('names its Back row after a text trigger', async () => {
+ stubCompactTouch(false);
+ render(
+ <DropdownMenu
+ renderTrigger={props => (
+ <button type="button" {...props}>
+ Options
+ </button>
+ )}>
+ <DropdownMenuSubMenu label="Move to" presentation="drill-in">
+ <DropdownMenuItem label="Folder A" onClick={() => {}} />
+ </DropdownMenuSubMenu>
+ </DropdownMenu>,
+ );
+ expect(await drillIntoMoveTo('Options')).toBe('Back to Options');
+ });
+
+ it('takes the name the caller gave the menu with aria-label', async () => {
+ stubCompactTouch(false);
+ render(
+ <DropdownMenu
+ aria-label="Row actions"
+ renderTrigger={props => (
+ <button type="button" {...props}>
+ …
+ </button>
+ )}>
+ <DropdownMenuSubMenu label="Move to" presentation="drill-in">
+ <DropdownMenuItem label="Folder A" onClick={() => {}} />
+ </DropdownMenuSubMenu>
+ </DropdownMenu>,
+ );
+ expect(shownMenuNamed('Row actions')).toBeDefined();
+ expect(await drillIntoMoveTo('…')).toBe('Back to Row actions');
+ });
+
+ const shownMenuNamed = (name: string) =>
+ screen.getAllByRole('menu', {name, ...h}).find(isShown);
+ });
});