Skip to content

Commit

Permalink
Move focus effect to SidebarBody
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Jan 17, 2024
1 parent 7e1841a commit e5a1f21
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/app/content/components/TableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { CloseToCAndMobileMenuButton, TOCBackButton, TOCCloseButton } from '../S
import { Header, HeaderText, SidebarPaneBody } from '../SidebarPane';
import { LeftArrow, TimesIcon } from '../Toolbar/styled';
import * as Styled from './styled';
import { assertWindow } from '../../../utils';
import { createTrapTab, useMatchMobileMediumQuery } from '../../../reactUtils';

interface SidebarProps {
Expand Down Expand Up @@ -52,16 +51,33 @@ function TabTrapper({
return null;
}



// tslint:disable-next-line:variable-name
const SidebarBody = React.forwardRef<
HTMLElement,
React.ComponentProps<typeof SidebarPaneBody>
>((props, ref) => {
const mRef = ref as MutableRefObject<HTMLElement>;

React.useEffect(
() => {
if (props.isTocOpen) {
const firstItemInToc = mRef?.current?.querySelector(
'ol > li a, old > li summary'
) as HTMLElement;

setTimeout(() => firstItemInToc?.focus(), 100);
}
},
[props.isTocOpen, mRef]
);

return (
<React.Fragment>
{typeof window !== 'undefined' && (
<TabTrapper
mRef={ref as MutableRefObject<HTMLElement>}
mRef={mRef}
isTocOpen={props.isTocOpen}
/>
)}
Expand Down Expand Up @@ -199,19 +215,6 @@ export class TableOfContents extends Component<SidebarProps> {
expandCurrentChapter(this.activeSection.current);
this.scrollToSelectedPage();
}
if (this.props.isOpen && !prevProps.isOpen) {
const firstItemInToc = this.sidebar.current?.querySelector(
'ol > li a, old > li summary'
) as HTMLElement;

// Something else affects focus; wait for it to pass
assertWindow().setTimeout(
() => {
firstItemInToc?.focus();
},
100
);
}
}

public componentWillUnmount() {
Expand Down

0 comments on commit e5a1f21

Please sign in to comment.