Skip to content

Commit

Permalink
Just do the focus in ComponentDidUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Nov 12, 2024
1 parent b7aab4b commit 0642746
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
38 changes: 7 additions & 31 deletions src/app/components/MainContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface Props {
className?: string;
dangerouslySetInnerHTML?: { __html: string; };
textSize?: TextResizerValue;
doFocus?: boolean;
}
// tslint:disable-next-line:variable-name
const ContentStyles = styled(({ textSize, ...props }) => <DynamicContentStyles {...props} />)`
Expand All @@ -30,34 +29,6 @@ const ContentStyles = styled(({ textSize, ...props }) => <DynamicContentStyles {
}
`;

function InnerContent({
book,
children,
doFocus,
...props
}: React.PropsWithChildren<Omit<Props, 'className'>>) {
React.useEffect(
() => {
if (window && doFocus) {
window.document.querySelector('main')?.focus();
window.scrollTo(0, 0);
}
},
[doFocus]
);

return (
<ContentStyles
id={MAIN_CONTENT_ID}
book={book}
tabIndex={-1}
{...props}
>
{children}
</ContentStyles>
);
}

// tslint:disable-next-line:variable-name
const MainContent = React.forwardRef<HTMLDivElement, React.PropsWithChildren<Props>>(
({book, children, className, ...props}, ref) => <Consumer>
Expand All @@ -66,9 +37,14 @@ const MainContent = React.forwardRef<HTMLDivElement, React.PropsWithChildren<Pro
className={className}
tabIndex={-1}
>
<InnerContent book={book} {...props}>
<ContentStyles
id={MAIN_CONTENT_ID}
book={book}
tabIndex={-1}
{...props}
>
{children}
</InnerContent>
</ContentStyles>
</main>}
</Consumer>
);
Expand Down
4 changes: 1 addition & 3 deletions src/app/content/components/Page/PageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default class PageComponent extends Component<PagePropTypes> {
private scrollToTopOrHashManager = stubScrollToTopOrHashManager;
private processing: Array<Promise<void>> = [];
private componentDidUpdateCounter = 0;
private doFocus = false;

public getTransformedContent = () => {
const {book, page, services} = this.props;
Expand Down Expand Up @@ -90,7 +89,6 @@ export default class PageComponent extends Component<PagePropTypes> {
// per rerender. componentDidUpdate is called multiple times when user navigates quickly.
const runId = this.getRunId();

this.doFocus = true;
// If page has changed, call postProcess that will remove old and attach new listerns and start mathjax typesetting.
if (prevProps.page !== this.props.page) {
this.postProcess();
Expand All @@ -99,6 +97,7 @@ export default class PageComponent extends Component<PagePropTypes> {
// Wait for the mathjax promise set by postProcess from previous or current componentDidUpdate call.
await Promise.all(this.processing);

this.container.current?.focus();
this.scrollToTopOrHashManager(prevProps.scrollToTopOrHash, this.props.scrollToTopOrHash);

const searchHighlightsChanged = !isEqual(
Expand Down Expand Up @@ -173,7 +172,6 @@ export default class PageComponent extends Component<PagePropTypes> {
ref={this.container}
dangerouslySetInnerHTML={{ __html: html}}
textSize={this.props.textSize}
doFocus={this.doFocus}
/>
{this.props.children}
</React.Fragment>;
Expand Down

0 comments on commit 0642746

Please sign in to comment.