-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Popover enabling :focus-visible
state on mouse interaction
#1694
Comments
:focus-hover
state on mouse interaction:focus-visible
state on mouse interaction
I see this behaviors but I am not sure if this is on purpose or not. One thing I do know is that once the popover disappears the focus should be disabled. Can someone confirm whether it is supposed to be enabled or not at all? I would love to help with this issue. |
This can't be on purpose. This is not how |
Removing the following lines fixes the issue. In my opinion these manual focus calls are unnecessary. If I click somewhere with my mouse, I know where my focus is going to. headlessui/packages/@headlessui-react/src/components/popover/popover.tsx Lines 266 to 269 in 5af3bd4
Also there are similar focus related issues with other components too. Modal component:
Tabs component:
|
Yup just encountered this on the vue tab component, odd enough when I click another button on the screen the behavior becomes the expected one. Not sure if some off ergonomic around focus-visible or the proposed fix covers it. Reproduction URL |
I get Minimal reproduction: https://stackblitz.com/edit/github-vhafgm?file=pages/index.tsx |
Hoping we can improve this but it might be out of our hands — we do need to programmatically focus things a lot in Headless UI for accessibility reasons and often the browser treats that programmatic focusing as if it were keyboard driven and triggers a focus indicator even though I totally agree that we don't want it to. On our own sites we use the PostCSS focus-visible polyfill still and not the native |
I have to click 2 times on the popover here: https://headlessui.com/vue/popover and the only browser that works at first click is firefox. Any update on this? |
As a quick, rudimental fix, the following helped me out for the <Tab
onClick={() => {
requestAnimationFrame(() => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
});
}}
>
…
</Tab> |
Any update on this? In VueJS I was able to fix the first focus (when we click on the PopoverButton) by using this:
But how to catch the event when Popover closes? Popover doesn't fire any events. It's possible, I guess, to put a global event handler that blurs Popover, but looks hacky. Also, if I set PopoverButton to be a link (by using |
Not sure if it's just me, but even after adding the postcss-focus-visible polyfill in Tailwind 3.0+ using JIT, it still doesn't work on the popover as a default button. Instructions here on adding |
Hey, thank you for this bug report! 🙏 This should be fixed by #2347, and will be available in the next release. However, it will require a bit of setup to make it behave how you want exactly because right now there is no way to control this behavior via a browser API. I wrote more about the solution in the PR itself and how you can make it work: #2347 (comment) You can use the insiders builds to play with the fix:
I also updated the original reproduction URL shared by @sibbng using the above versions, I also made sure to:
Link: https://stackblitz.com/edit/vitejs-vite-mggdgh?file=src/App.jsx |
Hey, thank you for the fix! I added |
It appears as if there is no group-focus-visible equivalent for the Headless UI Tailwind plugin. Am I missing something? |
You can remove <Popover className="relative">
<Popover.Button className="... focus-visible:outline-none">
<VscInfo className="ml-4 h-7 w-7 cursor-pointer text-gray-200" />
</Popover.Button>
... |
you just saved me from more hours searching for the right flag. Thanks.! |
Wow, is 2024 and this has not yet been fixed (at least in the Popover component). |
There is a standard that disables the visible focus state on manual focus() calls, but it is currently only implemented by Firefox: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus#focusvisible I have come up with this monkey patch that specifically targets the Popover behavior. It also lazily restores focus to the trigger if the next input is a keyboard event. Note that I don't use this function myself. Since it overrides some globals, it may break your application code. Please test thoroughly if you plan to use it. if (typeof window !== "undefined" && !globalThis.popover_patched) {
globalThis.popover_patched = true
let pointerDown = false
addEventListener("click", () => { pointerDown = true }, true)
const _focus = HTMLElement.prototype.focus;
HTMLElement.prototype.focus = function () {
if (pointerDown && this.dataset?.headlessuiState?.includes("open")) {
const handler = (e) => {
_focus.bind(this)();
if(event.code === "Space") this.click()
}
addEventListener("keydown", handler, {once:true})
addEventListener("pointerdown", () => {removeEventListener("keydown", handler)},{once:true})
return
};
_focus.bind(this)();
pointerDown = false
};
const _preventDefault = Event.prototype.preventDefault;
Event.prototype.preventDefault = function () {
console.log(this.target.dataset?.headlessuiState);
if (this.target.dataset?.headlessuiState?.includes("hover")) return;
_preventDefault.bind(this)();
}
} |
What package within Headless UI are you using?
@headlessui/react
@headlessui/vue
What version of that package are you using?
v1.6.6
What browser are you using?
Chrome
Reproduction URL
https://headlessui.com/react/popover
https://stackblitz.com/edit/vitejs-vite-93uk6f?file=src/App.jsx
Describe your issue
When I click
Popover.Button
it enables:focus-visible
state. AFAIK it should do that only with keyboard interactions. At least that's how regular HTML buttons work.The text was updated successfully, but these errors were encountered: