Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mosaic-menu-pointer-focus-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/mosaic': patch
---

Stop showing keyboard focus rings on menu items when a menu is opened and hovered with the mouse. Focus rings still appear when navigating the menu with the keyboard.
17 changes: 16 additions & 1 deletion packages/mosaic/src/primitives/menu/menu-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
arrow,
autoUpdate,
type ElementProps,
flip,
FloatingNode,
FloatingTree,
Expand Down Expand Up @@ -112,8 +113,21 @@ function MenuInner(props: MenuProps) {
const click = useClick(floatingContext, {
event: 'mousedown',
toggle: !isNested,
ignoreMouse: isNested,
ignoreMouse: true,
});
const mousePress = useMemo<ElementProps>(
() => ({
reference: {
onPointerDown(event: React.PointerEvent) {
if (isNested || event.button !== 0 || (event.pointerType !== 'mouse' && event.pointerType !== 'pen')) {
return;
}
floatingContext.onOpenChange(!open, event.nativeEvent, 'click');
},
},
}),
[floatingContext, isNested, open],
);
const baseRole = useRole(floatingContext, { role: 'menu' });
// `useRole` decides submenu-ness from the floating tree alone, so a menu inside a popover gets
// `role="menuitem"` on its trigger with no parent menu to be an item of. `isNested` is the real answer.
Expand Down Expand Up @@ -143,6 +157,7 @@ function MenuInner(props: MenuProps) {
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([
hover,
click,
mousePress,
role,
dismiss,
listNavigation,
Expand Down
21 changes: 21 additions & 0 deletions packages/mosaic/src/primitives/menu/menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ describe('Menu', () => {
expect(screen.getByText('Actions')).toHaveAttribute('data-open', '');
});

it('lets a mouse press focus the trigger as it opens', async () => {
const user = userEvent.setup();
render(
<Menu.Root>
<Menu.Trigger>Actions</Menu.Trigger>
<Menu.Positioner>
<Menu.Popup>
<Menu.Item label='Cut'>Cut</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Root>,
);
const onFocus = vi.fn();
screen.getByText('Actions').addEventListener('focus', onFocus);

await user.pointer({ keys: '[MouseLeft>]', target: screen.getByText('Actions') });

expect(onFocus).toHaveBeenCalled();
expect(screen.getByText('Cut')).toBeInTheDocument();
});

it('closes on trigger click when open', async () => {
const user = userEvent.setup();
render(
Expand Down
Loading