Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu trigger=click-hover updates for UX and accessibility #5403

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 29 additions & 25 deletions packages/@docs/demos/src/demos/core/Menu/Menu.demo.navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import { MantineDemo } from '@mantinex/demo';
import { DemoMenuItems } from './_menu-items';

const code = `
import { Menu, Group } from '@mantine/core';
import { Group, Menu } from '@mantine/core';

function Demo() {
return (
<Group>
<Menu trigger="click-hover" loop={false} withinPortal={false} trapFocus={false} menuItemTabIndex={0}>
{/* ... menu items */}
</Menu>
<Menu trigger="click-hover" loop={false} withinPortal={false} trapFocus={false} menuItemTabIndex={0}>
{/* ... menu items */}
</Menu>
{Array(4)
mg-aceik marked this conversation as resolved.
Show resolved Hide resolved
.fill(0)
.map((e, i) => (
<Menu
key={i}
trigger="click-hover"
loop={false}
withinPortal={false}
trapFocus={false}
menuItemTabIndex={0}
>
<DemoMenuItems />
mg-aceik marked this conversation as resolved.
Show resolved Hide resolved
</Menu>
))}
</Group>
);
}
Expand All @@ -23,24 +31,20 @@ function Demo() {
function Demo() {
return (
<Group>
<Menu
trigger="click-hover"
loop={false}
withinPortal={false}
trapFocus={false}
menuItemTabIndex={0}
>
<DemoMenuItems />
</Menu>
<Menu
trigger="click-hover"
loop={false}
withinPortal={false}
trapFocus={false}
menuItemTabIndex={0}
>
<DemoMenuItems />
</Menu>
{Array(4)
.fill(0)
.map((e, i) => (
<Menu
key={i}
trigger="click-hover"
loop={false}
withinPortal={false}
trapFocus={false}
menuItemTabIndex={0}
>
<DemoMenuItems />
</Menu>
))}
</Group>
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@mantine/core/src/components/Menu/Menu.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface MenuContext {
unstyled: boolean | undefined;
getStyles: GetStylesApi<MenuFactory>;
menuItemTabIndex: -1 | 0 | undefined;
openedViaClick: boolean;
setOpenedViaClick: (value: boolean) => void;
}

export const [MenuContextProvider, useMenuContext] = createSafeContext<MenuContext>(
Expand Down
24 changes: 21 additions & 3 deletions packages/@mantine/core/src/components/Menu/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ describe('@mantine/core/Menu', () => {

it('toggles menu when target is clicked when trigger="click"', async () => {
render(<TestContainer />);

expectClosed();
await userEvent.click(getControl());

expectOpened();
await userEvent.click(getControl());
expectClosed();
Expand All @@ -77,14 +75,34 @@ describe('@mantine/core/Menu', () => {
it('toggles menu when target is hovered when trigger="hover"', async () => {
render(<TestContainer trigger="hover" />);
expectClosed();

await userEvent.hover(getControl());
expectOpened();
await userEvent.unhover(getControl());
expectClosed();
});

it('toggles menu when target is hovered when trigger="click-hover"', async () => {
render(<TestContainer trigger="click-hover" />);
expectClosed();
await userEvent.hover(getControl());
expectOpened();
await userEvent.unhover(getControl());
expectClosed();
});

it('menu always stays open when target is clicked when trigger="click-hover"', async () => {
render(<TestContainer trigger="click-hover" />);
expectClosed();
await userEvent.click(getControl());
expectOpened();
await userEvent.click(getControl());
expectOpened();
await userEvent.hover(getControl());
expectOpened();
await userEvent.unhover(getControl());
expectOpened();
});

it('supports defaultOpened prop', () => {
render(<TestContainer defaultOpened />);
expectOpened();
Expand Down
6 changes: 5 additions & 1 deletion packages/@mantine/core/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { useDidUpdate, useUncontrolled } from '@mantine/hooks';
import {
ExtendComponent,
Expand Down Expand Up @@ -141,9 +141,11 @@ export function Menu(_props: MenuProps) {
finalValue: false,
onChange,
});
const [openedViaClick, setOpenedViaClick] = useState(false);

const close = () => {
setOpened(false);
setOpenedViaClick(false);
_opened && onClose?.();
};

Expand Down Expand Up @@ -180,6 +182,8 @@ export function Menu(_props: MenuProps) {
getItemIndex,
hovered,
setHovered,
openedViaClick,
setOpenedViaClick,
closeOnItemClick,
closeDropdown: trigger === 'click' ? close : closeDropdown,
openDropdown: trigger === 'click' ? open : openDropdown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,29 @@ export const MenuTarget = forwardRef<HTMLElement, MenuTargetProps>((props, ref)

const ctx = useMenuContext();

const onClick = createEventHandler(
children.props.onClick,
() => (ctx.trigger === 'click' || ctx.trigger === 'click-hover') && ctx.toggleDropdown()
);
const onClick = createEventHandler(children.props.onClick, () => {
if (ctx.trigger === 'click') {
ctx.toggleDropdown();
} else if (ctx.trigger === 'click-hover') {
ctx.setOpenedViaClick(true);
if (!ctx.opened) {
ctx.openDropdown();
}
}
});

const onMouseEnter = createEventHandler(
children.props.onMouseEnter,
() => (ctx.trigger === 'hover' || ctx.trigger === 'click-hover') && ctx.openDropdown()
);

const onMouseLeave = createEventHandler(
children.props.onMouseLeave,
() => (ctx.trigger === 'hover' || ctx.trigger === 'click-hover') && ctx.closeDropdown()
);
const onMouseLeave = createEventHandler(children.props.onMouseLeave, () => {
if (ctx.trigger === 'hover') {
ctx.closeDropdown();
} else if (ctx.trigger === 'click-hover' && !ctx.openedViaClick) {
ctx.closeDropdown();
}
});

return (
<Popover.Target refProp={refProp} popupType="menu" ref={ref} {...others}>
Expand Down