Skip to content

Commit

Permalink
Trap backwards navigation from initial focus on header in focus-trapp…
Browse files Browse the repository at this point in the history
…ed modals (#851)

* Add unit test for reverse navigation from initial focused header, FocusTrapper

* Update unit tests for FocusTrapper, handle backwards navigation from initially focused header

* Handle focus leaving non-focusable header element backwards in FocusTrapper
  • Loading branch information
stalgiag authored Dec 12, 2023
1 parent 918acdd commit 8215cb9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
8 changes: 7 additions & 1 deletion client/components/common/BasicModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ const BasicModal = ({
}, []);

return (
<FocusTrapper isActive={show} trappedElId={`focus-trapped-${id}`}>
<FocusTrapper
isActive={show}
initialFocusRef={
initialFocusRef?.current ? initialFocusRef : headerRef
}
trappedElId={`focus-trapped-${id}`}
>
<Modal
show={show}
id={`focus-trapped-${id}`}
Expand Down
27 changes: 20 additions & 7 deletions client/components/common/FocusTrapper/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React from 'react';
import PropTypes from 'prop-types';
import { useEffect, useRef } from 'react';

const FocusTrapper = ({ children, isActive, trappedElId }) => {
const FocusTrapper = ({ children, isActive, initialFocusRef, trappedElId }) => {
const focusableElsRef = useRef([]);

const updateFocusableElements = () => {
if (focusableElsRef.current.length > 0) return;

Expand Down Expand Up @@ -41,13 +40,22 @@ const FocusTrapper = ({ children, isActive, trappedElId }) => {
const firstFocusableEl = focusableEls[1];
// Last focusable element is before the blank 'after' div
const lastFocusableEl = focusableEls[focusableEls.length - 2];

if (event.shiftKey && document.activeElement === firstFocusableEl) {
// When SHIFT + TAB is pressed and the active element is the first focusable element
if (
event.shiftKey &&
(document.activeElement === firstFocusableEl ||
document.activeElement === focusableEls[0] ||
document.activeElement === initialFocusRef.current)
) {
lastFocusableEl.focus();
event.preventDefault();
} else if (
}
// When TAB is pressed and the active element is the last focusable element
else if (
!event.shiftKey &&
document.activeElement === lastFocusableEl
(document.activeElement === lastFocusableEl ||
document.activeElement ===
focusableEls[focusableEls.length - 1])
) {
firstFocusableEl.focus();
event.preventDefault();
Expand All @@ -60,9 +68,11 @@ const FocusTrapper = ({ children, isActive, trappedElId }) => {
document.addEventListener('keydown', trapFocus);
} else {
document.removeEventListener('keydown', trapFocus);
focusableElsRef.current = [];
}

return () => {
focusableElsRef.current = [];
document.removeEventListener('keydown', trapFocus);
};
}, [isActive]);
Expand All @@ -73,7 +83,10 @@ const FocusTrapper = ({ children, isActive, trappedElId }) => {
FocusTrapper.propTypes = {
children: PropTypes.node.isRequired,
isActive: PropTypes.bool.isRequired,
trappedElId: PropTypes.string.isRequired
trappedElId: PropTypes.string.isRequired,
initialFocusRef: PropTypes.shape({
current: PropTypes.object
})
};

export default FocusTrapper;
19 changes: 15 additions & 4 deletions client/tests/FocusTrapper.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { render, fireEvent, act } from '@testing-library/react';
import FocusTrapper from '../components/common/FocusTrapper';

describe('FocusTrapper', () => {
let trappedDiv;
let trappedDiv, initialFocusRef;

beforeEach(() => {
trappedDiv = document.createElement('div');
Expand All @@ -19,8 +19,20 @@ describe('FocusTrapper', () => {
});

const renderEls = async () => {
initialFocusRef = React.createRef();
return render(
<FocusTrapper isActive={true} trappedElId="trapped-div">
<FocusTrapper
isActive={true}
initialFocusRef={initialFocusRef}
trappedElId="trapped-div"
>
<h1
className="modal-header"
tabIndex="-1"
ref={initialFocusRef}
>
Modal Header
</h1>
<button>Click Me</button>
<a href="www">Link</a>
</FocusTrapper>,
Expand Down Expand Up @@ -67,11 +79,10 @@ describe('FocusTrapper', () => {

const container = document.getElementById('trapped-div');

const firstFocusable = container.querySelector('button');
const lastFocusable = container.querySelector('a');

act(() => {
firstFocusable.focus();
initialFocusRef.current.focus();
});

act(() => {
Expand Down

0 comments on commit 8215cb9

Please sign in to comment.