Skip to content

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

TypeScript

fix(menu): make hover move the single highlight in dropdown and context menus (#4496)

Cindy Zhangfef7dca467b6
diff --git a/.changeset/menu-hover-focus-single-highlight.md b/.changeset/menu-hover-focus-single-highlight.md
new file mode 100644
index 0000000000..9147cd1862
--- /dev/null
+++ b/.changeset/menu-hover-focus-single-highlight.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] DropdownMenu/ContextMenu: mouse hover now moves the highlight instead of adding a second one, so keyboard focus and pointer hover share a single highlighted item (#4493)
+@cixzhang
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index dd1465c2fb..ff15738751 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -781,4 +781,64 @@ describe('DropdownMenu keyboard access for menuitemradio/menuitemcheckbox (#3829
screen.getByRole('menuitemradio', {name: 'Oldest', hidden: true}),
).toHaveFocus();
});
+
+ it('moves focus to the item the mouse hovers, keeping a single highlight', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuItem label="Edit" onClick={() => {}} />
+ <DropdownMenuItem label="Duplicate" onClick={() => {}} />
+ <DropdownMenuItem label="Delete" onClick={() => {}} />
+ </DropdownMenu>,
+ );
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ // Keyboard focus starts on the first item.
+ const edit = screen.getByRole('menuitem', {name: 'Edit', hidden: true});
+ const del = screen.getByRole('menuitem', {name: 'Delete', hidden: true});
+ edit.focus();
+ expect(edit).toHaveFocus();
+
+ // A mouse hover over another item moves focus to it, so the single
+ // focus-driven highlight follows the pointer instead of leaving two.
+ fireEvent.pointerMove(del, {pointerType: 'mouse'});
+ expect(del).toHaveFocus();
+ expect(edit).not.toHaveFocus();
+ });
+
+ it('does not move focus on hover for a disabled item', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuItem label="Edit" onClick={() => {}} />
+ <DropdownMenuItem label="Delete" isDisabled onClick={() => {}} />
+ </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});
+ edit.focus();
+
+ fireEvent.pointerMove(del, {pointerType: 'mouse'});
+ expect(edit).toHaveFocus();
+ });
+
+ it('does not move focus for a non-mouse (touch) pointer', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuItem label="Edit" onClick={() => {}} />
+ <DropdownMenuItem label="Delete" onClick={() => {}} />
+ </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});
+ edit.focus();
+
+ fireEvent.pointerMove(del, {pointerType: 'touch'});
+ expect(edit).toHaveFocus();
+ });
});
diff --git a/packages/core/src/DropdownMenu/DropdownMenuCheckboxItem.tsx b/packages/core/src/DropdownMenu/DropdownMenuCheckboxItem.tsx
index a58d8341f6..88f1065808 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuCheckboxItem.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuCheckboxItem.tsx
@@ -23,11 +23,12 @@
* checkmark uses the `check` icon from the active theme's icon registry.
*/
-import {useCallback, type ReactNode} from 'react';
+import {useCallback, type PointerEvent, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {Icon, renderIconSlot, type IconType} from '../Icon';
import {Item} from '../Item';
import {useDropdownMenuContext} from './DropdownMenuContext';
+import {focusMenuItemOnHover} from './menuItemHover';
import {
colorVars,
spacingVars,
@@ -48,11 +49,6 @@ const styles = stylex.create({
default: 'transparent',
':focus': colorVars['--color-overlay-hover'],
},
- ':hover': {
- '@media (hover: hover)': {
- backgroundColor: colorVars['--color-overlay-hover'],
- },
- },
cursor: 'pointer',
outline: 'none',
},
@@ -194,12 +190,18 @@ export function DropdownMenuCheckboxItem({
}
}, [isDisabled, onChange, value, hasCloseOnSelect, ctx]);
+ const handlePointerMove = useCallback(
+ (e: PointerEvent<HTMLElement>) => focusMenuItemOnHover(e, isDisabled),
+ [isDisabled],
+ );
+
return (
<Item
{...rest}
role="menuitemcheckbox"
aria-checked={value}
tabIndex={isDisabled ? undefined : -1}
+ onPointerMove={handlePointerMove}
marker={
<span
aria-hidden="true"
diff --git a/packages/core/src/DropdownMenu/DropdownMenuItem.tsx b/packages/core/src/DropdownMenu/DropdownMenuItem.tsx
index 1c56bbf11d..0119a2598f 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuItem.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuItem.tsx
@@ -23,7 +23,7 @@
* - /packages/cli/templates/blocks/components/DropdownMenu/ (showcase blocks)
*/
-import {useCallback, type ReactNode} from 'react';
+import {useCallback, type PointerEvent, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {renderIconSlot, type IconType} from '../Icon';
import {Item} from '../Item';
@@ -36,6 +36,7 @@ import {
import {mergeProps} from '../utils';
import type {BaseProps} from '../BaseProps';
import {useDropdownMenuContext} from './DropdownMenuContext';
+import {focusMenuItemOnHover} from './menuItemHover';
import {themeProps} from '../utils/themeProps';
const menuItemStyles = stylex.create({
@@ -52,11 +53,6 @@ const menuItemStyles = stylex.create({
default: 'transparent',
':focus': colorVars['--color-overlay-hover'],
},
- ':hover': {
- '@media (hover: hover)': {
- backgroundColor: colorVars['--color-overlay-hover'],
- },
- },
border: 'none',
cursor: 'pointer',
textAlign: 'start',
@@ -133,10 +129,16 @@ export function DropdownMenuItem({
ctx?.closeMenu();
}, [isDisabled, onClick, ctx]);
+ const handlePointerMove = useCallback(
+ (e: PointerEvent<HTMLElement>) => focusMenuItemOnHover(e, isDisabled),
+ [isDisabled],
+ );
+
return (
<Item
role="menuitem"
tabIndex={isDisabled ? undefined : -1}
+ onPointerMove={handlePointerMove}
startContent={
icon
? renderIconSlot(icon, {size: 'sm', color: 'secondary'})
diff --git a/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx b/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
index 3f42e47608..795691e804 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
@@ -21,11 +21,12 @@
* has no registry glyph).
*/
-import {useCallback, type ReactNode} from 'react';
+import {useCallback, type PointerEvent, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {renderIconSlot, type IconType} from '../Icon';
import {Item} from '../Item';
import {useDropdownMenuContext} from './DropdownMenuContext';
+import {focusMenuItemOnHover} from './menuItemHover';
import {
colorVars,
spacingVars,
@@ -46,11 +47,6 @@ const styles = stylex.create({
default: 'transparent',
':focus': colorVars['--color-overlay-hover'],
},
- ':hover': {
- '@media (hover: hover)': {
- backgroundColor: colorVars['--color-overlay-hover'],
- },
- },
cursor: 'pointer',
outline: 'none',
},
@@ -188,12 +184,18 @@ export function DropdownMenuRadioItem({
}
}, [isDisabled, groupCtx, value, menuCtx]);
+ const handlePointerMove = useCallback(
+ (e: PointerEvent<HTMLElement>) => focusMenuItemOnHover(e, isDisabled),
+ [isDisabled],
+ );
+
return (
<Item
{...rest}
role="menuitemradio"
aria-checked={isChecked}
tabIndex={isDisabled ? undefined : -1}
+ onPointerMove={handlePointerMove}
marker={
<span
aria-hidden="true"
diff --git a/packages/core/src/DropdownMenu/menuItemHover.ts b/packages/core/src/DropdownMenu/menuItemHover.ts
new file mode 100644
index 0000000000..0f96b7e047
--- /dev/null
+++ b/packages/core/src/DropdownMenu/menuItemHover.ts
@@ -0,0 +1,39 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+/**
+ * @file menuItemHover.ts
+ * @output Exports focusMenuItemOnHover
+ * @position Shared helper for DropdownMenu/ContextMenu item components
+ *
+ * Menus keep a single highlighted item by using DOM focus as the sole
+ * highlight source. Mouse hover moves focus onto the pointed-at item so the
+ * one focus highlight follows the pointer — instead of a separate `:hover`
+ * background leaving the keyboard-focused item highlighted at the same time.
+ *
+ * SYNC: When modified, update the item components that use it:
+ * - /packages/core/src/DropdownMenu/DropdownMenuItem.tsx
+ * - /packages/core/src/DropdownMenu/DropdownMenuCheckboxItem.tsx
+ * - /packages/core/src/DropdownMenu/DropdownMenuRadioItem.tsx
+ */
+
+import type {PointerEvent} from 'react';
+
+/**
+ * Move focus to a menu item as the pointer moves over it, so hover and
+ * keyboard navigation share a single focus-driven highlight. Only reacts to a
+ * real mouse (not touch/pen, which have no hover), and skips disabled items.
+ */
+export function focusMenuItemOnHover(
+ e: PointerEvent<HTMLElement>,
+ isDisabled?: boolean,
+): void {
+ if (isDisabled || e.pointerType !== 'mouse') {
+ return;
+ }
+ const el = e.currentTarget;
+ if (el !== el.ownerDocument.activeElement) {
+ el.focus();
+ }
+}