|
| 1 | +# Drawer BaseUI Parity Fixes — Implementation Plan |
| 2 | + |
| 3 | +> **Context:** Follow-up pass after code review against `github.com/mui/base-ui packages/react/src/drawer`. Addresses 6 Critical + 10 Important + misc Minor items. Reference: the code-review transcript in session + `/tmp/baseui-drawer/` (Base UI source snapshot). |
| 4 | +
|
| 5 | +**Goal:** Bring the Vue Drawer port into behavioral + API parity with Base UI's React Drawer where it matters for correctness, without a full viewport-extraction refactor. |
| 6 | + |
| 7 | +**Approach:** Fix behavior gaps in place in existing components (C1 is addressed functionally, not structurally). Add API-parity aliases (`Backdrop`/`Popup`/`Viewport`) via re-exports. Port snap-release math line-for-line from Base UI `DrawerViewport.tsx`. Rewrite the CSS-var sign handling in `useSwipeDismiss.ts` to use damped deltas directly. |
| 8 | + |
| 9 | +**Execution mode:** Inline phases with commits between each phase. Each phase is independently buildable so a failure leaves the tree clean. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Phase 1 — Surface API fixes (low risk) |
| 14 | + |
| 15 | +**Files:** |
| 16 | +- `packages/core/src/Drawer/DrawerRoot.vue` — flip `snapToSequentialPoints` default from `true` → `false`; widen `modal` type to `boolean | 'trap-focus'`; change `update:open` signature to `[value, details?]` with `reason` |
| 17 | +- `packages/core/src/Drawer/DrawerContent.vue` — add `initialFocus` / `finalFocus` props, wire to `FocusScope`; handle `modal: 'trap-focus'` branch (trap focus true + disable-outside-pointer-events false) |
| 18 | +- `packages/core/src/Drawer/DrawerContentImpl.vue` — accept and pass through `initialFocus`/`finalFocus` |
| 19 | +- `packages/core/src/Drawer/index.ts` — add `DrawerBackdrop` (alias of Overlay), `DrawerPopup` (alias of Content), `DrawerViewport` export (new file), `Drawer*` namespace |
| 20 | +- `packages/core/src/Drawer/DrawerViewport.vue` — **new**, passthrough wrapper carrying `data-drawer-viewport` and forwarding slots. Thin for now; exists for API parity + future extraction point. |
| 21 | +- `packages/core/src/Drawer/composables/useDrawerSnapPoints.ts` — `parseSnapPoint` returns `null` for unknown units (then filtered); cache resolved active snap point |
| 22 | +- `packages/core/src/Drawer/Drawer.test.ts` — `SpyInstance` → `MockInstance` |
| 23 | +- `packages/core/src/Drawer/DrawerContentImpl.vue` — remove `nestedDepth = ? 1 : 0` (replaced in Phase 3) |
| 24 | + |
| 25 | +## Phase 2 — Gesture math fixes (high value, moderate risk) |
| 26 | + |
| 27 | +**C2 — sign-convention rewrite** in `useSwipeDismiss.ts`: |
| 28 | +- Drop `sign`/`offsetX`/`offsetY` reconstruction at lines 217-233 |
| 29 | +- Write `applyDirectionalDamping(rawDx, rawDy, allowedDirections)` that returns a `{x, y}` where the allowed-direction axes pass through linearly but overshoot on each axis sqrt-damps (Base UI `useSwipeDismiss.ts` lines ~140-180) |
| 30 | +- Assign directly: `dragOffset.value = damped`; `setCssVars(el, damped.x, damped.y)` |
| 31 | +- `finishSwipe` displacement computation now uses `getDisplacement(dir, damped.x, damped.y)` which produces the same magnitude the user physically moved |
| 32 | + |
| 33 | +**C3 — snapToNearest takes raw deltas**: |
| 34 | +- Expose `rawDelta` on `SwipeProgressDetails` (already has `deltaX`/`deltaY`) |
| 35 | +- In `DrawerContentImpl.vue:onRelease`, capture latest `rawDelta` via `onProgress` closure and pass to `snapToNearest` |
| 36 | +- Inside `useDrawerSnapPoints.ts:snapToNearest`, compute `dragDelta = direction === 'down' ? deltaY : direction === 'up' ? -deltaY : direction === 'right' ? deltaX : -deltaX` |
| 37 | + |
| 38 | +**C4 — snap release math rewrite** (port from Base UI `DrawerViewport.tsx` lines ~577-714): |
| 39 | +```ts |
| 40 | +const SNAP_VELOCITY_THRESHOLD = 0.5 |
| 41 | +const SNAP_VELOCITY_MULTIPLIER = 300 |
| 42 | +const MAX_SNAP_VELOCITY = 4 |
| 43 | +const FAST_SWIPE_VELOCITY = 0.5 |
| 44 | + |
| 45 | +function snapToNearest(dragDelta, velocity, direction, sequential) { |
| 46 | + const points = resolvedSnapPoints.value |
| 47 | + if (points.length === 0) |
| 48 | + return |
| 49 | + const ph = popupHeight.value |
| 50 | + const vel = (direction === 'up' || direction === 'down') ? velocity.y : velocity.x |
| 51 | + // In Base UI terms: positive vel = collapsing (moving in dismiss direction) |
| 52 | + const velSigned = (direction === 'up' || direction === 'left') ? -vel : vel |
| 53 | + |
| 54 | + const activePoint = points.find(p => p.value === activeSnapPoint.value) |
| 55 | + const currentOffset = activePoint?.offset ?? 0 |
| 56 | + const dragTargetOffset = Math.max(0, Math.min(ph, currentOffset + dragDelta)) |
| 57 | + |
| 58 | + let targetOffset = dragTargetOffset |
| 59 | + if (Math.abs(velSigned) >= SNAP_VELOCITY_THRESHOLD) { |
| 60 | + const clampedVel = Math.max(-MAX_SNAP_VELOCITY, Math.min(MAX_SNAP_VELOCITY, velSigned)) |
| 61 | + targetOffset = dragTargetOffset + clampedVel * SNAP_VELOCITY_MULTIPLIER |
| 62 | + } |
| 63 | + |
| 64 | + // Find closest snap point |
| 65 | + let closest = points[0] |
| 66 | + let closestDist = Math.abs(targetOffset - closest.offset) |
| 67 | + for (const p of points) { |
| 68 | + const d = Math.abs(targetOffset - p.offset) |
| 69 | + if (d < closestDist) { |
| 70 | + closest = p |
| 71 | + closestDist = d |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Close vs snap decision: close only if closer to fully-closed than any snap |
| 76 | + const closeDistance = Math.abs(targetOffset - ph) |
| 77 | + if (closeDistance < closestDist) { |
| 78 | + onSnapPointChange(null) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if (sequential) { |
| 83 | + // Sequential: only advance one step in the dragged direction, and only |
| 84 | + // if velocity direction matches drag direction AND |vel| >= FAST_SWIPE_VELOCITY, |
| 85 | + // OR the projected target has physically crossed the adjacent snap. |
| 86 | + const sorted = [...points].sort((a, b) => a.offset - b.offset) |
| 87 | + const currentIdx = sorted.findIndex(p => p.value === activeSnapPoint.value) |
| 88 | + if (currentIdx < 0) { |
| 89 | + onSnapPointChange(closest.value) |
| 90 | + return |
| 91 | + } |
| 92 | + const dragDir = Math.sign(dragDelta) // +1 = dismiss dir, -1 = open dir |
| 93 | + const velDir = Math.sign(velSigned) |
| 94 | + const shouldAdvance = velDir === dragDir && Math.abs(velSigned) >= FAST_SWIPE_VELOCITY |
| 95 | + const adjacentIdx = Math.max(0, Math.min(sorted.length - 1, currentIdx + dragDir)) |
| 96 | + const adjacent = sorted[adjacentIdx] |
| 97 | + if (shouldAdvance) { |
| 98 | + onSnapPointChange(adjacent.value) |
| 99 | + return |
| 100 | + } |
| 101 | + // Check physical crossing |
| 102 | + const crossed = dragDir > 0 |
| 103 | + ? targetOffset > adjacent.offset |
| 104 | + : targetOffset < adjacent.offset |
| 105 | + onSnapPointChange(crossed ? adjacent.value : (activeSnapPoint.value ?? closest.value)) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + onSnapPointChange(closest.value) |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Phase 3 — Missing behaviors |
| 114 | + |
| 115 | +**P3.1 — write `--drawer-swipe-strength` on release** (`DrawerContentImpl.vue`): in `onRelease`, compute a 0.1-1.0 scalar from remaining distance + velocity and set it on `contentElement`. Formula from Base UI `DrawerViewport.tsx` `computeReleaseDurationScalar`. |
| 116 | + |
| 117 | +**P3.2 — scroll-edge swipe** (`useSwipeDismiss.ts`): replace the blanket cancel at line 355-390 with a scroll-edge check. When first move is on the dismiss axis, allow the swipe only if the scrollable ancestor is at the relevant edge (`scrollTop === 0` for a `down` drawer, `scrollTop + clientHeight >= scrollHeight` for an `up` drawer, etc.). Base UI `useSwipeDismiss.ts:canSwipeFromScrollEdgeOnPendingMove`. |
| 118 | + |
| 119 | +**P3.3 — parent subscribes to nestedSwipeProgressStore** (`DrawerContentImpl.vue`): on mount, if parent context exists via `notifyParentSwipeProgressChange`, subscribe to `rootContext.nestedSwipeProgressStore` and write `DRAWER_CSS_VARS.swipeProgress` onto the popup element. Unsubscribe on unmount. |
| 120 | + |
| 121 | +**P3.4 — keepHeightWhileNested** (`DrawerContentImpl.vue`): guard `useResizeObserver` callback: if `rootContext.hasNestedDrawer.value === true` AND `rootContext.popupHeight.value > 0`, skip the height write. |
| 122 | + |
| 123 | +**P3.5 — data-swiping / data-swipe-direction on backdrop** (`DrawerOverlayImpl.vue` + `DrawerRoot.vue`): expose an `isSwiping` ref on root context; overlay reads it and renders the data-attrs. |
| 124 | + |
| 125 | +**P3.6 — nestedOpenDrawerCount** (`DrawerRoot.vue`): add a `nestedOpenDrawerCount: Ref<number>` to context, incremented/decremented in `onNestedDrawerPresenceChange`. `DrawerContentImpl.vue:150` reads `rootContext.nestedOpenDrawerCount.value` for the CSS var. |
| 126 | + |
| 127 | +## Phase 4 — Tests |
| 128 | + |
| 129 | +Add `Drawer.swipe.test.ts` (separate file): |
| 130 | +- Helper `simulateSwipe(el, { from, to, duration })` dispatching `pointerdown`/`pointermove`/`pointerup` events with `performance.now` stubbed |
| 131 | +- Test `should dismiss on downward swipe past threshold (down drawer)` ×4 sides |
| 132 | +- Test `should cancel on reversal within threshold` |
| 133 | +- Test `should snap to nearest point on release (with snap points)` |
| 134 | +- Test `should dismiss from lowest snap if targetOffset past close threshold` |
| 135 | +- Test `should advance sequentially on fast swipe (sequential mode)` |
| 136 | + |
| 137 | +Keep existing `Drawer.test.ts` for a11y/keyboard/click tests; fix `SpyInstance` import. |
| 138 | + |
| 139 | +## Phase 5 — Verify & commit |
| 140 | + |
| 141 | +- `pnpm --filter @reka-ui/core typecheck` (or the project's typecheck command) |
| 142 | +- `pnpm --filter @reka-ui/core test Drawer` |
| 143 | +- Commit per phase for easy review/bisect |
0 commit comments