Skip to content

Commit

Permalink
Focus after content is rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Jul 29, 2024
1 parent 07cca7c commit e7d70e6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
40 changes: 33 additions & 7 deletions src/app/components/MainContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { State } from '../content/types';
import { MAIN_CONTENT_ID } from '../context/constants';
import { Consumer } from '../context/SkipToContent';
import { mergeRefs } from '../utils';
import { assertWindow } from '../utils';
import DynamicContentStyles from './DynamicContentStyles';

interface Props {
book: State['book'];
className?: string;
dangerouslySetInnerHTML?: { __html: string; };
textSize?: TextResizerValue;
doFocus?: boolean;
}
// tslint:disable-next-line:variable-name
const ContentStyles = styled(({ textSize, ...props }) => <DynamicContentStyles {...props} />)`
Expand All @@ -29,6 +31,35 @@ const ContentStyles = styled(({ textSize, ...props }) => <DynamicContentStyles {
}
`;

function InnerContent({
book,
children,
doFocus,
...props
}: React.PropsWithChildren<Omit<Props, 'className'>>) {
const window = assertWindow();

React.useEffect(
() => {
if (doFocus) {
window.document.querySelector('main')?.focus();
}
},
[doFocus, window]
);

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 @@ -37,14 +68,9 @@ const MainContent = React.forwardRef<HTMLDivElement, React.PropsWithChildren<Pro
className={className}
tabIndex={-1}
>
<ContentStyles
id={MAIN_CONTENT_ID}
book={book}
tabIndex={-1}
{...props}
>
<InnerContent book={book} {...props}>
{children}
</ContentStyles>
</InnerContent>
</main>}
</Consumer>
);
Expand Down
6 changes: 4 additions & 2 deletions src/app/content/components/Page/PageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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 @@ -89,6 +90,7 @@ 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 @@ -97,8 +99,6 @@ 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.scrollToTopOrHashManager(prevProps.scrollToTopOrHash, this.props.scrollToTopOrHash);

const searchHighlightsChanged = !isEqual(
prevProps.searchHighlights.searchResults,
this.props.searchHighlights.searchResults
Expand All @@ -123,6 +123,7 @@ export default class PageComponent extends Component<PagePropTypes> {
});

lazyResources.checkLazyResources();
this.scrollToTopOrHashManager(prevProps.scrollToTopOrHash, this.props.scrollToTopOrHash);
}

public onHighlightSelect: HighlightUpdateOptions['onSelect'] = (selectedHighlight) => {
Expand Down Expand Up @@ -171,6 +172,7 @@ 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
2 changes: 0 additions & 2 deletions src/app/content/components/Page/scrollToTopOrHashManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ const scrollToTargetOrTop = (container: HTMLElement | null, hash: string, previo

const scrollToTop = () => {
const window = assertWindow();
const document = window.document;

document.querySelector('main')?.focus();
window.scrollTo(0, 0);
};

Expand Down

0 comments on commit e7d70e6

Please sign in to comment.