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

refactor: improve shadow dom support #2152

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

segunadebayo
Copy link
Member

No description provided.

Copy link

vercel bot commented Jan 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
zag-nextjs ✅ Ready (Inspect) Visit Preview Jan 1, 2025 10:50pm
zag-solid ✅ Ready (Inspect) Visit Preview Jan 1, 2025 10:50pm
zag-svelte ✅ Ready (Inspect) Visit Preview Jan 1, 2025 10:50pm
zag-vue ✅ Ready (Inspect) Visit Preview Jan 1, 2025 10:50pm
zag-website ✅ Ready (Inspect) Visit Preview Jan 1, 2025 10:50pm

Copy link

changeset-bot bot commented Jan 1, 2025

⚠️ No Changeset found

Latest commit: 4b171e7

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

Copy link

pkg-pr-new bot commented Jan 1, 2025

Open in Stackblitz

@zag-js/anatomy

npm i https://pkg.pr.new/chakra-ui/zag/@zag-js/anatomy@2152

@zag-js/anatomy-icons

npm i https://pkg.pr.new/chakra-ui/zag/@zag-js/anatomy-icons@2152

@zag-js/docs

npm i https://pkg.pr.new/chakra-ui/zag/@zag-js/docs@2152

@zag-js/core

npm i https://pkg.pr.new/chakra-ui/zag/@zag-js/core@2152

@zag-js/store

npm i https://pkg.pr.new/chakra-ui/zag/@zag-js/store@2152

@zag-js/types

npm i https://pkg.pr.new/chakra-ui/zag/@zag-js/types@2152

commit: 4b171e7

@mbeckem
Copy link

mbeckem commented Feb 24, 2025

Hi, i've stumbled upon this PR while debugging issues with outside click detectionwithin shadow doms. Do you plan to merge this in the near future? I think it could resolve the issues i've observed.

For example, when clicking the select trigger in a shadow dom, the popover closes and immediately reopens.

For reference, this is the patch I went ahead with for now.

@zag-js/interact-outside/dist/index.mjs:

diff --git a/dist/index.mjs b/dist/index.mjs
index e346d526de4f6cb0683e31f12589ee053d6a30b2..19c51cfe8e81cf31638b974d81f6b351b6a3e991 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -106,8 +106,7 @@ function trackInteractOutsideImpl(node, options) {
   const win = getWindow(node);
   const frames = getWindowFrames(win);
   const parentWin = getParentWindow(win);
-  function isEventOutside(event) {
-    const target = getEventTarget(event);
+  function isEventOutside(event, target) {
     if (!isHTMLElement(target)) return false;
     if (!target.isConnected) return false;
     if (contains(node, target)) return false;
@@ -126,8 +125,9 @@ function trackInteractOutsideImpl(node, options) {
     function handler() {
       const func = defer ? raf : (v) => v();
       const composedPath = event.composedPath?.() ?? [event.target];
+      const target = getEventTarget(event);
       func(() => {
-        if (!node || !isEventOutside(event)) return;
+        if (!node || !isEventOutside(event, target)) return;
         if (onPointerDownOutside || onInteractOutside) {
           const handler2 = callAll(onPointerDownOutside, onInteractOutside);
           node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
@@ -160,8 +160,9 @@ function trackInteractOutsideImpl(node, options) {
   }, 0);
   function onFocusin(event) {
     const func = defer ? raf : (v) => v();
+    const target = getEventTarget(event);
     func(() => {
-      if (!node || !isEventOutside(event)) return;
+      if (!node || !isEventOutside(event, target)) return;
       if (onFocusOutside || onInteractOutside) {
         const handler = callAll(onFocusOutside, onInteractOutside);
         node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
@@ -172,7 +173,7 @@ function trackInteractOutsideImpl(node, options) {
         detail: {
           originalEvent: event,
           contextmenu: false,
-          focusable: isFocusable(getEventTarget(event))
+          focusable: isFocusable(target)
         }
       });
     });

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.

2 participants