Skip to content

[drawer] Fix Shadow DOM swipe gestures - #5360

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/drawer-shadow-dom-swipe
Jul 30, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/drawer-shadow-dom-swipe

Conversation

@atomiks

@atomiks atomiks commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #5359

Changes

  • Use the drawer's document or shadow root when hit-testing swipe targets.
  • Preserve ignored-target and scroll-arbitration behavior inside shadow roots.

@atomiks atomiks added type: bug It doesn't behave as expected. component: drawer Changes related to the drawer component. labels Jul 28, 2026 — with ChatGPT Codex Connector
@pkg-pr-new

pkg-pr-new Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5360
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5360
    

commit: 18c4d76

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+35B(+0.01%) 🔺+13B(+0.01%)

Details of bundle changes

Performance

Total duration: 1,470.08 ms -30.88 ms(-2.1%) | Renders: 78 (+0) | Paint: 2,302.97 ms -50.27 ms(-2.1%)

No significant changes — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 18c4d76
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a6a9bb40dcba6000812a976
😎 Deploy Preview https://deploy-preview-5360--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks

atomiks commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@atomiks
atomiks marked this pull request as ready for review July 29, 2026 00:34

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR review

The fix itself is sound: routing every point hit-test through the element's getRootNode() is the right shape for shadow DOM, all five converted call sites are consistent with each other, and the downstream traversal helpers (contains, findScrollableTouchTarget, hasScrollableAncestor) are already shadow-safe, so nothing regresses in the light-DOM path. Nothing is merge-blocking. The one substantive gap is coverage: I reverted each hunk individually and only one of the five converted call sites is actually pinned by a test — including the canStart path that is the mechanism behind #5359, and the entire second commit.

Verification run locally on the PR head: DrawerViewport + full src/drawer suites green in jsdom and chromium (286 tests), tsc --noEmit clean on packages/react, eslint clean on all five changed files.

Bugs (1)

1. ℹ️ Hit testing still stops at the first shadow boundary

Location: packages/react/src/utils/getElementAtPoint.ts:8

return typeof root?.elementFromPoint === 'function' ? root.elementFromPoint(x, y) : null;

elementFromPoint on a Document or ShadowRoot retargets the result into that tree, so it descends exactly one level. If a consumer renders a web component inside Drawer.Popup and puts data-base-ui-swipe-ignore on an element inside that component's shadow tree, elementAtPoint resolves to the component's host and isSwipeIgnoredTarget's closest() never sees the marker. This is pre-existing (light-DOM drawers had it before the PR), and the virtual-keyboard path is insulated because it falls back to resolveKeyboardTouchTarget(getTarget(event.nativeEvent)), which is composed — but DrawerViewport.onTouchStart has no such fallback for the ignore check.

Failure scenario: A user embeds a <my-media-player> custom element with data-base-ui-swipe-ignore on its internal scrubber inside a bottom drawer; dragging the scrubber starts a drawer dismiss swipe instead of scrubbing.

Fix: Optional, and fine to leave for a follow-up. If you want it composed, loop into the hit element's own shadow root:

let element = root.elementFromPoint(x, y);
while (element?.shadowRoot) {
  const inner = element.shadowRoot.elementFromPoint(x, y);
  if (!inner || inner === element) break;
  element = inner;
}
return element;

Tests (1)

1. 🟠 Only one of five converted hit-test call sites is covered

Location: packages/react/src/drawer/viewport/DrawerViewport.test.tsx:251

I reverted each hunk individually against the PR head and re-ran src/drawer in chromium (286 tests). Only onTouchStart is pinned — the new test fails without its hunk, as intended. Everything else stays green when reverted:

Reverted hunk Suite result
DrawerViewport.tsx:357 (canStart) 286 passed
DrawerViewport.tsx:874 (onPointerDown) not separately isolated, but shares the untested pointer path
useSwipeDismiss.ts:349 (getTargetAtPoint) 286 passed
DrawerVirtualKeyboardProvider.tsx (whole commit fc614a6) 55 passed

canStart is the notable one. It is the branch that returns false because contains(popupElement, elementAtPoint) fails against the retargeted host — i.e. the mechanism that actually blocks the swipe reported in #5359 — and the PR ships no test for it. The virtual-keyboard commit ships no test at all; every existing VKB test stubs document.elementFromPoint with the drawer in the light DOM, so getRootNode() resolves to document and the stub is hit either way.

Failure scenario: Someone later "restores" ownerDocument(...) in canStart or in resolveKeyboardTouchTargetFromPoint to satisfy the AGENTS.md owner-utility rule, CI stays green, and shadow-DOM swipe silently regresses to the state of #5359.

Fix: Add a chromium test rendering into a real attachShadow root with laid-out popup geometry (no elementFromPoint stubbing) that drives a mouse pointer swipe through canStart, plus a shadow-root variant of one existing VKB tap-to-focus test (stub shadowRoot.elementFromPoint the way the current tests stub document.elementFromPoint). Note that an unstyled popup has no usable hit geometry — give the popup and the shadow host explicit sizes, or stub the shadow root's elementFromPoint as the new test does.

Simplifications (1)

1. 🟡 Derive the virtual-keyboard root from rootElement, not event.currentTarget

Location: packages/react/src/drawer/virtual-keyboard-provider/DrawerVirtualKeyboardProvider.tsx:571

const root = event.currentTarget.getRootNode();

rootElement is already null-guarded eight lines above and is what the rest of this callback uses for the contains(rootElement, keyboardTarget.focusTarget) checks. event.currentTarget only happens to be the same element because DrawerViewport is currently the sole caller of virtualKeyboard.onTouchEnd; the coupling isn't visible from the provider.

Failure scenario: A future component forwards its touch events into the provider — Drawer.SwipeArea is the obvious candidate, and it renders outside Drawer.Portal — so currentTarget sits in the light DOM while the drawer is portaled into a shadow root. The lift-point hit test then queries the wrong tree, pointTarget comes back null, and tap-to-focus silently falls back to the touchstart target.

Fix: const root = rootElement.getRootNode(); and drop the currentTarget read.

Docs (1)

1. 🟡 No comment records why getRootNode() replaces ownerDocument()

Location: packages/react/src/utils/getElementAtPoint.ts:3

AGENTS.md instructs contributors (and agents) to "always use the owner utilities ownerDocument and ownerWindow". This PR deliberately goes the other way for hit testing, at five call sites, with no note explaining that a Document retargets shadow content to the host and a ShadowRoot does not. The repo already sets the precedent for exactly this kind of one-liner — packages/react/src/utils/scrollable.ts:46 and :66 both carry a comment explaining why getParentNode is used instead of parentElement.

Failure scenario: A contributor reading getElementAtPoint(popupElement.getRootNode(), ...) next to ownerDocument(popupElement) on the adjacent line reads it as an inconsistency and normalizes it back, reintroducing #5359 with a green CI.

Fix: Add a one-line comment above getElementAtPoint, e.g. // The root node, not the document: 'Document.elementFromPoint' retargets shadow content to the host element, which then fails the 'contains()' check against a popup inside that shadow root.

Verdict

Approve after nits - the shadow-root routing is correct and green across jsdom and chromium; the outstanding items are a real coverage gap on four of five converted call sites and two small clarity fixes.


🤖 Review generated with Claude Code

@atomiks

atomiks commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

@claude review low

@github-actions

Copy link
Copy Markdown

PR review

No findings.

The switch from ownerDocument-based to getRootNode()-based hit testing is behavior-preserving for drawers outside shadow roots (getRootNode() resolves to the document there), and every call site null-checks the hit-test result, which covers engines where ShadowRoot.elementFromPoint returns null for hits outside the shadow tree (canStart bails, the viewport handlers treat null as not-ignored as before, and useSwipeDismiss/the keyboard provider fall back to the composed event target). Downstream helpers (findScrollableTouchTarget, contains) already traverse across shadow boundaries, so the deeper targets the shadow-root hit test now returns are handled. Residual risk is limited to cross-browser ShadowRoot.elementFromPoint retargeting quirks in real browsers, which the new Chromium-only tests cover for the main paths; I reviewed statically without executing the PR head.

Verdict

Approve — the hit-test root change is consistent across all call sites with no regression for the non-shadow path.


🤖 Review generated with Claude Code

@atomiks
atomiks merged commit 634b140 into mui:master Jul 30, 2026
6 checks passed
@atomiks
atomiks deleted the codex/drawer-shadow-dom-swipe branch July 30, 2026 00:49
legowhales added a commit to legowhales/svelte-base-drawer that referenced this pull request Aug 13, 2026
Ports the six applicable Drawer fixes from the base-ui v1.7.0 release:

- mui/base-ui#5105: reliable swipe-to-open — first-move re-anchoring is now
  gated on trackDrag (the SwipeArea passes trackDrag: false, so a flick whose
  whole travel lands in the first move still registers), and
  sawPrimaryButtonsOnMove initializes from the event type.
- mui/base-ui#5308: drag styles are only rewritten when the offset actually
  changed, fixing the snap point jump when the pointer is pinned at a screen
  edge. The swipeThreshold function is snapshotted at gesture start.
- mui/base-ui#5257: cross-axis touch arbitration rewritten
  (shouldYieldTouchMove) — 6px slop / 2px bias, one-shot attribution per
  gesture, non-cancelable moves yield to native scroll, and no preventDefault
  until an axis passes the slop (iOS kills native scrolling for the whole
  gesture otherwise).
- mui/base-ui#5360: elementFromPoint resolves through getRootNode() so swipe
  gestures work inside shadow DOM.
- mui/base-ui#5112: the popup no longer flashes fully open for a frame during
  a swipe-area open — resetAfterOpen skips the movement-var reset while the
  SwipeArea drives the gesture (swipeAreaActive), and the SwipeArea re-asserts
  its styles when the popup (re)mounts.
- mui/base-ui#5179: virtual keyboard scroll handling rework for focus moves
  while the keyboard is open — interval realign passes, preemptive focus
  geometry override to neutralize WebKit's reveal scroll, window scroll
  pinning, programmatic-focus flag, settle-watching scroll bookkeeping.

The SwipeArea outside-press guard is now deterministic (capture-phase
pointerdown/click listeners + isVirtualClick) instead of a 300ms timer.

Not ported (React-only): mui/base-ui#5109, Drawer.Handle/createHandle
detached triggers, bundle-size refactors.

Co-Authored-By: Claude Fable 5 <[email protected]>
zernonia added a commit to unovue/reka-ui that referenced this pull request Sep 28, 2026
Port the latest Base UI `DrawerVirtualKeyboardProvider` changes:
- Remove the extra keyboard scroll slack; pad by the overlap on top of at
  least the visibility margin (mui/base-ui#5768).
- Reapply `preventScroll` on native focus before `focusin` for iOS 27, and
  after a tapped label refocuses its control (mui/base-ui#5769).
- Track the keyboard across Chrome on iOS resizing the layout viewport, with
  an `svh` probe, a `resizing` guard and a window resize listener
  (mui/base-ui#5774).
- Find the scroll container across shadow roots (mui/base-ui#5360).

Also check for DrawerViewport once DrawerContent mounts, like Base UI's
popup, so a drawer that starts open no longer logs a false warning.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
zernonia added a commit to unovue/reka-ui that referenced this pull request Sep 28, 2026
#2889)

* feat(Drawer): add DrawerVirtualKeyboardProvider for software keyboards

The software keyboard slides over the bottom of the viewport without
resizing it, so a bottom-anchored drawer — and the field the user just
tapped — end up underneath it (#2877).

Wrapping the drawer in `DrawerVirtualKeyboardProvider` opts into
keyboard-aware focus and scroll handling: while a field inside the drawer
is focused it publishes the keyboard height as `--drawer-keyboard-inset`
on `DrawerViewport` (or the popup, when no viewport is used) and keeps the
field scrolled into the band left visible above the keyboard, adding
temporary slack to its scroller when the field would otherwise sit below
the scrollable range.

It also defends against WebKit: a tap on a field is focused synchronously
with the field's painted geometry overridden so the native reveal cannot
drag a transformed sheet, native focus moves are preempted the same way,
and a modal drawer pins the page against reveal scrolls the scroll lock
cannot hold. Alignment is suspended while pinch-zoomed or while a nested
drawer is open, and viewport changes under 60px are treated as browser
chrome rather than the keyboard.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* refactor(Drawer): pass view on the redispatched keyboard tap click

Matches Base UI, falling back to an init without `view` where the window
fails the realm check (jsdom). Restores lean Base UI references in the
ported code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* refactor(Drawer): require DrawerViewport for the virtual keyboard provider

Matches Base UI: the viewport wraps the popup, acts as the measurement and
containment root, and hosts `--drawer-keyboard-inset` for the popup and
everything inside it. Warns in development when it is missing instead of
falling back to the popup.
 

* fix(Drawer): address review feedback on the virtual keyboard provider

- `DrawerViewport` now remembers the element it registered on the root
  context. Vue nulls the template ref before `onUnmounted` runs, so the
  identity check against `currentElement` never matched and the root kept
  a detached viewport.
- `useDrawerVirtualKeyboard` resolves `getComputedStyle` / `matchMedia`
  from the element's own window, like the rest of the composable.
- Tests restore fake timers, `innerHeight`, `scrollTo`, `scrollY` and
  `elementFromPoint` in `afterEach` so cases stay order-independent, and
  a new case covers the viewport teardown.

 

* test(Drawer): restore the original innerHeight descriptor after keyboard tests

jsdom defines `window.innerHeight` as an own property, so deleting it in
teardown left the window without one. Capture the descriptor before
stubbing and put it back.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(Drawer): sync the virtual keyboard provider with upstream Base UI

Port the latest Base UI `DrawerVirtualKeyboardProvider` changes:
- Remove the extra keyboard scroll slack; pad by the overlap on top of at
  least the visibility margin (mui/base-ui#5768).
- Reapply `preventScroll` on native focus before `focusin` for iOS 27, and
  after a tapped label refocuses its control (mui/base-ui#5769).
- Track the keyboard across Chrome on iOS resizing the layout viewport, with
  an `svh` probe, a `resizing` guard and a window resize listener
  (mui/base-ui#5774).
- Find the scroll container across shadow roots (mui/base-ui#5360).

Also check for DrawerViewport once DrawerContent mounts, like Base UI's
popup, so a drawer that starts open no longer logs a false warning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: drawer Changes related to the drawer component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Drawer] Swipe gestures fail when rendered inside Shadow DOM

2 participants