Skip to content

Commit ed76a23

Browse files
committed
fix: try tab target when wrapped menu cannot receive focus
1 parent e6c87ed commit ed76a23

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

‎src/hooks/useAccessibility.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ export default function useAccessibility({
3636
const activeElement = document.activeElement;
3737
overlay.focus(options);
3838
if (document.activeElement === activeElement) {
39-
const focusTarget = (overlay.querySelector?.('[role="menu"]') ??
40-
overlay.querySelector?.('[tabindex]')) as HTMLElement | null;
41-
focusTarget?.focus(options);
39+
for (const selector of ['[role="menu"]', '[tabindex]']) {
40+
const focusTarget = overlay.querySelector?.(
41+
selector,
42+
) as HTMLElement | null;
43+
focusTarget?.focus(options);
44+
if (document.activeElement !== activeElement) {
45+
break;
46+
}
47+
}
4248
}
4349

4450
const focused = document.activeElement !== activeElement;

‎tests/basic.test.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,49 @@ describe('dropdown', () => {
506506
expect(document.activeElement.className).toContain('my-button');
507507
});
508508

509+
it.each(['missing', 'unfocusable'])(
510+
'focuses a tab target when the wrapped menu is %s',
511+
async (menuState) => {
512+
jest.useFakeTimers();
513+
try {
514+
const { container, baseElement } = render(
515+
<Dropdown
516+
trigger={['click']}
517+
overlay={
518+
<div>
519+
{menuState === 'unfocusable' && <div role="menu" />}
520+
<button tabIndex={0} className="custom-target">
521+
action
522+
</button>
523+
</div>
524+
}
525+
>
526+
<button className="my-button">open</button>
527+
</Dropdown>,
528+
);
529+
const trigger =
530+
container.querySelector<HTMLButtonElement>('.my-button');
531+
trigger.focus();
532+
fireEvent.click(trigger);
533+
await waitForTime();
534+
535+
const event = new KeyboardEvent('keydown', {
536+
keyCode: 9,
537+
cancelable: true,
538+
});
539+
act(() => {
540+
window.dispatchEvent(event);
541+
});
542+
expect(document.activeElement).toBe(
543+
baseElement.querySelector('.custom-target'),
544+
);
545+
expect(event.defaultPrevented).toBe(true);
546+
} finally {
547+
jest.useRealTimers();
548+
}
549+
},
550+
);
551+
509552
it('support Menu expandIcon', async () => {
510553
const props = {
511554
overlay: (

‎tests/useAccessibility.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { act, renderHook } from '@testing-library/react';
2+
import useAccessibility from '../src/hooks/useAccessibility';
3+
4+
it('closes without consuming Tab when the overlay ref is omitted', () => {
5+
const trigger = document.createElement('button');
6+
document.body.appendChild(trigger);
7+
const onOpenChange = jest.fn();
8+
const { unmount } = renderHook(() =>
9+
useAccessibility({
10+
open: true,
11+
triggerRef: { current: trigger },
12+
onOpenChange,
13+
}),
14+
);
15+
try {
16+
const event = new KeyboardEvent('keydown', {
17+
keyCode: 9,
18+
cancelable: true,
19+
});
20+
act(() => {
21+
window.dispatchEvent(event);
22+
});
23+
expect(event.defaultPrevented).toBe(false);
24+
expect(onOpenChange).toHaveBeenCalledWith(false);
25+
expect(document.activeElement).toBe(trigger);
26+
} finally {
27+
unmount();
28+
trigger.remove();
29+
}
30+
});

0 commit comments

Comments
 (0)