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

Fix document.activeElement not being correct when components are use inside custom elements #1570

Merged
merged 3 commits into from
Jan 27, 2025

Conversation

pmairoldi
Copy link
Contributor

Fixes #1564
Reverts fixes from #1314

This pull requests address the fact that docuement.activeElement will be incorrect when components are used inside of a custom element. After some tests here is what I found:

  1. if the focus is inside a custom element the document.activeElement will be the host of the custom element and the actual active element can be retrived by activeElement.shadowRoot.activeElement
  2. if a custom element is within another custom element includes another custom element as a child and that child has an element that is focused then the same trick as point 1 can be used but one level deeper activeElement.shadowRoot.activeElement..shadowRoot.activeElement

With these findings I have implemented a function useActiveElement to retrieve the activeElement regardless of the nested shadowRoot. With this function I've replace all instances of document.activeElement with useActiveElement(). After some tests it seems like everything still works and we do not need the fixes from #1314 anymore.

@pmairoldi pmairoldi marked this pull request as ready for review January 21, 2025 18:48
@zernonia zernonia linked an issue Jan 22, 2025 that may be closed by this pull request
@zernonia
Copy link
Member

Thanks for the awesome PR @pmairoldi ! Some minor comment on the function naming 😁

Copy link
Member

@zernonia zernonia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done @pmairoldi !!! Thanks for the PR! 🎉🚀

@zernonia zernonia merged commit f56f2db into unovue:main Jan 27, 2025
3 checks passed
Copy link

pkg-pr-new bot commented Jan 27, 2025

Open in Stackblitz

npm i https://pkg.pr.new/radix-vue@1570

commit: 9565a26

@pmairoldi pmairoldi deleted the fix/1564 branch January 31, 2025 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Web component DropdownMenu and Select arrow key bug
2 participants