Skip to content

Commit b09b649

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/ip-1.1.9
2 parents 8f2417c + 3dc573f commit b09b649

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/app/content/components/ContentWarning.spec.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ describe('ContentWarning', () => {
3535
renderToDom(<TestContainer><ContentWarning book={dummyBook} /></TestContainer>);
3636

3737
expect(services.osWebLoader.getBookFromId).toBeCalledWith(dummyBook.id);
38-
3938
await act(() => new Promise((resolve) => setTimeout(resolve, 1)));
4039

4140
const root = document?.body;
4241
const b = root?.querySelector('button');
4342

4443
expect(b).toBeTruthy();
44+
// Exercises the when-focus-is-already-in-the-modal branch
45+
b!.focus();
4546
act(() => ReactTestUtils.Simulate.click(b!));
4647
expect(root?.querySelector('button')).toBeFalsy();
4748
});

src/app/content/components/ContentWarning.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Modal from './Modal';
99
import theme from '../../theme';
1010
import Cookies from 'js-cookie';
1111
import { useTrapTabNavigation } from '../../reactUtils';
12+
import { assertDocument } from '../../utils';
1213

1314
// tslint:disable-next-line
1415
const WarningDiv = styled.div`
@@ -37,7 +38,23 @@ function WarningDivWithTrap({
3738
}) {
3839
const ref = React.useRef<HTMLDivElement>(null);
3940

40-
React.useEffect(() => ref.current?.focus(), []);
41+
// Demand focus
42+
React.useEffect(
43+
() => {
44+
const document = assertDocument();
45+
const grabFocus = () => {
46+
if (!ref.current?.contains(document.activeElement)) {
47+
ref.current?.focus();
48+
}
49+
};
50+
51+
grabFocus();
52+
document.body.addEventListener('focusin', grabFocus);
53+
54+
return () => document.body.removeEventListener('focusin', grabFocus);
55+
},
56+
[]
57+
);
4158

4259
useTrapTabNavigation(ref);
4360

src/app/reactUtils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ export function createTrapTab(...elements: HTMLElement[]) {
9999
return;
100100
}
101101
const trapTab = createTrapTab(el);
102-
const document = assertDocument();
103102

104-
document.body.addEventListener('keydown', trapTab, true);
103+
el.addEventListener('keydown', trapTab, true);
105104

106-
return () => document.body.removeEventListener('keydown', trapTab, true);
105+
return () => el.removeEventListener('keydown', trapTab, true);
107106
},
108107
[ref, otherDep]
109108
);

0 commit comments

Comments
 (0)