Skip to content

Commit

Permalink
fix(v2): make more accessible skip link
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed Feb 2, 2021
1 parent ffb2d29 commit 4b7ef71
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions packages/docusaurus-theme-classic/src/theme/SkipToContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,43 @@
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import React, {useRef, useEffect} from 'react';
import {useLocation} from '@docusaurus/router';

import styles from './styles.module.css';

function programmaticFocus(el) {
el.setAttribute('tabindex', '-1');
el.focus();
setTimeout(() => el.removeAttribute('tabindex'), 1000);
}

function SkipToContent(): JSX.Element {
const handleSkip = (e: React.KeyboardEvent<HTMLButtonElement>) => {
if (e.keyCode !== 13) {
return;
}
const containerRef = useRef(null);
const location = useLocation();

(document.activeElement as HTMLElement).blur();
const handleSkip = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();

const firstMainElement = document.querySelector('main:first-of-type');
const targetElement =
document.querySelector('main:first-of-type') ||
document.querySelector('.main-wrapper');

if (firstMainElement) {
firstMainElement.scrollIntoView();
if (targetElement) {
programmaticFocus(targetElement);
}
};

useEffect(() => {
programmaticFocus(containerRef.current);
}, [location.pathname]);

return (
<nav aria-label="Skip navigation links">
<button
type="button"
tabIndex={0}
className={styles.skipToContent}
onKeyDown={handleSkip}>
<div ref={containerRef}>
<a href="#main" className={styles.skipToContent} onClick={handleSkip}>
Skip to main content
</button>
</nav>
</a>
</div>
);
}

Expand Down

0 comments on commit 4b7ef71

Please sign in to comment.