Skip to content

Commit b32c80c

Browse files
committed
fix: retain outer Escape behavior for inline submenus
1 parent a7c05d2 commit b32c80c

2 files changed

Lines changed: 105 additions & 6 deletions

File tree

‎src/hooks/useAccessibility.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export default function useAccessibility({
5555
const handleKeyDown = (event: KeyboardEvent) => {
5656
switch (event.keyCode) {
5757
case ESC: {
58-
// Let a nested menu handle Escape before closing the outer popup.
58+
// Portaled popup submenus own Escape. Inline mode submenus retain
59+
// outer close/focus, even when popup containers are customized.
5960
const overlay = overlayRef?.current;
6061
const overlayElement = overlay?.list || overlay;
6162
const rootMenu = overlayElement?.matches?.('[role="menu"]')
@@ -65,7 +66,17 @@ export default function useAccessibility({
6566
event.target instanceof Element
6667
? event.target.closest('[role="menu"]')
6768
: null;
68-
if (rootMenu && targetMenu && targetMenu !== rootMenu) {
69+
const inlineSubmenu =
70+
rootMenu?.contains(targetMenu) &&
71+
Array.from(targetMenu?.classList || []).some((className) =>
72+
className.endsWith('-inline'),
73+
);
74+
if (
75+
rootMenu &&
76+
targetMenu &&
77+
targetMenu !== rootMenu &&
78+
!inlineSubmenu
79+
) {
6980
break;
7081
}
7182
handleCloseMenuAndReturnFocus();

‎tests/useAccessibility.test.tsx‎

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { act, renderHook } from '@testing-library/react';
1+
import * as React from 'react';
2+
import Menu from '@rc-component/menu';
3+
import { act, fireEvent, render, renderHook } from '@testing-library/react';
24
import useAccessibility from '../src/hooks/useAccessibility';
35

46
it('closes without consuming Tab when the overlay ref is omitted', () => {
@@ -29,9 +31,14 @@ it('closes without consuming Tab when the overlay ref is omitted', () => {
2931
}
3032
});
3133

32-
it.each([false, true])(
33-
'leaves a portaled nested menu Escape to its owner (wrapped: %s)',
34-
(wrapped) => {
34+
it.each([
35+
[false, false],
36+
[true, false],
37+
[false, true],
38+
[true, true],
39+
])(
40+
'leaves popup nested menu Escape to its owner (wrapped: %s, contained: %s)',
41+
(wrapped, contained) => {
3542
const trigger = document.createElement('button');
3643
const wrapper = document.createElement('div');
3744
const root = document.createElement('ul');
@@ -43,6 +50,7 @@ it.each([false, true])(
4350
submenu.appendChild(child);
4451
wrapper.appendChild(root);
4552
document.body.append(trigger, wrapper, submenu);
53+
if (contained) root.appendChild(submenu);
4654
const onOpenChange = jest.fn();
4755
const { unmount } = renderHook(() =>
4856
useAccessibility({
@@ -76,3 +84,83 @@ it.each([false, true])(
7684
}
7785
},
7886
);
87+
88+
it.each([false, true])(
89+
'closes for an inline nested menu (wrapped: %s)',
90+
(wrapped) => {
91+
const trigger = document.createElement('button');
92+
const wrapper = document.createElement('div');
93+
const root = document.createElement('ul');
94+
const submenu = document.createElement('ul');
95+
const child = document.createElement('li');
96+
root.setAttribute('role', 'menu');
97+
submenu.setAttribute('role', 'menu');
98+
submenu.className = 'custom-menu-sub custom-menu-inline';
99+
child.tabIndex = -1;
100+
submenu.appendChild(child);
101+
root.appendChild(submenu);
102+
wrapper.appendChild(root);
103+
document.body.append(trigger, wrapper);
104+
const onOpenChange = jest.fn();
105+
const { unmount } = renderHook(() =>
106+
useAccessibility({
107+
open: true,
108+
triggerRef: { current: trigger },
109+
onOpenChange,
110+
overlayRef: { current: wrapped ? wrapper : { list: root } },
111+
}),
112+
);
113+
try {
114+
child.focus();
115+
act(() =>
116+
child.dispatchEvent(
117+
new KeyboardEvent('keydown', { keyCode: 27, bubbles: true }),
118+
),
119+
);
120+
expect(onOpenChange).toHaveBeenCalledTimes(1);
121+
expect(onOpenChange).toHaveBeenCalledWith(false);
122+
expect(document.activeElement).toBe(trigger);
123+
} finally {
124+
unmount();
125+
trigger.remove();
126+
wrapper.remove();
127+
}
128+
},
129+
);
130+
131+
it('closes and restores focus from an actual rc-menu inline child', () => {
132+
const onOpenChange = jest.fn();
133+
const Fixture = () => {
134+
const triggerRef = React.useRef<HTMLButtonElement>(null);
135+
const overlayRef = React.useRef<any>(null);
136+
useAccessibility({ open: true, triggerRef, overlayRef, onOpenChange });
137+
return (
138+
<>
139+
<button ref={triggerRef}>Trigger</button>
140+
<Menu
141+
ref={overlayRef}
142+
mode="inline"
143+
defaultOpenKeys={['sub']}
144+
items={[
145+
{
146+
key: 'sub',
147+
label: 'Sub',
148+
children: [{ key: 'child', label: 'Child' }],
149+
},
150+
]}
151+
/>
152+
</>
153+
);
154+
};
155+
const { getByText, unmount } = render(<Fixture />);
156+
try {
157+
const child = getByText('Child').closest('li');
158+
child.focus();
159+
fireEvent.keyDown(child, { key: 'Escape', keyCode: 27, which: 27 });
160+
expect(onOpenChange).toHaveBeenCalledTimes(1);
161+
expect(onOpenChange).toHaveBeenCalledWith(false);
162+
expect(getByText('Trigger')).toHaveFocus();
163+
} finally {
164+
unmount();
165+
}
166+
});

0 commit comments

Comments
 (0)