From 06427467bf52f9f33be8352d5d2fd8ac6f0e68cd Mon Sep 17 00:00:00 2001 From: Roy Johnson Date: Tue, 12 Nov 2024 15:22:36 -0600 Subject: [PATCH] Just do the focus in ComponentDidUpdate --- src/app/components/MainContent.tsx | 38 ++++--------------- .../content/components/Page/PageComponent.tsx | 4 +- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/src/app/components/MainContent.tsx b/src/app/components/MainContent.tsx index 90484a3f15..556a0c9544 100644 --- a/src/app/components/MainContent.tsx +++ b/src/app/components/MainContent.tsx @@ -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 }) => )` @@ -30,34 +29,6 @@ const ContentStyles = styled(({ textSize, ...props }) => >) { - React.useEffect( - () => { - if (window && doFocus) { - window.document.querySelector('main')?.focus(); - window.scrollTo(0, 0); - } - }, - [doFocus] - ); - - return ( - - {children} - - ); -} - // tslint:disable-next-line:variable-name const MainContent = React.forwardRef>( ({book, children, className, ...props}, ref) => @@ -66,9 +37,14 @@ const MainContent = React.forwardRef - + {children} - + } ); diff --git a/src/app/content/components/Page/PageComponent.tsx b/src/app/content/components/Page/PageComponent.tsx index 2598c67fcd..a0db60e079 100644 --- a/src/app/content/components/Page/PageComponent.tsx +++ b/src/app/content/components/Page/PageComponent.tsx @@ -37,7 +37,6 @@ export default class PageComponent extends Component { private scrollToTopOrHashManager = stubScrollToTopOrHashManager; private processing: Array> = []; private componentDidUpdateCounter = 0; - private doFocus = false; public getTransformedContent = () => { const {book, page, services} = this.props; @@ -90,7 +89,6 @@ export default class PageComponent extends Component { // 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(); @@ -99,6 +97,7 @@ export default class PageComponent extends Component { // 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( @@ -173,7 +172,6 @@ export default class PageComponent extends Component { ref={this.container} dangerouslySetInnerHTML={{ __html: html}} textSize={this.props.textSize} - doFocus={this.doFocus} /> {this.props.children} ;