Only show outlines when user is keyboard navigating
See original GitHub issueFeature request
Is your feature request related to a problem? Please describe.
Let me preface this by saying I’m a huge proponent of accessibility across the web. However, aesthetically, it can be nice to hide some of it until it is needed.
Describe the solution you’d like
I’d like some sort of prop or setting to prevent focus outlines from showing up until the user has specifically navigated using their keyboard.
Describe alternatives you’ve considered
Alternatives would be to leave it as is (obviously), but there aren’t any direct alternatives that would satisfy this feature request as far as I can tell.
Additional context
I currently implement a solution that resembles this:
First, have a Provider which wraps keyboard nav listeners (<kbd>Tab</kbd>). These enable and disable a isKeyboardNavigating
boolean depending on whether the user has pressed tab or clicked last. The provider provides the boolean for whether the user is tabbing, as well as a manual way to set it (for example if something like a menu is open where nav is by arrow keys rather than just Tab).
Second, change the theme value for outline
whenever the user is not keyboard navigating to 'none'
.
// This sits inside of the parent wrapper (in this case `_app.tsx` in a Next.js app)
function Theme({ children }: PropsWithChildren<{}>) {
const { isKeyboardNavigating } = useKeyboardNav();
// Override theme.shadows.outline with 'none' when user is not keyboard navigating.
const customTheme = useMemo(
() => ({
...theme,
shadows: {
...theme.shadows,
outline: isKeyboardNavigating ? theme.shadows.outline : 'none',
},
}),
[keyboard],
);
return (
<ChakraProvider theme={customTheme}>
<CSSReset />
{children}
</ChakraProvider>
);
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top GitHub Comments
Hey @liamross,
We’ve made this possible in the upcoming v1 release. All you need to do is to install the
focus-visible
package and set it up as mentioned here:https://github.com/chakra-ui/chakra-ui/blob/develop/packages/css-reset/README.md
Cheers
@MikeMajara Same instructions can be found here:
https://www.npmjs.com/package/@chakra-ui/css-reset#disabling-border-for-non-keyboard-interactions
The original link might be dead because I believe the CSS reset is now included in the standard Chakra root provider