Skip to content

Commit ddc5dd1

Browse files
authored
fix(ContextMenu): a node-label sub-menu level names the bottom sheet (#11936)
* test(ContextMenu): a node-label sub-menu level must name the bottom sheet * fix(ContextMenu): a node-label sub-menu level names the bottom sheet ContextMenu named its bottom sheet only from a string sub-menu label, so a level whose label is a node kept the root's name and a screen reader did not announce the level. It now keeps a per-level title list fed by the sheet's onOpenSubmenu(item, title), as DropdownMenu does, and names the sheet after the current level's title. Back and close drop the titles with their levels. * fix(ContextMenu): an empty sub-menu title keeps the root sheet name; the release note names the sheet only * Merge remote-tracking branch 'origin/main' into fix/contextmenu-sheet-node-label * Merge remote-tracking branch 'origin/main' into fix/contextmenu-sheet-node-label * Merge remote-tracking branch 'origin/main' into fix/contextmenu-sheet-node-label
1 parent fbbcd2c commit ddc5dd1

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] ContextMenu: in a bottom sheet, a data sub-menu whose label is not a plain string (for example emphasized text) now names the sheet after that label's text while its level shows, as DropdownMenu does. Back returns the root name.
6+
7+
@cixzhang

‎packages/core/src/ContextMenu/ContextMenu.test.tsx‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,41 @@ describe('ContextMenu', () => {
293293
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
294294
});
295295

296+
it('names the sheet after a node-label submenu while its level shows', async () => {
297+
const user = userEvent.setup();
298+
299+
render(
300+
<ContextMenu
301+
presentation="bottom-sheet"
302+
label="Row actions"
303+
items={[
304+
{
305+
label: (
306+
<>
307+
<em>Move</em> to project
308+
</>
309+
),
310+
items: [{label: 'Apollo launch'}],
311+
},
312+
]}>
313+
<div>Right-click me</div>
314+
</ContextMenu>,
315+
);
316+
317+
fireEvent.contextMenu(screen.getByText('Right-click me'));
318+
await user.click(
319+
await screen.findByRole('button', {name: /Move to project/}),
320+
);
321+
expect(
322+
screen.getByRole('dialog', {name: 'Move to project'}),
323+
).toBeInTheDocument();
324+
325+
await user.click(screen.getByRole('button', {name: 'Back'}));
326+
expect(
327+
screen.getByRole('dialog', {name: 'Row actions'}),
328+
).toBeInTheDocument();
329+
});
330+
296331
it('drills into nested data items inside the BottomSheet', async () => {
297332
const user = userEvent.setup();
298333
const onMove = vi.fn();

‎packages/core/src/ContextMenu/ContextMenu.tsx‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,24 @@ export function ContextMenu({
444444
const focusOnShowRef = useRef(false);
445445
const focusFirstRef = useRef<() => void>(() => {});
446446
const [submenuPath, setSubmenuPath] = useState<ContextMenuItemData[]>([]);
447+
// The row reads a node label's text as the level opens. That text names the
448+
// sheet.
449+
const [submenuTitles, setSubmenuTitles] = useState<(string | undefined)[]>(
450+
[],
451+
);
447452
const currentSubmenu = submenuPath.at(-1);
448453
const currentItems = currentSubmenu?.items ?? items;
449454
const currentTitle = currentSubmenu?.label ?? label;
450-
const sheetLabel = typeof currentTitle === 'string' ? currentTitle : label;
455+
const sheetLabel =
456+
typeof currentTitle === 'string'
457+
? currentTitle
458+
: submenuTitles.at(-1) || label;
451459

452460
const updateOpenState = useCallback(
453461
(nextIsOpen: boolean) => {
454462
if (!nextIsOpen) {
455463
setSubmenuPath([]);
464+
setSubmenuTitles([]);
456465
}
457466
setIsOpen(nextIsOpen);
458467
onOpenChange?.(nextIsOpen);
@@ -908,7 +917,10 @@ export function ContextMenu({
908917
<Icon icon="chevronLeft" size="sm" xstyle={rtlStyles.mirror} />
909918
}
910919
isIconOnly
911-
onClick={() => setSubmenuPath(path => path.slice(0, -1))}
920+
onClick={() => {
921+
setSubmenuPath(path => path.slice(0, -1));
922+
setSubmenuTitles(titles => titles.slice(0, -1));
923+
}}
912924
/>
913925
{sheetViewHeading}
914926
</div>
@@ -918,7 +930,10 @@ export function ContextMenu({
918930
<MenuBottomSheetActionList
919931
items={currentItems}
920932
onSelect={handleBottomSheetSelect}
921-
onOpenSubmenu={item => setSubmenuPath(path => [...path, item])}
933+
onOpenSubmenu={(item, title) => {
934+
setSubmenuPath(path => [...path, item]);
935+
setSubmenuTitles(titles => [...titles, title]);
936+
}}
922937
/>
923938
</div>
924939
) : (

0 commit comments

Comments
 (0)