@m3ui-vue/m3ui-vue 1.0.0 → 1.0.2

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.
Files changed (54) hide show
  1. package/dist/API.md +42 -15
  2. package/dist/{MTextField-TZ7lfr09.js → MTextField-BPbFeI9M.js} +76 -73
  3. package/dist/MTextField-BPbFeI9M.js.map +1 -0
  4. package/dist/api.json +1 -1
  5. package/dist/chart.js +1 -1
  6. package/dist/chart.js.map +1 -1
  7. package/dist/components/MNavigationDrawer.vue.d.ts +9 -6
  8. package/dist/components/MShader.vue.d.ts +9 -3
  9. package/dist/components/MTransition.vue.d.ts +0 -9
  10. package/dist/composables/_hydration.d.ts +4 -0
  11. package/dist/composables/_springAnimation.d.ts +17 -0
  12. package/dist/composables/themeInitScript.d.ts +21 -0
  13. package/dist/composables/useColorPalette.d.ts +4 -0
  14. package/dist/composables/useTheme.d.ts +1 -0
  15. package/dist/index.d.ts +2 -0
  16. package/dist/m3ui-vue.css +1 -1
  17. package/dist/m3ui.js +3017 -2782
  18. package/dist/m3ui.js.map +1 -1
  19. package/dist/plugin.d.ts +7 -0
  20. package/dist/rich-text-editor.js +1 -1
  21. package/dist/styles.css +1 -1
  22. package/package.json +1 -1
  23. package/src/__tests__/MAppBarWithSearch.test.ts +12 -0
  24. package/src/__tests__/MBottomSheetScaffold.test.ts +21 -0
  25. package/src/__tests__/MNavigationDrawer.test.ts +185 -0
  26. package/src/__tests__/MNavigationRail.test.ts +17 -1
  27. package/src/__tests__/MPullToRefresh.test.ts +18 -1
  28. package/src/__tests__/MShader.test.ts +16 -0
  29. package/src/__tests__/MTransition.test.ts +12 -0
  30. package/src/__tests__/hydration.test.ts +86 -0
  31. package/src/__tests__/sheets.test.ts +19 -15
  32. package/src/__tests__/ssr.test.ts +109 -0
  33. package/src/__tests__/themeInitScript.test.ts +104 -0
  34. package/src/components/MBottomSheetScaffold.vue +23 -2
  35. package/src/components/MChart.vue +3 -2
  36. package/src/components/MContextMenu.vue +34 -33
  37. package/src/components/MLightbox.vue +6 -1
  38. package/src/components/MMenu.vue +34 -33
  39. package/src/components/MNavigationDrawer.vue +297 -37
  40. package/src/components/MNavigationRail.vue +69 -15
  41. package/src/components/MPullToRefresh.vue +18 -0
  42. package/src/components/MSearchBar.vue +8 -4
  43. package/src/components/MShader.vue +11 -3
  44. package/src/components/MTransition.vue +36 -41
  45. package/src/composables/_hydration.ts +10 -0
  46. package/src/composables/_springAnimation.ts +74 -0
  47. package/src/composables/_useSheetMotion.ts +101 -88
  48. package/src/composables/themeInitScript.ts +49 -0
  49. package/src/composables/useColorPalette.ts +10 -6
  50. package/src/composables/useTheme.ts +12 -6
  51. package/src/composables/useWindowSizeClass.ts +14 -4
  52. package/src/index.ts +2 -0
  53. package/src/plugin.ts +24 -11
  54. package/dist/MTextField-TZ7lfr09.js.map +0 -1
@@ -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;
@@ -12,6 +12,7 @@ import { computed, ref, watch, onUnmounted } from 'vue'
12
12
  import MIcon from './MIcon.vue'
13
13
  import MBadge from './MBadge.vue'
14
14
  import { useItemLink, type ItemLink } from '../composables/_useItemLink'
15
+ import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from '../composables/_springAnimation'
15
16
 
16
17
  export interface NavRailItem {
17
18
  value: string | number
@@ -162,35 +163,79 @@ function springValue(
162
163
  }
163
164
 
164
165
  // ── Dismissible panel (hideOnCollapse): slide + swipe ─────────────────────────────────────────
165
- // The panel's x runs from -width (off-screen) to 0, and the scrim's opacity follows it.
166
+ // The panel's x runs from -1 (off-screen, a fraction of its width) to 0, and the scrim's opacity follows it.
167
+ // Slides play as Web Animations (see _springAnimation.ts), on the compositor, so the open isn't janked by the
168
+ // panel mounting at the same moment; only the drag writes the transform directly.
166
169
  let panelX = 0
167
170
  let releaseVelocity = 0
171
+ let slideAnim: Animation | null = null
172
+ let slideScrimAnim: Animation | null = null
173
+ let slideSamples: number[] = []
174
+ let slidePanel: HTMLElement | null = null
175
+ let slideScrim: HTMLElement | null = null
176
+ let slideDone: (() => void) | null = null
168
177
 
169
178
  function setSlide(panel: HTMLElement, scrim: HTMLElement | null, x: number) {
170
179
  panelX = x
171
- panel.style.transform = `translateX(${x}px)`
172
- if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, 1 + x / (panel.offsetWidth || PANEL_WIDTH))))
180
+ panel.style.transform = `translateX(${x * 100}%)`
181
+ if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, 1 + x)))
173
182
  }
174
183
 
184
+ // Freezes the panel where the running slide has it
185
+ function stopSlide() {
186
+ if (!slideAnim) return
187
+ if (slidePanel) setSlide(slidePanel, slideScrim, sampleAt(slideSamples, slideAnim.currentTime))
188
+ slideAnim.cancel()
189
+ slideScrimAnim?.cancel()
190
+ slideAnim = slideScrimAnim = null
191
+ }
192
+
193
+ function finishSlide() {
194
+ const cb = slideDone
195
+ slideDone = null
196
+ cb?.()
197
+ }
198
+
199
+ // Without `done`, a pending Transition callback (an enter interrupted by a swipe) is kept for this slide
175
200
  function slide(el: Element, to: number, done?: () => void) {
176
201
  const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
177
202
  const scrim = (el as HTMLElement).querySelector<HTMLElement>('.wnr-scrim')
178
- if (!panel) return done?.()
203
+ if (done) {
204
+ finishSlide()
205
+ slideDone = done
206
+ }
207
+ if (!panel) return finishSlide()
179
208
  if (scrim) scrim.style.transition = 'none'
209
+ stopSlide()
210
+ const from = panelX
180
211
  const v = releaseVelocity
181
212
  releaseVelocity = 0
182
- springValue(panel, panelX, to, (x) => setSlide(panel, scrim, x), SLIDE_STIFFNESS, SLIDE_DAMPING, v, done)
213
+ // The end state goes inline underneath, so nothing jumps when the animation ends
214
+ setSlide(panel, scrim, to)
215
+ if (prefersReducedMotion() || from === to) return finishSlide()
216
+ slideSamples = sampleSpring(from, to, v, { dampingRatio: SLIDE_DAMPING, stiffness: SLIDE_STIFFNESS }, 0.001)
217
+ const a = playSamples(panel, slideSamples, (x) => ({ transform: `translateX(${x * 100}%)` }))
218
+ if (!a) return finishSlide()
219
+ slideScrimAnim = playSamples(scrim, slideSamples, (x) => ({ opacity: String(Math.min(1, Math.max(0, 1 + x))) }))
220
+ slideAnim = a
221
+ slidePanel = panel
222
+ slideScrim = scrim
223
+ a.onfinish = () => {
224
+ if (slideAnim !== a) return
225
+ slideAnim = slideScrimAnim = null
226
+ finishSlide()
227
+ }
183
228
  }
184
229
 
185
230
  // AnchoredDraggableDefaults: settles at the nearer anchor (positionalThreshold 50%) unless released faster
186
231
  // than 125dp/s, which goes the way of the fling
187
232
  const FLING_VELOCITY = 125
188
- let drag: { id: number; x: number; y: number; startX: number; active: boolean; samples: { t: number; x: number }[] } | null = null
233
+ let drag: { id: number; x: number; y: number; startX: number; width: number; active: boolean; samples: { t: number; x: number }[] } | null = null
189
234
  let suppressClick = false
190
235
 
191
236
  function onPanelPointerDown(e: PointerEvent) {
192
237
  if (!props.hideOnCollapse || e.button !== 0) return
193
- drag = { id: e.pointerId, x: e.clientX, y: e.clientY, startX: panelX, active: false, samples: [{ t: e.timeStamp, x: e.clientX }] }
238
+ drag = { id: e.pointerId, x: e.clientX, y: e.clientY, startX: 0, width: 1, active: false, samples: [{ t: e.timeStamp, x: e.clientX }] }
194
239
  }
195
240
 
196
241
  function onPanelPointerMove(e: PointerEvent) {
@@ -202,12 +247,15 @@ function onPanelPointerMove(e: PointerEvent) {
202
247
  if (Math.abs(dx) < 8) return
203
248
  drag.active = true
204
249
  ;(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId)
250
+ // Stop a running slide; the finger owns the panel from where it is
251
+ stopSlide()
252
+ drag.startX = panelX
253
+ drag.width = (e.currentTarget as HTMLElement).offsetWidth || PANEL_WIDTH
205
254
  }
206
255
  const panel = e.currentTarget as HTMLElement
207
- springTokens.set(panel, (springTokens.get(panel) ?? 0) + 1) // stop a running slide
208
256
  const scrim = panel.parentElement?.querySelector<HTMLElement>('.wnr-scrim') ?? null
209
257
  if (scrim) scrim.style.transition = 'none'
210
- setSlide(panel, scrim, Math.min(0, Math.max(-(panel.offsetWidth || PANEL_WIDTH), drag.startX + dx)))
258
+ setSlide(panel, scrim, Math.min(0, Math.max(-1, drag.startX + dx / drag.width)))
211
259
  drag.samples.push({ t: e.timeStamp, x: e.clientX })
212
260
  drag.samples = drag.samples.filter((p) => e.timeStamp - p.t < 100)
213
261
  }
@@ -219,14 +267,14 @@ function onPanelPointerUp(e: PointerEvent) {
219
267
  const lastSample = drag.samples[drag.samples.length - 1]!
220
268
  const dt = (lastSample.t - first.t) / 1000
221
269
  const velocity = dt > 0 ? (lastSample.x - first.x) / dt : 0
270
+ const width = drag.width
222
271
  drag = null
223
272
  if (!wasActive) return
224
273
  suppressClick = true
225
274
  setTimeout(() => { suppressClick = false })
226
275
  const panel = e.currentTarget as HTMLElement
227
- const width = panel.offsetWidth || PANEL_WIDTH
228
- const dismiss = Math.abs(velocity) > FLING_VELOCITY ? velocity < 0 : panelX < -width / 2
229
- releaseVelocity = velocity
276
+ const dismiss = Math.abs(velocity) > FLING_VELOCITY ? velocity < 0 : panelX < -0.5
277
+ releaseVelocity = velocity / width
230
278
  if (dismiss) close()
231
279
  else slide(panel.parentElement!, 0)
232
280
  }
@@ -246,9 +294,14 @@ function onPanelEnter(el: Element, done: () => void) {
246
294
  if (props.hideOnCollapse) {
247
295
  // Standalone modal: the items are expanded from the start, the panel slides in from off-screen
248
296
  panelExpanded.value = true
297
+ // Reopened mid-close: the old panel goes now
298
+ finishSlide()
299
+ slideAnim?.cancel()
300
+ slideScrimAnim?.cancel()
301
+ slideAnim = slideScrimAnim = null
249
302
  const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
250
303
  const scrim = (el as HTMLElement).querySelector<HTMLElement>('.wnr-scrim')
251
- if (panel) setSlide(panel, scrim, -(panel.offsetWidth || PANEL_WIDTH))
304
+ if (panel) setSlide(panel, scrim, -1)
252
305
  slide(el, 0, done)
253
306
  return
254
307
  }
@@ -269,8 +322,7 @@ function onPanelEnter(el: Element, done: () => void) {
269
322
  }
270
323
  function onPanelLeave(el: Element, done: () => void) {
271
324
  if (props.hideOnCollapse) {
272
- const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
273
- slide(el, -(panel?.offsetWidth || PANEL_WIDTH), done)
325
+ slide(el, -1, done)
274
326
  return
275
327
  }
276
328
  const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
@@ -295,6 +347,8 @@ watch(
295
347
  )
296
348
 
297
349
  onUnmounted(() => {
350
+ slideAnim?.cancel()
351
+ slideScrimAnim?.cancel()
298
352
  document.removeEventListener('keydown', onKeydown)
299
353
  if (props.modal && props.expanded) document.body.style.overflow = ''
300
354
  })
@@ -89,6 +89,18 @@ function canStart() {
89
89
  return !props.disabled && !props.refreshing && !committed && !!scrollEl.value && scrollEl.value.scrollTop <= 0;
90
90
  }
91
91
 
92
+ // While a pull can start, the scroller only allows the finger moving up (pan-down): a touch-action that lets the
93
+ // browser pan along the pull makes Chrome deliver it at 60Hz on a 120Hz phone, as a possible scroll waiting on
94
+ // our touchmove listener (see CLAUDE.md). Scrolled, refreshing or disabled, it pans both ways. Browsers without
95
+ // directional pan values (Safari) keep pan-y.
96
+ function updateTouchAction() {
97
+ const el = scrollEl.value;
98
+ if (!el) return;
99
+ const directional = typeof CSS !== "undefined" && !!CSS.supports?.("touch-action", "pan-down");
100
+ const action = canStart() && directional ? "pan-down" : "pan-y";
101
+ if (el.style.touchAction !== action) el.style.touchAction = action;
102
+ }
103
+
92
104
  function beginPull(y: number) {
93
105
  startY = y;
94
106
  pulling = true;
@@ -125,6 +137,7 @@ function finishPull() {
125
137
  hideIndicator(true);
126
138
  }
127
139
  pullDistance = 0;
140
+ updateTouchAction();
128
141
  }
129
142
 
130
143
  // Mouse/pen: pointer events. Touch is handled separately below — a touch's
@@ -220,8 +233,10 @@ watch(
220
233
  committed = false;
221
234
  hideIndicator(true);
222
235
  }
236
+ updateTouchAction();
223
237
  },
224
238
  );
239
+ watch(() => props.disabled, updateTouchAction);
225
240
 
226
241
  // The indicator's transform/opacity are owned entirely by the functions
227
242
  // above, written directly to the DOM — never bound reactively in the
@@ -238,6 +253,8 @@ onMounted(() => {
238
253
  // Must be non-passive: preventDefault() here is what stops the native scroll.
239
254
  el.addEventListener("touchmove", onTouchMove, { passive: false });
240
255
  el.addEventListener("touchend", onTouchEnd);
256
+ el.addEventListener("scroll", updateTouchAction, { passive: true });
257
+ updateTouchAction();
241
258
  el.addEventListener("touchcancel", onTouchEnd);
242
259
  });
243
260
  onBeforeUnmount(() => {
@@ -247,6 +264,7 @@ onBeforeUnmount(() => {
247
264
  el.removeEventListener("touchstart", onTouchStart);
248
265
  el.removeEventListener("touchmove", onTouchMove);
249
266
  el.removeEventListener("touchend", onTouchEnd);
267
+ el.removeEventListener("scroll", updateTouchAction);
250
268
  el.removeEventListener("touchcancel", onTouchEnd);
251
269
  });
252
270
  </script>
@@ -1,7 +1,10 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
3
  * A collapsed search field that expands into a docked panel or a fullscreen overlay, with results shown below
4
- * the input.
4
+ * the input. The expanded panel is teleported to `<body>`; `class`, `style` and other attributes you pass land on
5
+ * the collapsed bar. So with a fullscreen variant you can hide the bar (e.g. `class="hidden"` on phones) and open
6
+ * search from your own icon button through `v-model:expanded`. The docked panel is placed under the bar, so it
7
+ * needs the bar visible.
5
8
  *
6
9
  * @slot leading Overrides the collapsed bar's leading icon
7
10
  * @slot trailing Trailing content in both the collapsed bar and the expanded header (e.g. an avatar)
@@ -155,13 +158,14 @@ onUnmounted(() => { document.body.style.overflow = '' })
155
158
 
156
159
  <template>
157
160
  <div ref="barEl" class="relative w-full" v-bind="$attrs">
161
+ <!-- Flat, told apart by its color: Compose's SearchBarDefaults.ShadowElevation is Level0 for every variant
162
+ (SearchBarTokens.ContainerElevation's Level3 isn't used by any of them) -->
158
163
  <div
159
164
  v-show="!expanded"
160
- class="flex h-14 items-center gap-3 rounded-full px-4 transition-[box-shadow,background-color] duration-150 focus-within:outline focus-within:outline-2 focus-within:outline-secondary focus-within:-outline-offset-2"
165
+ class="flex h-14 items-center gap-3 rounded-full px-4 transition-[background-color] duration-150 focus-within:outline focus-within:outline-2 focus-within:outline-secondary focus-within:-outline-offset-2"
161
166
  :class="[
162
167
  disabled ? 'pointer-events-none opacity-[0.38]' : '',
163
168
  appBar?.scrolled.value ? 'bg-surface-container-highest' : 'bg-surface-container-high',
164
- appBar ? '' : 'shadow-elevation-3',
165
169
  ]"
166
170
  >
167
171
  <span class="inline-flex shrink-0 items-center" :class="iconPosition === 'end' ? 'order-last' : ''">
@@ -202,7 +206,7 @@ onUnmounted(() => { document.body.style.overflow = '' })
202
206
  ref="panelEl"
203
207
  class="fixed z-500 flex flex-col overflow-hidden"
204
208
  :class="[
205
- fullscreen ? 'inset-0' : 'max-h-[70vh] rounded-[28px] shadow-elevation-3',
209
+ fullscreen ? 'inset-0' : 'max-h-[70vh] rounded-[28px]',
206
210
  contained ? 'bg-surface-container-low' : 'bg-surface-container-high',
207
211
  ]"
208
212
  :style="variant === 'docked' ? panelPos : undefined"