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
48 changes: 30 additions & 18 deletions packages/react-native-web/src/exports/Modal/ModalFocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
* LICENSE file in the root directory of this source tree.
*/

import { type ReactNode, useEffect, useLayoutEffect, useRef } from 'react';
import {
type ReactNode,
useEffect,
useLayoutEffect,
useRef,
useState
} from 'react';

import canUseDOM from '../../modules/canUseDom';
import type { HostNode, Nullable } from '../../types';
Expand Down Expand Up @@ -91,6 +97,10 @@ const ModalFocusTrap = ({
trapFocusInProgress: false,
lastFocusedElement: null
});
// Read during render, before a child with autoFocus takes focus
const [lastFocusedElementOutsideTrap] = useState(() =>
canUseDOM ? document.activeElement : null
);

useLayoutEffect(() => {
if (canUseDOM && active) {
Expand Down Expand Up @@ -158,23 +168,25 @@ const ModalFocusTrap = ({

// To be fully compliant with WCAG we need to refocus element that triggered opening modal
// after closing it
useEffect(function () {
if (canUseDOM) {
const lastFocusedElementOutsideTrap = document.activeElement;
return function () {
if (
lastFocusedElementOutsideTrap &&
document.contains(lastFocusedElementOutsideTrap) &&
// Don't take focus back if it moved somewhere else while the modal
// was closing, e.g. to another modal
(document.activeElement == null ||
document.activeElement === document.body)
) {
UIManager.focus(lastFocusedElementOutsideTrap as HTMLElement);
}
};
}
}, []);
useEffect(
function () {
if (canUseDOM) {
return function () {
if (
lastFocusedElementOutsideTrap &&
document.contains(lastFocusedElementOutsideTrap) &&
// Don't take focus back if it moved somewhere else while the modal
// was closing, e.g. to another modal
(document.activeElement == null ||
document.activeElement === document.body)
) {
UIManager.focus(lastFocusedElementOutsideTrap as HTMLElement);
}
};
}
},
[lastFocusedElementOutsideTrap]
);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ describe('components/Modal', () => {
expect(document.activeElement).toBe(modalTrigger);
});

test('focus is brought back to the element that triggered modal after closing, when its content took focus on mount', () => {
const modal = (visible: boolean) => (
<>
<a data-testid="trigger" href="#" />
<Modal visible={visible}>
<input autoFocus data-testid="inside" />
</Modal>
</>
);

const { getByTestId, rerender } = render(modal(false));
getByTestId('trigger').focus();

rerender(modal(true));
expect(document.activeElement).toBe(getByTestId('inside'));

rerender(modal(false));
expect(document.activeElement).toBe(getByTestId('trigger'));
});

test('focus is brought back to the body when element that triggered modal is removed from the DOM after closing modal', () => {
const { rerender } = render(
<>
Expand Down