mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-25 15:07:17 +02:00
refactor: extract useSkipToContent() (#7269)
This commit is contained in:
parent
fe286f1833
commit
3bef88232f
3 changed files with 50 additions and 29 deletions
|
@ -5,40 +5,14 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, {useRef} from 'react';
|
import React from 'react';
|
||||||
import {useHistory} from '@docusaurus/router';
|
|
||||||
import Translate from '@docusaurus/Translate';
|
import Translate from '@docusaurus/Translate';
|
||||||
import {useLocationChange} from '@docusaurus/theme-common';
|
import {useSkipToContent} from '@docusaurus/theme-common';
|
||||||
|
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
function programmaticFocus(el: HTMLElement) {
|
|
||||||
el.setAttribute('tabindex', '-1');
|
|
||||||
el.focus();
|
|
||||||
el.removeAttribute('tabindex');
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function SkipToContent(): JSX.Element {
|
export default function SkipToContent(): JSX.Element {
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const {containerRef, handleSkip} = useSkipToContent();
|
||||||
const {action} = useHistory();
|
|
||||||
const handleSkip = (e: React.MouseEvent<HTMLAnchorElement>) => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
const targetElement: HTMLElement | null =
|
|
||||||
document.querySelector('main:first-of-type') ||
|
|
||||||
document.querySelector('.main-wrapper');
|
|
||||||
|
|
||||||
if (targetElement) {
|
|
||||||
programmaticFocus(targetElement);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
useLocationChange(({location}) => {
|
|
||||||
if (containerRef.current && !location.hash && action === 'PUSH') {
|
|
||||||
programmaticFocus(containerRef.current);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={containerRef} role="region">
|
<div ref={containerRef} role="region">
|
||||||
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
|
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
|
||||||
|
|
|
@ -57,6 +57,8 @@ export {
|
||||||
useLayoutDocsSidebar,
|
useLayoutDocsSidebar,
|
||||||
} from './utils/docsUtils';
|
} from './utils/docsUtils';
|
||||||
|
|
||||||
|
export {useSkipToContent} from './utils/a11yUtils';
|
||||||
|
|
||||||
export {useTitleFormatter} from './utils/generalUtils';
|
export {useTitleFormatter} from './utils/generalUtils';
|
||||||
|
|
||||||
export {usePluralForm} from './utils/usePluralForm';
|
export {usePluralForm} from './utils/usePluralForm';
|
||||||
|
|
45
packages/docusaurus-theme-common/src/utils/a11yUtils.ts
Normal file
45
packages/docusaurus-theme-common/src/utils/a11yUtils.ts
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type React from 'react';
|
||||||
|
import {useCallback, useRef} from 'react';
|
||||||
|
import {useHistory} from '@docusaurus/router';
|
||||||
|
import {useLocationChange} from './useLocationChange';
|
||||||
|
import {ThemeClassNames} from './ThemeClassNames';
|
||||||
|
|
||||||
|
function programmaticFocus(el: HTMLElement) {
|
||||||
|
el.setAttribute('tabindex', '-1');
|
||||||
|
el.focus();
|
||||||
|
el.removeAttribute('tabindex');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSkipToContent(): {
|
||||||
|
containerRef: React.RefObject<HTMLDivElement>;
|
||||||
|
handleSkip: (e: React.MouseEvent<HTMLAnchorElement>) => void;
|
||||||
|
} {
|
||||||
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
const {action} = useHistory();
|
||||||
|
const handleSkip = useCallback((e: React.MouseEvent<HTMLAnchorElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const targetElement: HTMLElement | null =
|
||||||
|
document.querySelector('main:first-of-type') ||
|
||||||
|
document.querySelector(`.${ThemeClassNames.wrapper.main}`);
|
||||||
|
|
||||||
|
if (targetElement) {
|
||||||
|
programmaticFocus(targetElement);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useLocationChange(({location}) => {
|
||||||
|
if (containerRef.current && !location.hash && action === 'PUSH') {
|
||||||
|
programmaticFocus(containerRef.current);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {containerRef, handleSkip};
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue