Skip to content

[Bug]: Annotation Select picks the shadow host instead of elements inside shadow DOM #17276

Closed

Description

@henrik092

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/desktop

Steps to reproduce

  1. Save this page as index.html and serve it with any static server (for example npx serve):

    <!doctype html>
    <html>
      <body>
        <shadow-demo></shadow-demo>
        <script>
          customElements.define(
            "shadow-demo",
            class extends HTMLElement {
              constructor() {
                super();
                this.attachShadow({ mode: "open" }).innerHTML = `
                  <style>button { margin: 8px; padding: 12px; }</style>
                  <button>First</button>
                  <button>Second</button>`;
              }
            },
          );
        </script>
      </body>
    </html>
  2. Open it in the desktop preview browser.

  3. Start the annotation tool, choose Select, and hover over / click the Second button.

Expected behavior

The hovered element inside the open shadow root (the Second button) is outlined and selected, like elements in the regular DOM.

Actual behavior

The whole shadow host (<shadow-demo>) is outlined and selected. The captured element context reports the host, e.g. selector html > body > shadow-demo.

Real-world impact: apps built from web components are effectively unpickable. In Home Assistant the entire UI lives under <home-assistant>, so every click selects the whole page (html > body > home-assistant).

Likely cause: pickFromPoint() in apps/desktop/src/preview/PickPreload.ts only queries document.elementsFromPoint(). Hit-testing on document is retargeted to the outermost shadow host and never descends into shadow roots. A possible direction is to continue with candidate.shadowRoot.elementsFromPoint(x, y) while the candidate has an open shadow root (closed roots, including the picker's own overlay, stay opaque), and to make the reported selector shadow-aware. Possibly related boundary issue for iframes: #3141.

Impact

Major degradation or frequent failure

Version or commit

Desktop nightly (auto-update channel nightly); the same code path is unchanged on main @ 805967a

Environment

Linux Mint 22.3 (x86_64), T3 Code desktop AppImage

Workaround

Use the Region (or Draw) tool instead of Select on shadow-DOM pages.

Activity

  1. juliusmarminge commented on Oct 8, 2026

    @juliusmarminge
    Member

    Note

    Grok responding on behalf of Julius.

    Thanks for the clear repro. From reading the code on main @ 805967a, the cause you describe matches what's there. I haven't run the repro page myself.

    Where it happens

    • PickPreload.ts:276-284: pickFromPoint() only walks document.elementsFromPoint() and returns the first non-overlay element. Document-level hit testing is retargeted to the outermost shadow host, and nothing descends into shadowRoot. Hover (L1194-L1195) and click (L1225-L1226) both go through it, so the outline, label and selection all land on the host.
    • The selector comes from react-grab's getElementContext() (L392-L414). Today it gets the host, which is consistent with the html > body > shadow-demo you saw. From the minified react-grab 0.1.44 bundle, it looks like an inner element would get a selector that is only unique within its own shadow root, with no host prefix. So fixing the pick alone probably isn't enough: the selector sent to the agent (and copied into style changes at L1373-L1374) would need the host chain.
    • The screenshot crop uses getBoundingClientRect() (L1380), which is in viewport coordinates, so it should work unchanged for inner elements.
    • The Region tool's element scan has the same gap: document.querySelectorAll("body *") (L1150) doesn't enter shadow roots either. That's likely why it falls back to a plain region on shadow-DOM pages.

    Possible fix (untested)

    1. In pickFromPoint(), after picking a candidate, check whether it has an open shadowRoot. If so, run shadowRoot.elementsFromPoint(x, y), keep only results whose getRootNode() is that root, and repeat while there's a deeper open root. If the inner query finds nothing, keep the host. Closed roots return shadowRoot === null, so they stay opaque. That includes the picker's own overlay, which is a closed root on a host marked data-t3code-annotation-ui (L520-L524), so the existing isAnnotationNode skip should still apply. A similar descent pattern already exists in RecordingInput.ts:52.
    2. In captureElement(), build a shadow-aware selector by walking getRootNode().host outward and joining the selector for each scope, for example home-assistant >>> ha-panel >>> button. >>> isn't valid querySelector syntax, so it should be treated as an agent-readable path, or carried in a separate field, rather than something to re-query directly.
    3. Optionally, make the Region scan descend open roots too, so the tools behave the same way.

    PickPreload.ts is the only element-pick path I found (it has been at this path since 3d9e8dea7). Iframe boundaries are a separate, related gap tracked in #3141. I didn't find any duplicates or open PRs for this.

  2. added
    bug
    via-triage
    on Oct 8, 2026
  3. added
    needs more info
    and removed
    needs more info
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugvia-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions