An open source design system that's fully customizable and agent ready
TypeScript
feat(DropdownMenuItem): forward host attributes and DOM handlers to the row (#7196)
Christopher Chedeaue02fa7c754b9
diff --git a/.changeset/dropdown-menu-item-host-attributes.md b/.changeset/dropdown-menu-item-host-attributes.mdnew file mode 100644index 0000000000..35f69a4fde--- /dev/null+++ b/.changeset/dropdown-menu-item-host-attributes.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[feat] `DropdownMenuItem` passes host attributes and DOM event handlers to its row, as `DropdownMenuCheckboxItem`, `DropdownMenuRadioItem` and `Item` already do: a row can carry `aria-current`, `aria-busy`, a `data-*` mark, an `id`, or drag-and-drop handlers. The row keeps its own role and tab stop; a caller's `onPointerMove` and `onAuxClick` run after the row's hover focus and middle-click close.++@vjeuxdiff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsxindex f0249b36ff..fa8f353eeb 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx@@ -1457,6 +1457,111 @@ describe('DropdownMenuItem ref', () => {});});+describe('DropdownMenuItem host attributes', () => {+ it('puts aria-*, data-* and id on the row root, a link row included', () => {+ render(+ <DropdownMenu button={{label: 'Views'}}>+ <DropdownMenuItem+ label="Inbox"+ href="/inbox"+ aria-current="page"+ data-view="inbox"+ id="inbox-row"+ />+ <DropdownMenuItem+ label="Reload"+ onClick={() => {}}+ aria-busy={true}+ data-pending=""+ />+ </DropdownMenu>,+ );+ const inbox = screen.getByRole('menuitem', {name: 'Inbox', hidden: true});+ expect(inbox.tagName).toBe('A');+ expect(inbox).toHaveAttribute('aria-current', 'page');+ expect(inbox).toHaveAttribute('data-view', 'inbox');+ expect(inbox).toHaveAttribute('id', 'inbox-row');+ const reload = screen.getByRole('menuitem', {name: 'Reload', hidden: true});+ expect(reload).toHaveAttribute('aria-busy', 'true');+ expect(reload).toHaveAttribute('data-pending', '');+ });++ it('runs the DOM handlers the caller attaches, such as a drop target', () => {+ const onDragOver = vi.fn((event: {preventDefault: () => void}) =>+ event.preventDefault(),+ );+ const onDrop = vi.fn();+ render(+ <DropdownMenu button={{label: 'Help'}}>+ <DropdownMenuItem+ label="Report a bug"+ onClick={() => {}}+ onDragOver={onDragOver}+ onDrop={onDrop}+ />+ </DropdownMenu>,+ );+ const row = screen.getByRole('menuitem', {+ name: 'Report a bug',+ hidden: true,+ });+ fireEvent.dragOver(row);+ fireEvent.drop(row);+ expect(onDragOver).toHaveBeenCalledTimes(1);+ expect(onDrop).toHaveBeenCalledTimes(1);+ });++ it("keeps the row's own role, tab stop and hover focus beside a caller's onPointerMove", async () => {+ const user = userEvent.setup();+ const onPointerMove = vi.fn();+ render(+ <DropdownMenu button={{label: 'Actions'}}>+ <DropdownMenuItem label="Edit" onClick={() => {}} />+ <DropdownMenuItem+ label="Delete"+ onClick={() => {}}+ onPointerMove={onPointerMove}+ />+ </DropdownMenu>,+ );+ await user.click(screen.getByRole('button', {name: /Actions/}));+ const edit = screen.getByRole('menuitem', {name: 'Edit', hidden: true});+ const del = screen.getByRole('menuitem', {name: 'Delete', hidden: true});+ expect(del).toHaveAttribute('tabindex', '-1');+ edit.focus();++ fireEvent.pointerMove(del, {pointerType: 'mouse'});++ // The row still takes the one focus highlight under the mouse, and the+ // caller's handler runs as well.+ expect(del).toHaveFocus();+ expect(onPointerMove).toHaveBeenCalledTimes(1);+ });++ it("a middle click on a link row runs the caller's onAuxClick and still closes the menu", async () => {+ const user = userEvent.setup();+ const onAuxClick = vi.fn();+ render(+ <DropdownMenu button={{label: 'Actions'}}>+ <DropdownMenuItem label="Docs" href="/docs" onAuxClick={onAuxClick} />+ </DropdownMenu>,+ );+ const trigger = screen.getByRole('button', {name: 'Actions'});+ await user.click(trigger);+ const row = screen.getByRole('menuitem', {name: 'Docs', hidden: true});++ fireEvent(+ row,+ new MouseEvent('auxclick', {bubbles: true, cancelable: true, button: 1}),+ );++ expect(onAuxClick).toHaveBeenCalledTimes(1);+ await waitFor(() =>+ expect(trigger).toHaveAttribute('aria-expanded', 'false'),+ );+ });+});+describe('DropdownMenuGroup (compound mode)', () => {it('a focusable node in the heading is not reachable by the arrow keys', async () => {// A heading is not a menu row, so a control inside one lands in thediff --git a/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjsindex 1d3d7de5e5..f74833c215 100644--- a/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs+++ b/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs@@ -8,7 +8,7 @@ export const docs = {displayName: 'Dropdown Menu Item',isHiddenFromOverview: true,description:- 'Helper component for custom item rendering with consistent styling.',+ 'Helper component for custom item rendering with consistent styling. Host attributes and DOM event handlers (aria-*, data-*, id, drag and drop) reach the row root, the element carrying role="menuitem".',playground: {// Standalone DropdownMenuItem has no required props, so the properties-tab// preview renders an empty row without seeded content. Seed a label and@@ -94,7 +94,8 @@ export const docsZh = {name: 'DropdownMenuItem',isHiddenFromOverview: true,displayName: 'Dropdown Menu Item',- description: '用于自定义项渲染的辅助组件,提供一致的样式。',+ description:+ '用于自定义项渲染的辅助组件,提供一致的样式。宿主属性和 DOM 事件处理器(aria-*、data-*、id、拖放)会落在行根元素上,即带 role="menuitem" 的元素。',props: [{name: 'icon',diff --git a/packages/core/src/DropdownMenu/DropdownMenuItem.tsx b/packages/core/src/DropdownMenu/DropdownMenuItem.tsxindex 269706dff6..e4db9cb102 100644--- a/packages/core/src/DropdownMenu/DropdownMenuItem.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenuItem.tsx@@ -15,6 +15,10 @@* creating an invisible button (keyboard access is provided by the parent menu).* With an `href` the root is a real anchor carrying the role.*+ * Host attributes and DOM handlers the caller passes (aria-*, data-*, id, drag+ * and drop, …) reach that row root; the row's own role, tab stop and hover+ * focus are set after them, so a caller cannot overwrite them.+ ** SYNC: When modified, update these files to stay in sync:* - /packages/core/src/DropdownMenu/DropdownMenu.doc.mjs* - /packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs@@ -40,7 +44,7 @@ import {typographyVars,typeScaleVars,} from '../theme/tokens.stylex';-import {mergeProps} from '../utils';+import {composeEventHandlers, mergeProps} from '../utils';import type {BaseProps} from '../BaseProps';import {useDropdownMenuContext} from './DropdownMenuContext';import {focusMenuItemOnHover} from './menuItemHover';@@ -105,9 +109,9 @@ const itemSizeStyles = stylex.create({lg: {},});-export interface DropdownMenuItemProps extends Pick<+export interface DropdownMenuItemProps extends Omit<BaseProps,- 'xstyle' | 'className' | 'style'+ 'role' | 'tabIndex'> {/** Icon to display before the label. */icon?: ReactNode | IconType;@@ -192,6 +196,9 @@ export function DropdownMenuItem({className,style,ref,+ onPointerMove,+ onAuxClick,+ ...rest}: DropdownMenuItemProps) {const ctx = useDropdownMenuContext();const menuSize = ctx?.menuSize ?? 'md';@@ -254,6 +261,7 @@ export function DropdownMenuItem({return (<Item+ {...rest}ref={ref}// A row that navigates IS the link: its root is the application's// anchor, so a modified click, a middle click, copying the address and@@ -262,7 +270,8 @@ export function DropdownMenuItem({as={href != null ? LinkComponent : undefined}role="menuitem"tabIndex={isDisabled ? undefined : -1}- onPointerMove={handlePointerMove}+ // The row's own handling runs first, then the caller's.+ onPointerMove={composeEventHandlers(handlePointerMove, onPointerMove)}{...pressable}startContent={icon@@ -276,7 +285,7 @@ export function DropdownMenuItem({description={description}endContent={endContent}onClick={handleClick}- onAuxClick={handleAuxClick}+ onAuxClick={composeEventHandlers(handleAuxClick, onAuxClick)}href={href}target={target}rel={rel}