@m3ui-vue/m3ui-vue 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -185,16 +185,15 @@ function openTrajectory() {
185
185
  // hook on the same still-mounted element without waiting for `done()`. Both
186
186
  // `currentIdx` (how far along the trajectory we are, so a reversal continues
187
187
  // smoothly from wherever it currently is instead of snapping back to a fixed
188
- // start) and `cancelActive` (so the previous rAF loop actually stops instead
189
- // of running alongside the new one, fighting over the same style every
190
- // frame) have to live outside this function to survive that interruption.
188
+ // start) and `cancelActive` (so the previous animation actually stops instead
189
+ // of running alongside the new one, fighting over the same style) have to live outside this function to survive that interruption.
191
190
  let currentIdx = 0
192
191
  let cancelActive: (() => void) | null = null
193
- // Set only while a close is playing: stops the loop *and* calls its `done()`.
192
+ // Set only while a close is playing: stops the animation *and* calls its `done()`.
194
193
  // Used on unmount — a menu item that navigates away (e.g. "Log out" ->
195
194
  // router.push) unmounts this component mid-close, and Vue only removes the
196
195
  // Teleported panel from <body> inside the leave's `done()`, so merely
197
- // cancelling the loop there left the half-closed panel stranded on screen.
196
+ // cancelling the animation there left the half-closed panel stranded on screen.
198
197
  let finishLeave: (() => void) | null = null
199
198
 
200
199
  function playTrajectory(el: Element, done: () => void, opening: boolean) {
@@ -202,37 +201,39 @@ function playTrajectory(el: Element, done: () => void, opening: boolean) {
202
201
  const htmlEl = el as HTMLElement
203
202
  const samples = openTrajectory()
204
203
  const last = samples.length - 1
205
- const direction = opening ? 1 : -1
206
- const target = opening ? last : 0
207
- let lastTime = 0
208
- let rafId = 0
209
- let cancelled = false
210
-
211
- function apply(idx: number) {
212
- const sample = samples[Math.round(Math.max(0, Math.min(last, idx)))]!
213
- htmlEl.style.transform = `scale(${sample.scale})`
214
- htmlEl.style.opacity = String(Math.min(1, Math.max(0, sample.alpha)))
204
+ const from = Math.round(Math.max(0, Math.min(last, currentIdx)))
205
+ const to = opening ? last : 0
206
+ const step = opening ? 1 : -1
207
+ const frames: { scale: number, alpha: number }[] = []
208
+ for (let i = from; opening ? i <= to : i >= to; i += step) frames.push(samples[i]!)
209
+ const style = (s: { scale: number, alpha: number }) => ({
210
+ transform: `scale(${s.scale})`,
211
+ opacity: String(Math.min(1, Math.max(0, s.alpha))),
212
+ })
213
+ // Played as a Web Animation (see _springAnimation.ts), which the compositor runs, so the open isn't janked by
214
+ // the panel's content mounting at the same moment. The end state goes inline underneath it.
215
+ Object.assign(htmlEl.style, style(samples[to]!))
216
+ let ended = false
217
+ function complete() {
218
+ if (ended) return
219
+ ended = true
220
+ currentIdx = to
221
+ cancelActive = null
222
+ finishLeave = null
223
+ done()
215
224
  }
216
-
217
- function step(now: number) {
218
- if (cancelled) return
219
- const dtSteps = lastTime ? (now - lastTime) / 1000 / trajectoryStep : 1
220
- lastTime = now
221
- currentIdx = Math.max(0, Math.min(last, currentIdx + direction * dtSteps))
222
- apply(currentIdx)
223
- if ((opening && currentIdx >= target) || (!opening && currentIdx <= target)) {
224
- cancelActive = null
225
- finishLeave = null
226
- done()
227
- return
228
- }
229
- rafId = requestAnimationFrame(step)
225
+ if (frames.length < 2 || typeof htmlEl.animate !== 'function') return complete()
226
+ const anim = htmlEl.animate(frames.map(style), { duration: (frames.length - 1) * trajectoryStep * 1000, easing: 'linear' })
227
+ anim.finished.catch(() => {})
228
+ anim.onfinish = complete
229
+ cancelActive = () => {
230
+ // Remember where it got to, so the reversal continues from there
231
+ const t = Number(anim.currentTime ?? 0) / (trajectoryStep * 1000)
232
+ currentIdx = Math.max(0, Math.min(last, from + step * t))
233
+ ended = true
234
+ anim.cancel()
230
235
  }
231
-
232
- cancelActive = () => { cancelled = true; cancelAnimationFrame(rafId) }
233
236
  finishLeave = opening ? null : () => { cancelActive?.(); cancelActive = null; finishLeave = null; done() }
234
- apply(currentIdx)
235
- rafId = requestAnimationFrame(step)
236
237
  }
237
238
  function panelEnter(el: Element, done: () => void) {
238
239
  playTrajectory(el, done, true)
@@ -170,16 +170,15 @@ function openTrajectory() {
170
170
  // hook on the same still-mounted element without waiting for `done()`. Both
171
171
  // `currentIdx` (how far along the trajectory we are, so a reversal continues
172
172
  // smoothly from wherever it currently is instead of snapping back to a fixed
173
- // start) and `cancelActive` (so the previous rAF loop actually stops instead
174
- // of running alongside the new one, fighting over the same style every
175
- // frame) have to live outside this function to survive that interruption.
173
+ // start) and `cancelActive` (so the previous animation actually stops instead
174
+ // of running alongside the new one, fighting over the same style) have to live outside this function to survive that interruption.
176
175
  let currentIdx = 0
177
176
  let cancelActive: (() => void) | null = null
178
- // Set only while a close is playing: stops the loop *and* calls its `done()`.
177
+ // Set only while a close is playing: stops the animation *and* calls its `done()`.
179
178
  // Used on unmount — a menu item that navigates away (e.g. "Log out" ->
180
179
  // router.push) unmounts this component mid-close, and Vue only removes the
181
180
  // Teleported panel from <body> inside the leave's `done()`, so merely
182
- // cancelling the loop there left the half-closed panel stranded on screen.
181
+ // cancelling the animation there left the half-closed panel stranded on screen.
183
182
  let finishLeave: (() => void) | null = null
184
183
 
185
184
  function playTrajectory(el: Element, done: () => void, opening: boolean) {
@@ -187,37 +186,39 @@ function playTrajectory(el: Element, done: () => void, opening: boolean) {
187
186
  const htmlEl = el as HTMLElement
188
187
  const samples = openTrajectory()
189
188
  const last = samples.length - 1
190
- const direction = opening ? 1 : -1
191
- const target = opening ? last : 0
192
- let lastTime = 0
193
- let rafId = 0
194
- let cancelled = false
195
-
196
- function apply(idx: number) {
197
- const sample = samples[Math.round(Math.max(0, Math.min(last, idx)))]!
198
- htmlEl.style.transform = `scale(${sample.scale})`
199
- htmlEl.style.opacity = String(Math.min(1, Math.max(0, sample.alpha)))
189
+ const from = Math.round(Math.max(0, Math.min(last, currentIdx)))
190
+ const to = opening ? last : 0
191
+ const step = opening ? 1 : -1
192
+ const frames: { scale: number, alpha: number }[] = []
193
+ for (let i = from; opening ? i <= to : i >= to; i += step) frames.push(samples[i]!)
194
+ const style = (s: { scale: number, alpha: number }) => ({
195
+ transform: `scale(${s.scale})`,
196
+ opacity: String(Math.min(1, Math.max(0, s.alpha))),
197
+ })
198
+ // Played as a Web Animation (see _springAnimation.ts), which the compositor runs, so the open isn't janked by
199
+ // the panel's content mounting at the same moment. The end state goes inline underneath it.
200
+ Object.assign(htmlEl.style, style(samples[to]!))
201
+ let ended = false
202
+ function complete() {
203
+ if (ended) return
204
+ ended = true
205
+ currentIdx = to
206
+ cancelActive = null
207
+ finishLeave = null
208
+ done()
200
209
  }
201
-
202
- function step(now: number) {
203
- if (cancelled) return
204
- const dtSteps = lastTime ? (now - lastTime) / 1000 / trajectoryStep : 1
205
- lastTime = now
206
- currentIdx = Math.max(0, Math.min(last, currentIdx + direction * dtSteps))
207
- apply(currentIdx)
208
- if ((opening && currentIdx >= target) || (!opening && currentIdx <= target)) {
209
- cancelActive = null
210
- finishLeave = null
211
- done()
212
- return
213
- }
214
- rafId = requestAnimationFrame(step)
210
+ if (frames.length < 2 || typeof htmlEl.animate !== 'function') return complete()
211
+ const anim = htmlEl.animate(frames.map(style), { duration: (frames.length - 1) * trajectoryStep * 1000, easing: 'linear' })
212
+ anim.finished.catch(() => {})
213
+ anim.onfinish = complete
214
+ cancelActive = () => {
215
+ // Remember where it got to, so the reversal continues from there
216
+ const t = Number(anim.currentTime ?? 0) / (trajectoryStep * 1000)
217
+ currentIdx = Math.max(0, Math.min(last, from + step * t))
218
+ ended = true
219
+ anim.cancel()
215
220
  }
216
-
217
- cancelActive = () => { cancelled = true; cancelAnimationFrame(rafId) }
218
221
  finishLeave = opening ? null : () => { cancelActive?.(); cancelActive = null; finishLeave = null; done() }
219
- apply(currentIdx)
220
- rafId = requestAnimationFrame(step)
221
222
  }
222
223
  function panelEnter(el: Element, done: () => void) {
223
224
  playTrajectory(el, done, true)
@@ -1,7 +1,8 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
3
  * A slide-out or inline drawer of grouped, badge-able, optionally-nested navigation items — modal (Teleported,
4
- * scrim-backed) or inline (participates in layout, supports a collapsed icon-only mode).
4
+ * scrim-backed, swiped closed toward its edge) or inline (participates in layout, supports a collapsed
5
+ * icon-only mode).
5
6
  *
6
7
  * @slot header Custom header content (e.g. profile card, logo). Fully generic, no positioning constraints
7
8
  * applied — overrides the title prop.
@@ -13,9 +14,11 @@
13
14
  * @event update:modelValue Open/closed state for v-model
14
15
  * @event select Fires when an item is clicked — use to track the selected item
15
16
  */
16
- import { ref, reactive, watch, provide, computed } from "vue";
17
+ import { ref, reactive, watch, provide, computed, nextTick, onBeforeUnmount } from "vue";
17
18
  import MIcon from "./MIcon.vue";
18
19
  import MDrawerItemList from "./_MDrawerItemList.vue";
20
+ import { motionSprings, type SpringSpec } from "../composables/motion";
21
+ import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from "../composables/_springAnimation";
19
22
 
20
23
  export interface DrawerItem {
21
24
  value: string | number;
@@ -61,8 +64,10 @@ const props = withDefaults(
61
64
  * variant always renders on left or right.
62
65
  */
63
66
  side?: 'left' | 'right' | 'top' | 'bottom';
67
+ /** Lets the modal drawer be swiped closed toward its edge; with false it only closes through v-model or the scrim */
68
+ swipeEnabled?: boolean;
64
69
  }>(),
65
- { modal: true, side: 'left' },
70
+ { modal: true, side: 'left', swipeEnabled: true },
66
71
  );
67
72
 
68
73
  const emit = defineEmits<{
@@ -79,6 +84,7 @@ function isSectionOpen(section: DrawerSection, index: number) {
79
84
  }
80
85
 
81
86
  function toggleSection(section: DrawerSection, index: number) {
87
+ refreshListTouchAction();
82
88
  const key = section.title ?? `__${index}`;
83
89
  openSections.value[key] = !isSectionOpen(section, index);
84
90
  }
@@ -88,6 +94,7 @@ function isItemOpen(item: DrawerItem) {
88
94
  }
89
95
 
90
96
  function toggleItem(item: DrawerItem) {
97
+ refreshListTouchAction();
91
98
  openItems[item.value] = !openItems[item.value];
92
99
  }
93
100
 
@@ -110,6 +117,7 @@ function onChildSelect(item: DrawerItem) {
110
117
  }
111
118
 
112
119
  function onChildToggle(item: DrawerItem) {
120
+ refreshListTouchAction();
113
121
  openItems[item.value] = !openItems[item.value];
114
122
  }
115
123
 
@@ -153,12 +161,276 @@ watch(
153
161
  }
154
162
  },
155
163
  );
164
+
165
+ // ── Modal: slide in/out + swipe to close ────────────────────────────────────────────────────
166
+ // After ModalNavigationDrawer (NavigationDrawer.kt), which is left/right only; top/bottom add a bottom sheet's
167
+ // nested scroll (see moveDrag). Opens with DefaultSpatial (0.9 / 700), closes with
168
+ // FastEffects (1.0 / 3800); a released swipe settles at the nearer anchor (DrawerPositionalThreshold 50%)
169
+ // unless flung faster than DrawerVelocityThreshold (400dp/s), with DefaultSpatial. The scrim's alpha follows
170
+ // the panel's offset.
171
+ //
172
+ // Each spring plays as a Web Animation (see _springAnimation.ts): a rAF loop janked the open on phones. Only the
173
+ // drag itself writes styles directly. Offsets are fractions of the panel's size, so opening never reads layout.
174
+ const DEFAULT_SPATIAL = motionSprings.standard.defaultSpatial
175
+ const FAST_EFFECTS = motionSprings.standard.fastEffects
176
+ const FLING_VELOCITY = 400
177
+
178
+ let panelEl: HTMLElement | null = null
179
+ let scrimEl: HTMLElement | null = null
180
+ // 0 = open, `closedDir` (-1 for left/top, 1 for right/bottom) = off-screen, in fractions of the panel's size
181
+ let offset = 0
182
+ let velocity = 0
183
+ let closedDir = -1
184
+ let anim: Animation | null = null
185
+ let scrimAnim: Animation | null = null
186
+ let samples: number[] = []
187
+ let settled: (() => void) | null = null
188
+
189
+ const horizontal = () => props.side === "left" || props.side === "right";
190
+ const transformAt = (p: number) => (horizontal() ? `translateX(${p * 100}%)` : `translateY(${p * 100}%)`);
191
+ const opacityAt = (p: number) => String(Math.min(1, Math.max(0, 1 - p / closedDir)));
192
+
193
+ function bindModal(root: Element) {
194
+ panelEl = root.querySelector<HTMLElement>(".nd-panel");
195
+ scrimEl = root.querySelector<HTMLElement>(".nd-scrim");
196
+ closedDir = props.side === "left" || props.side === "top" ? -1 : 1;
197
+ }
198
+
199
+ function writeModal() {
200
+ if (panelEl) panelEl.style.transform = transformAt(offset);
201
+ if (scrimEl) scrimEl.style.opacity = opacityAt(offset);
202
+ }
203
+
204
+ // Freezes the panel where the running animation has it
205
+ function stopAnim() {
206
+ if (!anim) return;
207
+ offset = sampleAt(samples, anim.currentTime);
208
+ writeModal();
209
+ anim.cancel();
210
+ scrimAnim?.cancel();
211
+ anim = scrimAnim = null;
212
+ }
213
+
214
+ function settle() {
215
+ const cb = settled;
216
+ settled = null;
217
+ cb?.();
218
+ }
219
+
220
+ // Without `done`, a pending Transition callback (an enter interrupted by a swipe) is kept for this run
221
+ function animateTo(to: number, spring: SpringSpec, done?: () => void) {
222
+ if (done) {
223
+ settled?.();
224
+ settled = done;
225
+ }
226
+ stopAnim();
227
+ const v = velocity;
228
+ velocity = 0;
229
+ const from = offset;
230
+ // The end state goes inline underneath, so nothing jumps when the animation ends
231
+ offset = to;
232
+ writeModal();
233
+ if (prefersReducedMotion() || from === to) return settle();
234
+ samples = sampleSpring(from, to, v, spring, 0.001);
235
+ const a = playSamples(panelEl, samples, (p) => ({ transform: transformAt(p) }));
236
+ if (!a) return settle();
237
+ scrimAnim = playSamples(scrimEl, samples, (p) => ({ opacity: opacityAt(p) }));
238
+ anim = a;
239
+ a.onfinish = () => {
240
+ if (anim !== a) return;
241
+ anim = scrimAnim = null;
242
+ settle();
243
+ };
244
+ }
245
+
246
+ // Top/bottom: wherever the touch-action lets the browser pan along the drawer's swipe, Chrome treats that drag as
247
+ // a possible scroll waiting on our touchmove listener and delivers it at 60Hz on a 120Hz phone (measured on a
248
+ // device: the panel visibly moved every other frame, while the left/right swipe, crosswise to pan-y, came at
249
+ // 120Hz). So the list only allows the scroll that can actually happen: at the edge the drawer closes toward,
250
+ // only scrolling away from it (bottom: pan-down, the finger moving up; top: pan-up), elsewhere both ways. Outside
251
+ // the list the panel is touch-action: none. Browsers without directional pan values (Safari) keep pan-y.
252
+ let listEl: HTMLElement | null = null;
253
+
254
+ function updateListTouchAction() {
255
+ const list = listEl;
256
+ if (!list || horizontal() || !props.swipeEnabled) return;
257
+ const atEdge = props.side === "bottom"
258
+ ? list.scrollTop <= 0
259
+ : list.scrollTop + list.clientHeight >= list.scrollHeight - 1;
260
+ const directional = typeof CSS !== "undefined" && !!CSS.supports?.("touch-action", "pan-down");
261
+ list.style.touchAction = atEdge && directional ? (props.side === "bottom" ? "pan-down" : "pan-up") : "pan-y";
262
+ }
263
+
264
+ // Expanding a section or item changes how far the list scrolls without a scroll event: re-check once it settles
265
+ function refreshListTouchAction() {
266
+ setTimeout(updateListTouchAction, 400);
267
+ }
268
+
269
+ function watchList(root: Element | null) {
270
+ listEl?.removeEventListener("scroll", updateListTouchAction);
271
+ listEl = root?.querySelector<HTMLElement>("[data-nd-list]") ?? null;
272
+ if (!listEl) return;
273
+ listEl.addEventListener("scroll", updateListTouchAction, { passive: true });
274
+ // After the first layout, so the open doesn't force one
275
+ requestAnimationFrame(updateListTouchAction);
276
+ }
277
+
278
+ function onModalEnter(el: Element, done: () => void) {
279
+ // Reopened mid-close: the old panel goes now
280
+ settled?.();
281
+ settled = null;
282
+ anim?.cancel();
283
+ scrimAnim?.cancel();
284
+ anim = scrimAnim = null;
285
+ bindModal(el);
286
+ watchList(el);
287
+ offset = closedDir;
288
+ velocity = 0;
289
+ animateTo(0, DEFAULT_SPATIAL, done);
290
+ }
291
+
292
+ function onModalLeave(el: Element, done: () => void) {
293
+ bindModal(el);
294
+ drag = null;
295
+ watchList(null);
296
+ animateTo(closedDir, FAST_EFFECTS, done);
297
+ }
298
+
299
+ onBeforeUnmount(() => {
300
+ anim?.cancel();
301
+ scrimAnim?.cancel();
302
+ watchList(null);
303
+ });
304
+
305
+ // Positions below are along the drawer's axis: x for left/right, y for top/bottom.
306
+ //
307
+ // A left/right drawer's swipe is crosswise to the list's scroll, so pointer events do (touch-action pan-y leaves
308
+ // the horizontal axis to us). A top/bottom drawer's swipe runs along it, and touch-action is fixed when the finger
309
+ // lands, so a touch there goes through touch events instead, like MPullToRefresh: the first move decides, and from
310
+ // that very touchmove on preventDefault() keeps the browser out. Deciding a few pointermoves later (as for mouse)
311
+ // let the browser start its own pan or overscroll first, and the two fought: the drag lagged on phones.
312
+ let drag: { id: number; touch: boolean; main: number; cross: number; start: number; size: number; active: boolean; list: HTMLElement | null; samples: { t: number; p: number }[] } | null = null;
313
+ let suppressClick = false;
314
+
315
+ function beginDrag(id: number, touch: boolean, x: number, y: number, t: number, target: EventTarget | null) {
316
+ const main = horizontal() ? x : y;
317
+ const list = (target as Element | null)?.closest<HTMLElement>("[data-nd-list]") ?? null;
318
+ drag = { id, touch, main, cross: horizontal() ? y : x, start: 0, size: 1, active: false, list, samples: [{ t, p: main }] };
319
+ }
320
+
321
+ // Returns true when this move started the drag
322
+ function moveDrag(x: number, y: number, t: number, panel: HTMLElement): boolean {
323
+ if (!drag) return false;
324
+ const main = horizontal() ? x : y;
325
+ let started = false;
326
+ if (!drag.active) {
327
+ const d = main - drag.main;
328
+ const dc = (horizontal() ? y : x) - drag.cross;
329
+ if (Math.abs(dc) > 8 && Math.abs(dc) > Math.abs(d)) { drag = null; return false; }
330
+ if (Math.abs(d) < (horizontal() ? 8 : drag.touch ? 2 : 4)) return false;
331
+ if (!horizontal()) {
332
+ if (Math.abs(dc) > Math.abs(d)) { drag = null; return false; }
333
+ bindModal(panel.parentElement!);
334
+ // Like a bottom sheet's nested scroll: dragging away from the edge only scrolls, and a drag toward it
335
+ // scrolls the list first, the drawer following once the list can't scroll that way any more
336
+ const list = drag.list;
337
+ const listScrolls = !!list && (d > 0 ? list.scrollTop > 0 : list.scrollTop + list.clientHeight < list.scrollHeight - 1);
338
+ if (Math.sign(d) !== closedDir || listScrolls) { drag = null; return false; }
339
+ }
340
+ drag.active = true;
341
+ started = true;
342
+ drag.main = main;
343
+ bindModal(panel.parentElement!);
344
+ // Stop a running slide; the finger owns the panel now
345
+ stopAnim();
346
+ velocity = 0;
347
+ drag.start = offset;
348
+ drag.size = (horizontal() ? panel.offsetWidth : panel.offsetHeight) || 1;
349
+ }
350
+ const min = Math.min(0, closedDir);
351
+ const max = Math.max(0, closedDir);
352
+ offset = Math.min(max, Math.max(min, drag.start + (main - drag.main) / drag.size));
353
+ writeModal();
354
+ drag.samples.push({ t, p: main });
355
+ drag.samples = drag.samples.filter((s) => t - s.t < 100);
356
+ return started;
357
+ }
358
+
359
+ function endDrag() {
360
+ if (!drag) return;
361
+ const wasActive = drag.active;
362
+ const first = drag.samples[0]!;
363
+ const lastSample = drag.samples[drag.samples.length - 1]!;
364
+ const dt = (lastSample.t - first.t) / 1000;
365
+ const v = dt > 0 ? (lastSample.p - first.p) / dt : 0;
366
+ const size = drag.size;
367
+ drag = null;
368
+ if (!wasActive) return;
369
+ suppressClick = true;
370
+ setTimeout(() => { suppressClick = false; });
371
+ const dismiss = Math.abs(v) > FLING_VELOCITY
372
+ ? Math.sign(v) === closedDir
373
+ : Math.abs(offset) > 0.5;
374
+ velocity = v / size;
375
+ if (!dismiss) return animateTo(0, DEFAULT_SPATIAL);
376
+ close();
377
+ // The parent may not close it (no v-model bound): spring back instead of staying mid-swipe
378
+ nextTick(() => {
379
+ if (props.modelValue) animateTo(0, DEFAULT_SPATIAL);
380
+ });
381
+ }
382
+
383
+ function onPanelPointerDown(e: PointerEvent) {
384
+ if (!props.swipeEnabled || e.button !== 0) return;
385
+ if (e.pointerType === "touch" && !horizontal()) return;
386
+ beginDrag(e.pointerId, false, e.clientX, e.clientY, e.timeStamp, e.target);
387
+ }
388
+
389
+ function onPanelPointerMove(e: PointerEvent) {
390
+ if (!drag || drag.touch || e.pointerId !== drag.id) return;
391
+ const panel = e.currentTarget as HTMLElement;
392
+ if (moveDrag(e.clientX, e.clientY, e.timeStamp, panel)) panel.setPointerCapture?.(e.pointerId);
393
+ }
394
+
395
+ function onPanelPointerUp(e: PointerEvent) {
396
+ if (!drag || drag.touch || e.pointerId !== drag.id) return;
397
+ endDrag();
398
+ }
399
+
400
+ function onPanelTouchStart(e: TouchEvent) {
401
+ if (!props.swipeEnabled || horizontal() || e.touches.length !== 1) return;
402
+ const t = e.touches[0]!;
403
+ beginDrag(t.identifier, true, t.clientX, t.clientY, e.timeStamp, e.target);
404
+ }
405
+
406
+ function onPanelTouchMove(e: TouchEvent) {
407
+ if (drag?.touch) {
408
+ const id = drag.id;
409
+ const t = Array.from(e.changedTouches).find((x) => x.identifier === id);
410
+ if (t) moveDrag(t.clientX, t.clientY, e.timeStamp, e.currentTarget as HTMLElement);
411
+ }
412
+ // From the very move that started it (top/bottom), or as belt and braces (left/right): the browser must not
413
+ // scroll or navigate with the swipe
414
+ if (drag?.active && e.cancelable) e.preventDefault();
415
+ }
416
+
417
+ function onPanelTouchEnd(e: TouchEvent) {
418
+ if (!drag?.touch) return;
419
+ const id = drag.id;
420
+ if (Array.from(e.changedTouches).some((x) => x.identifier === id)) endDrag();
421
+ }
422
+
423
+ function onPanelClickCapture(e: MouseEvent) {
424
+ if (!suppressClick) return;
425
+ e.preventDefault();
426
+ e.stopPropagation();
427
+ }
156
428
  </script>
157
429
 
158
430
  <template>
159
431
  <!-- ── Modal variant ──────────────────────────────────────── -->
160
432
  <Teleport v-if="modal" to="body">
161
- <Transition name="nd" :duration="{ enter: 300, leave: 280 }" appear>
433
+ <Transition :css="false" @enter="onModalEnter" @leave="onModalLeave" appear>
162
434
  <div
163
435
  v-if="modelValue"
164
436
  class="fixed inset-0 z-100 flex"
@@ -168,20 +440,30 @@ watch(
168
440
  side === 'top' ? 'flex-col' : '',
169
441
  ]"
170
442
  >
171
- <div class="nd-scrim absolute inset-0 bg-scrim/32" @click="close" />
443
+ <!-- will-change: a drag writes their transform/opacity directly, without the layer an animation gets -->
444
+ <div class="nd-scrim absolute inset-0 bg-scrim/32 will-change-[opacity]" @click="close" />
172
445
 
173
446
  <nav
174
447
  v-bind="$attrs"
175
448
  :class="[
176
- 'nd-panel relative flex flex-col bg-surface-container-low shadow-elevation-1',
177
- `nd-panel-${side}`,
449
+ 'nd-panel relative flex flex-col bg-surface-container-low shadow-elevation-1 will-change-transform',
178
450
  side === 'top' || side === 'bottom'
179
451
  ? 'w-full max-h-[85vh]'
180
452
  : 'h-full max-w-[85vw]',
181
453
  side === 'top' || side === 'bottom' || width ? '' : 'w-[22.5rem]',
182
454
  side === 'right' ? 'rounded-l-lg' : side === 'left' ? 'rounded-r-lg' : side === 'bottom' ? 'rounded-t-lg' : '',
455
+ swipeEnabled ? (side === 'left' || side === 'right' ? 'touch-pan-y' : 'touch-none') : '',
183
456
  ]"
184
457
  :style="side !== 'top' && side !== 'bottom' && width ? { width } : undefined"
458
+ @pointerdown="onPanelPointerDown"
459
+ @pointermove="onPanelPointerMove"
460
+ @pointerup="onPanelPointerUp"
461
+ @pointercancel="onPanelPointerUp"
462
+ @touchstart.passive="onPanelTouchStart"
463
+ @touchmove="onPanelTouchMove"
464
+ @touchend="onPanelTouchEnd"
465
+ @touchcancel="onPanelTouchEnd"
466
+ @click.capture="onPanelClickCapture"
185
467
  >
186
468
  <div v-if="$slots.header" class="shrink-0">
187
469
  <slot name="header" />
@@ -192,7 +474,14 @@ watch(
192
474
  </h2>
193
475
  </div>
194
476
 
195
- <div class="scrollbar-none flex min-h-0 flex-1 flex-col overflow-y-auto px-3 py-2">
477
+ <!-- touch-action stops at the nearest scroll container, so this one needs pan-y of its own or a
478
+ finger landing on an item lets the browser take a left/right drawer's swipe (top/bottom: the
479
+ script narrows it, see updateListTouchAction) -->
480
+ <div
481
+ data-nd-list
482
+ class="scrollbar-none flex min-h-0 flex-1 flex-col overflow-y-auto px-3 py-2"
483
+ :class="swipeEnabled ? 'touch-pan-y overscroll-y-contain' : ''"
484
+ >
196
485
  <template v-for="(section, si) in sections" :key="si">
197
486
  <div v-if="si > 0" class="my-1 border-t border-outline-variant" />
198
487
 
@@ -444,35 +733,6 @@ watch(
444
733
  </template>
445
734
 
446
735
  <style scoped>
447
- /* ── Modal transitions ─────────────────────────────────── */
448
- .nd-scrim {
449
- transition: opacity 280ms ease;
450
- }
451
- .nd-enter-from .nd-scrim,
452
- .nd-leave-to .nd-scrim {
453
- opacity: 0;
454
- }
455
-
456
- .nd-panel {
457
- transition: transform 300ms cubic-bezier(0.2, 0, 0, 1);
458
- }
459
- .nd-enter-from .nd-panel-left,
460
- .nd-leave-to .nd-panel-left {
461
- transform: translateX(-100%);
462
- }
463
- .nd-enter-from .nd-panel-right,
464
- .nd-leave-to .nd-panel-right {
465
- transform: translateX(100%);
466
- }
467
- .nd-enter-from .nd-panel-top,
468
- .nd-leave-to .nd-panel-top {
469
- transform: translateY(-100%);
470
- }
471
- .nd-enter-from .nd-panel-bottom,
472
- .nd-leave-to .nd-panel-bottom {
473
- transform: translateY(100%);
474
- }
475
-
476
736
  /* ── Section collapse animation ────────────────────────── */
477
737
  .nd-section-grid {
478
738
  display: grid;