Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/babel/traverse-7.23.2
Browse files Browse the repository at this point in the history
  • Loading branch information
staxly[bot] authored Jul 18, 2024
2 parents 6ec7f6b + 3dc573f commit 9224cff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/app/content/components/ContentWarning.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ describe('ContentWarning', () => {
renderToDom(<TestContainer><ContentWarning book={dummyBook} /></TestContainer>);

expect(services.osWebLoader.getBookFromId).toBeCalledWith(dummyBook.id);

await act(() => new Promise((resolve) => setTimeout(resolve, 1)));

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

expect(b).toBeTruthy();
// Exercises the when-focus-is-already-in-the-modal branch
b!.focus();
act(() => ReactTestUtils.Simulate.click(b!));
expect(root?.querySelector('button')).toBeFalsy();
});
Expand Down
19 changes: 18 additions & 1 deletion src/app/content/components/ContentWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Modal from './Modal';
import theme from '../../theme';
import Cookies from 'js-cookie';
import { useTrapTabNavigation } from '../../reactUtils';
import { assertDocument } from '../../utils';

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

React.useEffect(() => ref.current?.focus(), []);
// Demand focus
React.useEffect(
() => {
const document = assertDocument();
const grabFocus = () => {
if (!ref.current?.contains(document.activeElement)) {
ref.current?.focus();
}
};

grabFocus();
document.body.addEventListener('focusin', grabFocus);

return () => document.body.removeEventListener('focusin', grabFocus);
},
[]
);

useTrapTabNavigation(ref);

Expand Down
5 changes: 2 additions & 3 deletions src/app/reactUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ export function createTrapTab(...elements: HTMLElement[]) {
return;
}
const trapTab = createTrapTab(el);
const document = assertDocument();

document.body.addEventListener('keydown', trapTab, true);
el.addEventListener('keydown', trapTab, true);

return () => document.body.removeEventListener('keydown', trapTab, true);
return () => el.removeEventListener('keydown', trapTab, true);
},
[ref, otherDep]
);
Expand Down

0 comments on commit 9224cff

Please sign in to comment.