Skip to content

Commit

Permalink
fix(theme-classic): make focused link outlined with JS disabled (#6505)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Feb 3, 2022
1 parent caa9d28 commit 01c6f15
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useTitleFormatter,
useAlternatePageUtils,
useThemeConfig,
keyboardFocusedClassName,
} from '@docusaurus/theme-common';
import {useLocation} from '@docusaurus/router';

Expand Down Expand Up @@ -101,6 +102,9 @@ export default function LayoutHead(props: Props): JSX.Element {
<title>{pageTitle}</title>
<meta property="og:title" content={pageTitle} />
<meta name="twitter:card" content="summary_large_image" />
{/* The keyboard focus class name need to be applied when SSR so links
are outlined when JS is disabled */}
<body className={keyboardFocusedClassName} />
</Head>

{/* image can override the default image */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {useEffect} from 'react';

import './styles.css';

export const keyboardFocusedClassName = 'navigation-with-keyboard';

// This hook detect keyboard focus indicator to not show outline for mouse users
// Inspired by https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
export default function useKeyboardNavigation(): void {
useEffect(() => {
const keyboardFocusedClassName = 'navigation-with-keyboard';

function handleOutlineStyles(e: MouseEvent | KeyboardEvent) {
if (e.type === 'keydown' && (e as KeyboardEvent).key === 'Tab') {
document.body.classList.add(keyboardFocusedClassName);
Expand Down
5 changes: 4 additions & 1 deletion packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export {
} from './utils/tabGroupChoiceUtils';

export {default as useHideableNavbar} from './hooks/useHideableNavbar';
export {default as useKeyboardNavigation} from './hooks/useKeyboardNavigation';
export {
default as useKeyboardNavigation,
keyboardFocusedClassName,
} from './hooks/useKeyboardNavigation';
export {default as usePrismTheme} from './hooks/usePrismTheme';
export {default as useLockBodyScroll} from './hooks/useLockBodyScroll';
export {default as useWindowSize} from './hooks/useWindowSize';
Expand Down

0 comments on commit 01c6f15

Please sign in to comment.