Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feat] `createContextMenu(element, options)` attaches ContextMenu's menu to any element, without wrapping it, and `useContextMenu(ref, options)` does the same from a component. A ListItem row passes its ref, so the list keeps `ul > li`, with no wrapper element, and its dividers. The menu opens on a long press, a right click, Shift+F10 or the context-menu key, and `destroy` removes every listener and node it added.

@vjeux
141 changes: 140 additions & 1 deletion apps/storybook/stories/ContextMenu.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import type {Meta, StoryObj} from '@storybook/react';
import {useState} from 'react';
import {useEffect, useRef, useState} from 'react';
import {
ContextMenu,
ContextMenuItem,
ContextMenuCheckboxItem,
ContextMenuRadioGroup,
ContextMenuRadioItem,
createContextMenu,
useContextMenu,
} from '@astryxdesign/core/ContextMenu';
import {Divider} from '@astryxdesign/core/Divider';
import {List, ListItem} from '@astryxdesign/core/List';
import {
PencilIcon,
TrashIcon,
Expand Down Expand Up @@ -647,3 +650,139 @@ export const BottomSheetSubmenu: Story = {
}
},
};

const headlessStyles = stylex.create({
list: {
listStyle: 'none',
margin: 0,
padding: 0,
maxWidth: '320px',
},
row: {
padding: '12px 16px',
borderBottomWidth: '1px',
borderBottomStyle: 'solid',
borderBottomColor: '#e5e7eb',
userSelect: 'none',
},
listItems: {
maxWidth: '320px',
},
});

/**
* Four rows of a list: the second carries a menu from the framework-agnostic
* createContextMenu, the third and fourth from useContextMenu. The list keeps
* only `li` children; the menus render outside it. The log below the list
* records what the page saw, for the interaction tests.
*/
function HeadlessRows() {
const plainRowRef = useRef<HTMLLIElement>(null);
const hookRowRef = useRef<HTMLLIElement>(null);
const contentRowRef = useRef<HTMLLIElement>(null);
const [log, setLog] = useState<ReadonlyArray<string>>([]);
const record = (entry: string) => setLog(entries => [...entries, entry]);
useEffect(() => {
const row = plainRowRef.current;
if (row == null) {
return;
}
const menu = createContextMenu(row, {
label: 'Inbox actions',
onOpenChange: isOpen => record(isOpen ? 'open' : 'close'),
items: [
{label: 'Archive', onClick: () => record('archive')},
{label: 'Delete', variant: 'destructive', onClick: () => {}},
],
});
return () => menu.destroy();
}, []);
useContextMenu(hookRowRef, {
label: 'Sent actions',
items: [{label: 'Forward', onClick: () => record('forward')}],
});
useContextMenu(contentRowRef, {
label: 'Archive actions',
menuContent: (
<ContextMenuItem label="Restore" onClick={() => record('restore')} />
),
});
return (
<div>
<ul {...stylex.props(headlessStyles.list)} data-testid="headless-list">
<li tabIndex={0} {...stylex.props(headlessStyles.row)}>
Drafts
</li>
<li
ref={plainRowRef}
tabIndex={0}
data-testid="headless-row"
onClick={() => record('row-click')}
{...stylex.props(headlessStyles.row)}>
Inbox
</li>
<li
ref={hookRowRef}
tabIndex={0}
data-testid="hook-row"
{...stylex.props(headlessStyles.row)}>
Sent
</li>
<li
ref={contentRowRef}
tabIndex={0}
data-testid="hook-content-row"
{...stylex.props(headlessStyles.row)}>
Archive
</li>
</ul>
<output data-testid="headless-log">{log.join(',')}</output>
</div>
);
}

export const HeadlessOnAListRow: Story = {
name: 'Headless / createContextMenu on a list row',
parameters: {
docs: {
description: {
story:
'createContextMenu attaches ContextMenu to an element that is already on the page, here a list row, without React; useContextMenu does the same from a component. A held finger, a right click, Shift+F10 or the context-menu key opens it.',
},
},
},
render: () => <HeadlessRows />,
};

/** A ListItem whose row menu attaches to its own `li`. */
function RowWithMenu({label}: {label: string}) {
const rowRef = useRef<HTMLLIElement>(null);
useContextMenu(rowRef, {
label: `Actions for ${label}`,
items: [
{label: 'Archive', onClick: () => {}},
{label: 'Delete', variant: 'destructive', onClick: () => {}},
],
});
return <ListItem ref={rowRef} label={label} />;
}

export const HeadlessOnListItems: Story = {
name: 'Headless / useContextMenu on ListItem rows',
parameters: {
docs: {
description: {
story:
'Each ListItem passes its ref to useContextMenu, so the menu attaches to the row itself: the list keeps `ul > li`, with no wrapper element, and its dividers. A ContextMenu around a ListItem would put a wrapper between the list and its row.',
},
},
},
render: () => (
<List hasDividers xstyle={headlessStyles.listItems} data-testid="menu-list">
<RowWithMenu label="Inbox" />
<RowWithMenu label="Sent" />
<RowWithMenu label="Drafts" />
<RowWithMenu label="Archive" />
</List>
),
};
12 changes: 8 additions & 4 deletions docs/specs/components/ContextMenu/ContextMenu.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Consumer migration instructions belong in consumer docs and release notes.
`context-menu` target.
- Cursor-anchor placement across the two presentation paths, and open state
(FR10).
- The menu root in `ContextMenu.tsx` that ContextMenu and `createContextMenu`
both render: the menu, its open state, outside press, and focus return.
Only `ContextMenu.tsx` and `createContextMenu.tsx` use it.

**Does not own / non-goals**

Expand Down Expand Up @@ -112,7 +115,7 @@ Consumer props, content modes, and presentation policy remain documented in
| FR7 | Sub-menu rows follow `component:DropdownMenu` FR11 inside the pointer menu: ContextMenu mounts the same drill-in view stack, named after its `label`, so a sub-menu drills in on a phone here too. A sub-menu with a header or footer shows its rows alone here (`component:DropdownMenu` FR20). | `component:DropdownMenu/DEC-4` |
| FR8 | In the pointer menu ArrowDown on the last row wraps to the first and ArrowUp on the first to the last; PageDown and PageUp page as in `component:DropdownMenu` FR12; a held key's auto-repeat does not activate; typeahead matches the row's label element alone. A disabled row is reached by these keys and by typeahead and stays inert, as `component:DropdownMenu` FR17 states. | Current source and tests |
| FR9 | While the Pointer menu surface is open, it participates in `family:overlay-dismissal` through the shared stack and provides descendant depth. One Escape press affects only the top-most present nested surface; ContextMenu closes only when it is that surface. | `family:overlay-dismissal` FR1–FR6; `ContextMenu.test.tsx` nested-layer case |
| FR10 | ContextMenu MUST be built on the public headless context menu (`spec:AST-107` FR8), with its invocation, open state, and cleanup the factory's and no behavior of its own beside the factory's. Implementation gap: no headless context menu ships; ContextMenu owns its invocation, open state, outside-press listener, and cleanup itself. | `spec:AST-107` FR8 |
| FR10 | ContextMenu MUST be built on the public headless context menu (`spec:AST-107` FR8), with its invocation, open state, and cleanup the factory's and no behavior of its own beside the factory's. Implementation gap: ContextMenu and `createContextMenu` share one menu, open state, outside-press listener, and cleanup, but ContextMenu takes its invocation on its own wrapper element instead of through the factory. | `spec:AST-107` FR8 |
| FR11 | Tab or Shift+Tab on a row of the pointer menu, its own or an open sub-menu flyout's at any depth, closes every open level and the menu and calls `onOpenChange(false)` once, as `component:DropdownMenu` FR23 states for its menu. | WAI-ARIA Menu Button pattern; current tests |

### Allowed variation
Expand Down Expand Up @@ -268,9 +271,10 @@ anchor is positioning infrastructure rather than consumer anatomy.

## Current-state impact

- **No headless factory (FR10).** No headless context menu ships, so ContextMenu
owns its invocation, open state, and outside-press listener itself, against
`spec:AST-107` FR8.
- **ContextMenu beside the factory (FR10).** `createContextMenu` and
`useContextMenu` share ContextMenu's menu, open state, and outside-press
listener, but ContextMenu takes its invocation on its own wrapper element
instead of through the factory, against `spec:AST-107` FR8.
- **Touch grow, pan dismissal, handoff, and lift (FR5).** A touch-opened
menu keeps the layer's own entrance and exit instead of growing from the
finger, a scroll that starts outside leaves it open, the held finger that
Expand Down
Loading