Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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/contextmenu-sheet-node-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[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.

@cixzhang
35 changes: 35 additions & 0 deletions packages/core/src/ContextMenu/ContextMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,41 @@ describe('ContextMenu', () => {
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
});

it('names the sheet after a node-label submenu while its level shows', async () => {
const user = userEvent.setup();

render(
<ContextMenu
presentation="bottom-sheet"
label="Row actions"
items={[
{
label: (
<>
<em>Move</em> to project
</>
),
items: [{label: 'Apollo launch'}],
},
]}>
<div>Right-click me</div>
</ContextMenu>,
);

fireEvent.contextMenu(screen.getByText('Right-click me'));
await user.click(
await screen.findByRole('button', {name: /Move to project/}),
);
expect(
screen.getByRole('dialog', {name: 'Move to project'}),
).toBeInTheDocument();

await user.click(screen.getByRole('button', {name: 'Back'}));
expect(
screen.getByRole('dialog', {name: 'Row actions'}),
).toBeInTheDocument();
});

it('drills into nested data items inside the BottomSheet', async () => {
const user = userEvent.setup();
const onMove = vi.fn();
Expand Down
21 changes: 18 additions & 3 deletions packages/core/src/ContextMenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,24 @@ export function ContextMenu({
const focusOnShowRef = useRef(false);
const focusFirstRef = useRef<() => void>(() => {});
const [submenuPath, setSubmenuPath] = useState<ContextMenuItemData[]>([]);
// The row reads a node label's text as the level opens. That text names the
// sheet.
const [submenuTitles, setSubmenuTitles] = useState<(string | undefined)[]>(
[],
);
const currentSubmenu = submenuPath.at(-1);
const currentItems = currentSubmenu?.items ?? items;
const currentTitle = currentSubmenu?.label ?? label;
const sheetLabel = typeof currentTitle === 'string' ? currentTitle : label;
const sheetLabel =
typeof currentTitle === 'string'
? currentTitle
: submenuTitles.at(-1) || label;

const updateOpenState = useCallback(
(nextIsOpen: boolean) => {
if (!nextIsOpen) {
setSubmenuPath([]);
setSubmenuTitles([]);
}
setIsOpen(nextIsOpen);
onOpenChange?.(nextIsOpen);
Expand Down Expand Up @@ -908,7 +917,10 @@ export function ContextMenu({
<Icon icon="chevronLeft" size="sm" xstyle={rtlStyles.mirror} />
}
isIconOnly
onClick={() => setSubmenuPath(path => path.slice(0, -1))}
onClick={() => {
setSubmenuPath(path => path.slice(0, -1));
setSubmenuTitles(titles => titles.slice(0, -1));
}}
/>
{sheetViewHeading}
</div>
Expand All @@ -918,7 +930,10 @@ export function ContextMenu({
<MenuBottomSheetActionList
items={currentItems}
onSelect={handleBottomSheetSelect}
onOpenSubmenu={item => setSubmenuPath(path => [...path, item])}
onOpenSubmenu={(item, title) => {
setSubmenuPath(path => [...path, item]);
setSubmenuTitles(titles => [...titles, title]);
}}
/>
</div>
) : (
Expand Down