Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Checkbox] Control still has the blue outline on focus, even wih focus-visible package applied #2234

Closed
la55u opened this issue Oct 13, 2020 · 18 comments
Labels
not stale Type: Bug 🐛 Something isn't working

Comments

@la55u
Copy link

la55u commented Oct 13, 2020

Describe the bug

The checkbox square has a blue outline color when focused with mouse click, despite the focus-visible package being used.

To reproduce

  1. npm i focus-visible
  2. add import "focus-visible/dist/focus-visible" to _app.js
  3. add a <Checkbox/> element

Expected behavior

The blue outline should only be visible when using keyboard navigation, just like with Button etc.

System information

"@chakra-ui/core": "^1.0.0-rc.5",
"next": "9.5.3",

@cschroeter
Copy link
Member

It is also the case if you use :focus-visible which is now natively supported in Chrome

@stale
Copy link

stale bot commented Dec 25, 2020

Hi!
This issue hasn't seen any activity recently. We close inactive issues after 35 days to manage the volume of issues we receive.
If we missed this issue or you want to keep it open, please reply here. That will reset the timer and allow more time for this issue to be addressed before it is closed.

@stale stale bot added the stale label Dec 25, 2020
@rheng001
Copy link

Facing the same issue

@stale stale bot removed the stale label Dec 27, 2020
@with-heart with-heart added not stale Type: Bug 🐛 Something isn't working labels Dec 27, 2020
@kdfriedman
Copy link

kdfriedman commented Mar 4, 2021

I was recently able to make this work as expected:

.chakra-checkbox__input[data-focus-visible-added]
  + .chakra-checkbox__control[data-focus] {
  box-shadow: 0 0 0 3px rgb(66 153 225 / 60%);
}

I'm using the data-focus-visible-added attribute which is appended to the previous sibling input element. If you combine the input and span elements, the styling works as expected for tabbed focus vs click focus. As a caveat, I have the focus-visible polyfill installed as well.

@lingyun-z
Copy link

lingyun-z commented Apr 15, 2021

I added these in my global theme and it works as expected.

*:focus {
  box-shadow: none !important;
}

*[data-focus] {
  box-shadow: none !important;
}

@mwmcode
Copy link
Contributor

mwmcode commented Apr 21, 2021

Safari (the new IE) is supposed to support focus-visible soon 🤞 can i use

The world's most valuable company is always late to the party focus-visible, <input type="date" />, flex gap, BroadcastChannel....😐

I needed to overwrite checkbox boxshadow so I ended up modifying @kdfriedman 's solution to

/* using tailwind */
.chakra-radiobox__control[data-focus],
.chakra-checkbox__control[data-focus] {
  @apply ring-2 ring-red-300 !important;
}

@Blagoj5
Copy link

Blagoj5 commented Jul 24, 2021

Another workaround can be:

<Checkbox
  size="sm"
  colorScheme="primary"
  defaultIsChecked
  css={`
    > span:first-of-type {
      box-shadow: unset;
    }
  `}
  >
  Random text
</Checkbox>

@Brettm12345
Copy link

Brettm12345 commented Aug 20, 2021

The focus visible package just adds the polyfill for the focus visible css selector which indicates keyboard focus. In order to remove the box shadow you gotta do this

<Checkbox _focusVisible={{ shadow: 'outline'}} _focus={{ shadow: 'none'}} />

to apply this globally add the following to your global styles

'.js-focus-visible :focus:not(.focus-visible)': {
  shadow: 'none',
},

@anilanar
Copy link
Contributor

anilanar commented Oct 7, 2021

Perhaps what's not clear is whether if this is intended or just a bug, or under which circumstances this is a bug.

@verekia
Copy link

verekia commented Oct 19, 2021

Thanks @Brettm12345.

If you disable Chakra's CSS Reset, you do need to add this style manually.

@Nic-Performio
Copy link

Im having the same issue with switch, radio button and checkbox. However it does work on button. I have tried the suggestions above but nothing has worked. Has anyone else faced this?

When trying some of these work arounds I was playing around in dev tools and it looks like it works when forcing element state but does not work without doing that.

@ishowta
Copy link
Contributor

ishowta commented Oct 22, 2021

In chakra component such as Checkbox and Radio, input element is hidden and styled by custom elements. They have the data-focus attribute, so the following code probably work.

const GlobalStyles = css`
  /*
    https://github.com/WICG/focus-visible#2-update-your-css

    This will hide the focus indicator if the element receives focus via the mouse,
    but it will still show up on keyboard focus.
  */
  .js-focus-visible :focus:not(.focus-visible),
  .js-focus-visible :focus:not(.focus-visible) + [data-focus] {
    outline: none;
    box-shadow: none;
  }
`

@verekia
Copy link

verekia commented Oct 23, 2021

For those who would rather not keep the fix in their code, I've included it as a theme extension into a library of mine of Chakra helpers. Not particularly advocating using random people's libraries in your projects, but just saying it's there ;)

@Nic-Performio
Copy link

In chakra component such as Checkbox and Radio, input element is hidden and styled by custom elements. They have the data-focus attribute, so the following code probably work.

const GlobalStyles = css`
  /*
    https://github.com/WICG/focus-visible#2-update-your-css

    This will hide the focus indicator if the element receives focus via the mouse,
    but it will still show up on keyboard focus.
  */
  .js-focus-visible :focus:not(.focus-visible),
  .js-focus-visible :focus:not(.focus-visible) + [data-focus] {
    outline: none;
    box-shadow: none;
  }
`

Thanks, this worked! :)

@segunadebayo
Copy link
Member

For anyone still having this issue. Kindly use the workaround suggested above.

@ehynds
Copy link

ehynds commented Apr 6, 2022

Another option if you want to set this globally in your theme:

const theme = extendTheme({
  components: {
    Checkbox: {
      baseStyle: {
        control: {
          _focus: {
            boxShadow: 'none',
          },
        },
      },
    }
  }
});

@lcswillems
Copy link

lcswillems commented May 31, 2022

In the release logs of Chakra v2, I see a mention to a data-focus-visible-disabled attribute:

When using the focus-visible package, you can now opt out of the automatic focus style override by adding data-focus-visible-disabled to a DOM element.

Is it useful to solve this issue? Is there any documentation or example about it?

@DavidLabaz
Copy link

I added these in my global theme and it works as expected.

*:focus {
  box-shadow: none !important;
}

*[data-focus] {
  box-shadow: none !important;
}

dude, thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not stale Type: Bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests