-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(v2): remove focus outline from mouse users (#3626)
* refactor(v2): remove focus outline from mouse users * Improvements * Cleanup
- Loading branch information
Showing
5 changed files
with
60 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/docusaurus-theme-classic/src/theme/hooks/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
body:not(.navigation-with-keyboard) *:not(input):focus { | ||
outline: none; | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/docusaurus-theme-classic/src/theme/hooks/useKeyboardNavigation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 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 {useEffect} from 'react'; | ||
|
||
import './styles.css'; | ||
|
||
// 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 | ||
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); | ||
} | ||
|
||
if (e.type === 'mousedown') { | ||
document.body.classList.remove(keyboardFocusedClassName); | ||
} | ||
} | ||
|
||
document.addEventListener('keydown', handleOutlineStyles); | ||
document.addEventListener('mousedown', handleOutlineStyles); | ||
|
||
return () => { | ||
document.body.classList.remove(keyboardFocusedClassName); | ||
document.removeEventListener('keydown', handleOutlineStyles); | ||
document.removeEventListener('mousedown', handleOutlineStyles); | ||
}; | ||
}, []); | ||
} | ||
|
||
export default useKeyboardNavigation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters