question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Only show outlines when user is keyboard navigating

See original GitHub issue

Feature 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
segunadebayocommented, Sep 13, 2020

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

5reactions
liamrosscommented, Mar 26, 2021

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keyboard-Only Focus Styles | CSS-Tricks
But those focus styles are most useful when tabbing or otherwise navigating with a keyboard, and less so when they are triggered by...
Read more >
Enable :focus only on keyboard use (or tab press)
Removing outline is terrible for accessibility! Ideally, the focus ring shows up only when the user intends to use the keyboard.
Read more >
Focus Style CSS for Keyboard Navigation | Rob Marshall
A solution to keyboard only navigation with a focus state for buttons and links. Purely HTML and CSS. An easy alternative to :focus-visible....
Read more >
Improve keyboard navigation with outline styling ... - Webflow
With focused (keyboard) state, the outline styling of an element will only be visible when a user navigates the page with their keyboard....
Read more >
focus-visible - CSS: Cascading Style Sheets - MDN Web Docs
(Many browsers show a "focus ring" by default in this case.) ... focus indicator based on the user's input modality (mouse vs. keyboard)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found