Skip to content

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

TypeScript

fix(DropdownMenu): a bottom sheet opened from renderTrigger is named after the menu, not "Menu" (#9604)

Ojas Mutreja7d1179aa9b14
diff --git a/.changeset/dropdownmenu-sheet-render-trigger-name.md b/.changeset/dropdownmenu-sheet-render-trigger-name.md
new file mode 100644
index 0000000000..e219138188
--- /dev/null
+++ b/.changeset/dropdownmenu-sheet-render-trigger-name.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A data-mode `DropdownMenu` that opens as a bottom sheet from a `renderTrigger` now titles the sheet, and names its dialog, after the menu's `aria-label`, else the trigger's own name, as its popover does. It no longer reads "Menu" for every such sheet. (#9525)
+@HelloOjasMutreja
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index 15acea880e..c76e5622f0 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -169,6 +169,82 @@ describe('DropdownMenu', () => {
);
});
+ describe('bottom sheet opened from a custom trigger', () => {
+ it("names the sheet and its heading after the caller's aria-label", async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu
+ aria-label="Create"
+ presentation="bottom-sheet"
+ renderTrigger={props => (
+ <button type="button" {...props}>
+ Add
+ </button>
+ )}
+ items={[{label: 'Contact'}, {label: 'Invoice'}]}
+ />,
+ );
+
+ await user.click(screen.getByRole('button', {name: 'Add'}));
+
+ expect(screen.getByRole('dialog', {name: 'Create'})).toBeInTheDocument();
+ expect(screen.getByRole('heading', {name: 'Create'})).toBeInTheDocument();
+ expect(screen.queryByRole('dialog', {name: 'Menu'})).toBeNull();
+ });
+
+ it("falls back to the trigger's own name without an aria-label", async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu
+ presentation="bottom-sheet"
+ renderTrigger={props => (
+ <button type="button" {...props}>
+ Create
+ </button>
+ )}
+ items={[{label: 'Contact'}, {label: 'Invoice'}]}
+ />,
+ );
+
+ await user.click(screen.getByRole('button', {name: 'Create'}));
+
+ expect(screen.getByRole('dialog', {name: 'Create'})).toBeInTheDocument();
+ expect(screen.getByRole('heading', {name: 'Create'})).toBeInTheDocument();
+ });
+
+ it('names an open controlled sheet from the trigger on its first render', () => {
+ render(
+ <DropdownMenu
+ isMenuOpen
+ presentation="bottom-sheet"
+ renderTrigger={props => (
+ <button type="button" {...props}>
+ Create
+ </button>
+ )}
+ items={[{label: 'Contact'}]}
+ />,
+ );
+
+ expect(screen.getByRole('dialog', {name: 'Create'})).toBeInTheDocument();
+ });
+
+ it('keeps the default title when the trigger has no name to read', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu
+ presentation="bottom-sheet"
+ renderTrigger={props => <button type="button" {...props} />}
+ items={[{label: 'Contact'}]}
+ />,
+ );
+
+ await user.click(screen.getByRole('button', {name: ''}));
+
+ expect(screen.getByRole('dialog', {name: 'Menu'})).toBeInTheDocument();
+ });
+ });
+
it('renders data-driven actions in a bottom sheet when requested', async () => {
const user = userEvent.setup();
const onEdit = vi.fn();
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index 0ed3059384..8e6bd78a2d 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -533,6 +533,20 @@ function holdInvokerThroughPress(
doc.addEventListener('pointercancel', onPressEnd, true);
}
+/**
+ * The name of a menu opened from a custom trigger: the caller's `aria-label`
+ * when it is a non-empty string, else the trigger's own accessible name. The
+ * popover and the bottom sheet both name the menu through this.
+ */
+function resolveCustomTriggerMenuName(
+ callerLabel: unknown,
+ triggerName: string | undefined,
+): string | undefined {
+ return typeof callerLabel === 'string' && callerLabel.trim() !== ''
+ ? callerLabel
+ : triggerName;
+}
+
function DropdownMenuBottomSheet({
button: buttonFromProps,
renderTrigger,
@@ -596,11 +610,22 @@ function DropdownMenuBottomSheet({
useEffect(() => {
void preloadMenuBottomSheetActionList();
}, []);
+ // The control's name is only readable from the DOM once it is rendered.
+ const [triggerName, setTriggerName] = useState<string | undefined>(undefined);
+ useIsomorphicLayoutEffect(() => {
+ if (renderTrigger != null && isOpen) {
+ setTriggerName(readAccessibleName(buttonRef.current));
+ }
+ }, [renderTrigger, isOpen]);
+ const rootTitle =
+ (renderTrigger != null
+ ? resolveCustomTriggerMenuName(rest['aria-label'], triggerName)
+ : undefined) ?? button.label;
const currentSubmenu = submenuPath.at(-1);
const currentItems = currentSubmenu?.items ?? items;
- const currentTitle = currentSubmenu?.label ?? button.label;
+ const currentTitle = currentSubmenu?.label ?? rootTitle;
const sheetLabel =
- typeof currentTitle === 'string' ? currentTitle : button.label;
+ typeof currentTitle === 'string' ? currentTitle : rootTitle;
const setOpen = useCallback(
(nextIsOpen: boolean) => {
@@ -1040,12 +1065,9 @@ function DropdownMenuPopover({
// 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
+ ? resolveCustomTriggerMenuName(rest['aria-label'], triggerName)
: typeof button.label === 'string'
? button.label
: undefined;