Skip to content

Commit

Permalink
extract useSkipToContent()
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Apr 29, 2022
1 parent 3b1170e commit a7254ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import React, {useRef} from 'react';
import {useHistory} from '@docusaurus/router';
import React from 'react';
import Translate from '@docusaurus/Translate';
import {useLocationChange} from '@docusaurus/theme-common';
import {useSkipToContent} from '@docusaurus/theme-common';

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 {
const containerRef = useRef<HTMLDivElement>(null);
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);
}
});

const {containerRef, handleSkip} = useSkipToContent();
return (
<div ref={containerRef} role="region">
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export {
useLayoutDocsSidebar,
} from './utils/docsUtils';

export {useSkipToContent} from './utils/a11yUtils';

export {useTitleFormatter} from './utils/generalUtils';

export {usePluralForm} from './utils/usePluralForm';
Expand Down
41 changes: 41 additions & 0 deletions packages/docusaurus-theme-common/src/utils/a11yUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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';

function programmaticFocus(el: HTMLElement) {
el.setAttribute('tabindex', '-1');
el.focus();
el.removeAttribute('tabindex');
}

export function useSkipToContent() {
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('.main-wrapper');

if (targetElement) {
programmaticFocus(targetElement);
}
}, []);

useLocationChange(({location}) => {
if (containerRef.current && !location.hash && action === 'PUSH') {
programmaticFocus(containerRef.current);
}
});

return {containerRef, handleSkip};
}

0 comments on commit a7254ec

Please sign in to comment.