Skip to content

Conversation

cpsoinos
Copy link

@cpsoinos cpsoinos commented Sep 5, 2025

This pull request introduces comprehensive improvements to focus management across several Radix UI packages, with a particular emphasis on supporting Shadow DOM scenarios. The main enhancement is the addition and integration of the getDeepActiveElement utility, which reliably retrieves the currently focused element even when focus is within a shadow root. This change ensures consistent and predictable focus behavior for components rendered inside Shadow DOM, and also introduces new logic for menu and submenu interactions in these contexts.

Focus Management Enhancements:

  • Added the getDeepActiveElement utility to @radix-ui/primitive, enabling accurate retrieval of the focused element across Shadow DOM boundaries.
  • Replaced all direct usages of document.activeElement with getDeepActiveElement in @radix-ui/react-focus-scope, @radix-ui/react-menu, @radix-ui/react-navigation-menu, and @radix-ui/react-one-time-password-field, ensuring robust focus detection and handling in Shadow DOM environments. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]

Menu and Submenu Shadow DOM Support:

  • Implemented logic in @radix-ui/react-menu to force-close other submenus when entering any menu item within Shadow DOM, using a custom bubbling event (radix-force-close-submenu). This prevents unintended multiple submenus from remaining open. [1] [2]
  • Added an effect to MenuSubTrigger to listen for the custom close event and conditionally close submenus, excluding the currently hovered trigger.
  • Adjusted submenu grace period logic to use a longer timeout in Shadow DOM and prevent automatic closing, relying instead on explicit user actions for submenu closure. [1] [2]

Utilities and Dependency Updates:

  • Added @radix-ui/primitive as a dependency to @radix-ui/react-focus-scope to support the new utility function.
  • Introduced the isInShadowDOM utility to detect if an element is inside a Shadow DOM, used to trigger adjusted menu behaviors.

These changes collectively improve focus management reliability and menu interaction consistency, especially for applications leveraging Shadow DOM for encapsulation.

Fixes #3287
Fixes #3353
Fixes #2606

Copy link

changeset-bot bot commented Sep 5, 2025

⚠️ No Changeset found

Latest commit: de4315b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@cpsoinos cpsoinos force-pushed the JA-13509-shadow-dom-focus branch from ca85a26 to 897233e Compare September 5, 2025 21:05
cpsoinos and others added 2 commits September 23, 2025 15:43
- Add data-radix-menu-sub-trigger attribute to MenuSubTrigger for proper identification
- Implement custom event system for explicit submenu closure in shadow DOM contexts
- Disable automatic grace timer closure in shadow DOM to prevent premature submenu closing
- Use native event coordinates for improved accuracy in shadow DOM environments
- Extend grace period to 800ms in shadow DOM to account for coordinate detection limitations
- Ensure only one submenu remains open when navigating between multiple subtriggers

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove unnecessary empty lines and unused variables
- Extract shadow DOM detection into reusable utility function
- Simplify coordinate handling by removing unused adjustments
- Revert isPointerInGraceArea changes that weren't needed for the fix
- Reduce code duplication in shadow DOM detection logic

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@cpsoinos cpsoinos force-pushed the JA-13509-shadow-dom-focus branch from 970ef4c to b9126cc Compare September 23, 2025 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant