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 main when content changes #2221

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/app/content/__snapshots__/routes.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ Array [
overflow: visible;
}

.c4:focus-visible {
outline: none;
}

.c4 .highlight {
position: relative;
z-index: 1;
Expand Down
1 change: 1 addition & 0 deletions src/app/content/components/Page/PageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,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
4 changes: 4 additions & 0 deletions src/app/content/components/Page/PageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default styled(MainContent)`
${contentTextStyle}
overflow: visible;

:focus-visible {
outline: none;
}

@media screen {
flex: 1;
display: flex;
Expand Down
16 changes: 5 additions & 11 deletions src/app/content/components/Page/scrollToTopOrHashManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement } from '@openstax/types/lib.dom';
import { scrollTo } from '../../../domUtils';
import * as selectNavigation from '../../../navigation/selectors';
import { AppState } from '../../../types';
import { assertWindow, memoizeStateToProps, resetTabIndex } from '../../../utils';
import { assertWindow, memoizeStateToProps } from '../../../utils';
import { isSearchScrollTarget } from '../../search/guards';
import { selectedResult } from '../../search/selectors';
import * as select from '../../selectors';
Expand All @@ -26,20 +26,14 @@ const scrollToTarget = (container: HTMLElement | null, hash: string) => {
}
};

const scrollToTargetOrTop = (container: HTMLElement | null, hash: string) => {
const scrollToTargetOrTop = (container: HTMLElement | null, hash: string, previous: boolean) => {
if (getScrollTarget(container, hash)) {
scrollToTarget(container, hash);
} else {
scrollToTop();
} else if (previous) {
assertWindow().scrollTo(0, 0);
}
};

const scrollToTop = () => {
const window = assertWindow();
resetTabIndex(window.document);
window.scrollTo(0, 0);
};

const getScrollTarget = (container: HTMLElement | null, hash: string): HTMLElement | null => {
return container && typeof(window) !== 'undefined' && hash
? container.querySelector(`[id="${hash.replace(/^#/, '')}"]`)
Expand All @@ -59,7 +53,7 @@ const scrollToTopOrHashManager = (
return;
}
if (previous?.page !== current.page) {
scrollToTargetOrTop(container, current.hash);
scrollToTargetOrTop(container, current.hash, Boolean(previous));
} else if (previous?.hash !== current.hash) {
scrollToTarget(container, current.hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ exports[`text resizer does not render if textSize is null 1`] = `
>
<input
aria-label="Search this book"
autoFocus={true}
className="c18"
data-testid="mobile-search-input"
onChange={[Function]}
Expand Down Expand Up @@ -1722,7 +1721,6 @@ exports[`text resizer opens menu when clicking menu button 1`] = `
>
<input
aria-label="Search this book"
autoFocus={true}
className="c27"
data-testid="mobile-search-input"
onChange={[Function]}
Expand Down
Loading
Loading