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

[Switch] .Mui-focusVisible is set on a wrong element #15

Closed
michaldudak opened this issue Nov 2, 2022 · 4 comments
Closed

[Switch] .Mui-focusVisible is set on a wrong element #15

michaldudak opened this issue Nov 2, 2022 · 4 comments
Labels
bug 🐛 Something doesn't work component: switch This is the name of the generic UI component, not the React module!

Comments

@michaldudak
Copy link
Member

michaldudak commented Nov 2, 2022

The Mui-focusVisible class is applied to the Unstyled Switch's root element, even though it's not a focused element. The real DOM focus is placed on the input inside the root. In this case, the .Mui-focusVisible and :focus-visible selectors don't point to the same element. It can be problematic when we want to remove the useIsFocusVisible hook. It could also be surprising for developers who wish to rely on the native property instead of the workaround useIsFocusVisible offers.

One possible solution would be to replace input type="checkbox" with input type="hidden" (or input type="checkbox" hidden) and make the root element focusable. It would require handling clicks and keypresses by the component (instead of delegating this to the native checkbox).

The Material UI component is also affected by this.

@trizotti
Copy link

trizotti commented Jan 6, 2023

Hello, @michaldudak ! I was trying to test the possible solution from your suggestion and opened a draft PR (#35744) (ignore the messy code, it's just a experiment):

  1. I changed the input to hidden;
  2. I allowed the root element (which already receives the Mui-focusVisible class) to be focusable;
  3. I set the handle of focus, clicks and keyboard interactions directly to the root element of the component;

Current result:

Click, keyboard interaction and focus are fine. The component works as normal;

What's missing?

The onChange event won't fire because the input is hidden.

I have a question:

Once the toggle is not handled by the input itself anymore, the event is not triggered automatically.
If the input stays hidden, how could I fire the onChange event correctly?

Thanks in advance

@michaldudak
Copy link
Member Author

Hi @trizotti. You will likely need to trigger the event manually, similarly to how SelectInput does it (https://github.com/mui/material-ui/blob/3bde8db383f61897e8710657f05d36a2f27737aa/packages/mui-material/src/Select/SelectInput.js#L286-L298)

@nicolas-ot
Copy link

I will try to solve this issue

@michaldudak michaldudak changed the title [Switch][base] .Mui-focusVisible is set on a wrong element [Switch][base-ui] .Mui-focusVisible is set on a wrong element Aug 18, 2023
@michaldudak michaldudak transferred this issue from mui/material-ui Feb 27, 2024
@michaldudak michaldudak changed the title [Switch][base-ui] .Mui-focusVisible is set on a wrong element [Switch] .Mui-focusVisible is set on a wrong element Feb 27, 2024
@michaldudak michaldudak added bug 🐛 Something doesn't work component: switch This is the name of the generic UI component, not the React module! labels Feb 27, 2024
@michaldudak
Copy link
Member Author

This issue is not relevant anymore, as we changed the Switch structure and API substantially in #135. If there are any problems with the new implementation, feel free to open a new issue.

@github-project-automation github-project-automation bot moved this from In progress to Done in Base UI Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: switch This is the name of the generic UI component, not the React module!
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants