Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus on TOC when TOC is opened #2102

Merged
merged 6 commits into from
Feb 13, 2024
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
2 changes: 1 addition & 1 deletion src/app/content/components/SidebarControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const CloseToCAndMobileMenuButton = styled((props) => {
`;

// tslint:disable-next-line:variable-name
export const TOCControl = ({ message, children, ...props }: React.PropsWithChildren<InnerProps>) =>
const TOCControl = ({ message, children, ...props }: React.PropsWithChildren<InnerProps>) =>
<OpenButton
aria-label={useIntl().formatMessage({ id: message })}
{...props}
Expand Down
37 changes: 35 additions & 2 deletions src/app/content/components/TableOfContents/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React from 'react';
import { unmountComponentAtNode } from 'react-dom';
import { act as reactDomAct } from 'react-dom/test-utils';
import renderer from 'react-test-renderer';
import ConnectedTableOfContents, { TableOfContents } from '.';
import createTestStore from '../../../../test/createTestStore';
import { book as archiveBook, page, shortPage } from '../../../../test/mocks/archiveLoader';
import { mockCmsBook } from '../../../../test/mocks/osWebLoader';
import { renderToDom } from '../../../../test/reactutils';
import TestContainer from '../../../../test/TestContainer';
import * as reactUtils from '../../../reactUtils';
import { AppState, Store } from '../../../types';
import { assertWindow } from '../../../utils';
import * as actions from '../../actions';
import { initialState } from '../../reducer';
import { formatBookData } from '../../utils';
import * as domUtils from '../../utils/domUtils';
import * as reactUtils from '../../../reactUtils';

const book = formatBookData(archiveBook, mockCmsBook);

Expand Down Expand Up @@ -76,13 +77,45 @@ describe('TableOfContents', () => {
renderer.act(() => {
store.dispatch(actions.closeToc());
});

expect(component.root.findByType(TableOfContents).props.isOpen).toBe(false);

renderer.act(() => {
store.dispatch(actions.openToc());
});
expect(component.root.findByType(TableOfContents).props.isOpen).toBe(true);
});

it('focuses when opening', () => {
jest.spyOn(reactUtils, 'useMatchMobileMediumQuery')
.mockReturnValue(true);

const {root} = renderToDom(<TestContainer store={store}>
<ConnectedTableOfContents />
</TestContainer>);
const sb = root.querySelector('[data-testid="toc"]')!;
const firstTocItem = sb.querySelector('ol > li a, old > li summary') as HTMLElement;
const focusSpy = jest.spyOn(firstTocItem as any, 'focus');

reactDomAct(() => {
store.dispatch(actions.closeToc());
});
reactDomAct(() => {
sb?.dispatchEvent(new Event('transitionend'));
});

expect(focusSpy).not.toHaveBeenCalled();

reactDomAct(() => {
store.dispatch(actions.openToc());
});
reactDomAct(() => {
sb?.dispatchEvent(new Event('transitionend'));
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the trick was that the event listener isn't added until the thing re-renders with the open prop, so you need to have the event dispatch in a separate act call after you update the open state


expect(focusSpy).toHaveBeenCalled();
});

it('resets toc on navigate', () => {
const dispatchSpy = jest.spyOn(store, 'dispatch');

Expand All @@ -105,7 +138,7 @@ describe('TableOfContents', () => {

const event = document.createEvent('UIEvents');
event.initEvent('scroll', true, false);
renderer.act(() => {
reactDomAct(() => {
assertWindow().dispatchEvent(event);
});

Expand Down
22 changes: 21 additions & 1 deletion src/app/content/components/TableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,32 @@ const SidebarBody = React.forwardRef<
HTMLElement,
React.ComponentProps<typeof SidebarPaneBody>
>((props, ref) => {
const mRef = ref as MutableRefObject<HTMLElement>;

React.useEffect(
() => {
const firstItemInToc = mRef?.current?.querySelector(
'ol > li a, old > li summary'
) as HTMLElement;
const el = mRef.current;
const transitionListener = () => {
firstItemInToc?.focus();
};

if (props.isTocOpen) {
el?.addEventListener('transitionend', transitionListener);
}

return () => el?.removeEventListener('transitionend', transitionListener);
},
[props.isTocOpen, mRef]
);

return (
<React.Fragment>
{typeof window !== 'undefined' && (
<TabTrapper
mRef={ref as MutableRefObject<HTMLElement>}
mRef={mRef}
isTocOpen={props.isTocOpen}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const HighlightsHelpInfo = () => {

return <Wrapper data-analytics-region='Mobile MH help info'>
<Message />
<PlainButton onClick={dismiss} data-analytics-label='close'>
<PlainButton onClick={dismiss} data-analytics-label='close' aria-label='dismiss'>
<CloseIcon />
</PlainButton>
</Wrapper>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ exports[`HighlightsHelpInfo matches snapshot when showed 1`] = `
}
/>
<button
aria-label="dismiss"
className="c1"
data-analytics-label="close"
onClick={[Function]}
Expand Down
Loading