@m3ui-vue/m3ui-vue 1.0.3 → 1.1.0

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 (99) hide show
  1. package/dist/API.md +75 -11
  2. package/dist/MMenuItem-BOkcVtEe.js +809 -0
  3. package/dist/MMenuItem-BOkcVtEe.js.map +1 -0
  4. package/dist/{MSlider-CdNIH7jc.js → MSlider-BXsBv0ox.js} +208 -182
  5. package/dist/MSlider-BXsBv0ox.js.map +1 -0
  6. package/dist/{MTextField-B4X92vH_.js → MTextField-rML9jnXs.js} +2 -2
  7. package/dist/{MTextField-B4X92vH_.js.map → MTextField-rML9jnXs.js.map} +1 -1
  8. package/dist/api.json +1 -1
  9. package/dist/code-editor.js +1 -1
  10. package/dist/components/MBottomSheet.vue.d.ts +20 -1
  11. package/dist/components/MDialog.vue.d.ts +17 -8
  12. package/dist/components/MDragDropList.vue.d.ts +1 -1
  13. package/dist/components/MMap.vue.d.ts +4 -4
  14. package/dist/components/MMapControl.vue.d.ts +20 -3
  15. package/dist/components/MMapMarker.vue.d.ts +5 -2
  16. package/dist/components/MMapPopup.vue.d.ts +5 -3
  17. package/dist/components/MMenuItem.vue.d.ts +2 -2
  18. package/dist/components/MSeekBar.vue.d.ts +122 -0
  19. package/dist/components/MSplitButton.vue.d.ts +2 -2
  20. package/dist/components/_MMapAttribution.vue.d.ts +7 -0
  21. package/dist/composables/_mapPin.d.ts +5 -0
  22. package/dist/composables/_popupPosition.d.ts +12 -0
  23. package/dist/composables/_useCalendarMonth.d.ts +24 -0
  24. package/dist/composables/_useEscapeStack.d.ts +3 -0
  25. package/dist/composables/_useScrollLock.d.ts +8 -0
  26. package/dist/composables/_useSheetMotion.d.ts +16 -1
  27. package/dist/composables/useLocale.d.ts +8 -0
  28. package/dist/index.d.ts +2 -0
  29. package/dist/locales/index.js +56 -0
  30. package/dist/locales/index.js.map +1 -1
  31. package/dist/m3ui-vue.css +1 -1
  32. package/dist/m3ui.js +5269 -4714
  33. package/dist/m3ui.js.map +1 -1
  34. package/dist/map.js +777 -641
  35. package/dist/map.js.map +1 -1
  36. package/dist/rich-text-editor.js +8 -8
  37. package/dist/styles.css +1 -1
  38. package/dist/{useLocale-b5-hzsL3.js → useLocale-CqQrhWFl.js} +9 -1
  39. package/dist/useLocale-CqQrhWFl.js.map +1 -0
  40. package/package.json +1 -1
  41. package/src/__tests__/MDialog.test.ts +30 -0
  42. package/src/__tests__/MMap.test.ts +132 -2
  43. package/src/__tests__/MSeekBar.test.ts +170 -0
  44. package/src/__tests__/MTooltip.test.ts +110 -0
  45. package/src/__tests__/_fakeMaplibre.ts +3 -0
  46. package/src/__tests__/calendarMonth.test.ts +59 -0
  47. package/src/__tests__/pickerModals.test.ts +128 -0
  48. package/src/__tests__/popupPosition.test.ts +224 -0
  49. package/src/__tests__/scrollLock.test.ts +110 -0
  50. package/src/__tests__/sheets.test.ts +158 -0
  51. package/src/components/MAutocomplete.vue +2 -4
  52. package/src/components/MBottomSheet.vue +211 -13
  53. package/src/components/MCommandPalette.vue +4 -6
  54. package/src/components/MContextMenu.vue +19 -5
  55. package/src/components/MDatePicker.vue +29 -70
  56. package/src/components/MDatePickerModal.vue +116 -172
  57. package/src/components/MDateRangePicker.vue +29 -51
  58. package/src/components/MDateRangePickerModal.vue +84 -162
  59. package/src/components/MDialog.vue +87 -63
  60. package/src/components/MLightbox.vue +6 -3
  61. package/src/components/MMap.vue +74 -28
  62. package/src/components/MMapControl.vue +121 -7
  63. package/src/components/MMapIdentify.vue +2 -1
  64. package/src/components/MMapMarker.vue +14 -7
  65. package/src/components/MMapPopup.vue +8 -4
  66. package/src/components/MMenu.vue +31 -12
  67. package/src/components/MMenuItem.vue +21 -4
  68. package/src/components/MMultiAutocomplete.vue +2 -4
  69. package/src/components/MMultiSelect.vue +2 -4
  70. package/src/components/MNavigationDrawer.vue +2 -8
  71. package/src/components/MNavigationRail.vue +2 -3
  72. package/src/components/MSearchBar.vue +2 -5
  73. package/src/components/MSeekBar.vue +593 -0
  74. package/src/components/MSelect.vue +2 -8
  75. package/src/components/MSplitButton.vue +23 -7
  76. package/src/components/MSpotlightSearch.vue +4 -6
  77. package/src/components/MTimePicker.vue +23 -6
  78. package/src/components/MTimePickerModal.vue +213 -226
  79. package/src/components/MTooltip.vue +99 -3
  80. package/src/components/_MMapAttribution.vue +81 -0
  81. package/src/composables/_mapPin.ts +25 -0
  82. package/src/composables/_popupPosition.ts +39 -0
  83. package/src/composables/_useCalendarMonth.ts +74 -0
  84. package/src/composables/_useEscapeStack.ts +33 -0
  85. package/src/composables/_useScrollLock.ts +41 -0
  86. package/src/composables/_useSheetMotion.ts +82 -20
  87. package/src/composables/useLocale.ts +16 -0
  88. package/src/index.ts +2 -0
  89. package/src/locales/de.ts +8 -0
  90. package/src/locales/es.ts +8 -0
  91. package/src/locales/fr.ts +8 -0
  92. package/src/locales/ja.ts +8 -0
  93. package/src/locales/ko.ts +8 -0
  94. package/src/locales/pt.ts +8 -0
  95. package/src/locales/zh.ts +8 -0
  96. package/dist/MMenuItem-DFyXflk9.js +0 -707
  97. package/dist/MMenuItem-DFyXflk9.js.map +0 -1
  98. package/dist/MSlider-CdNIH7jc.js.map +0 -1
  99. package/dist/useLocale-b5-hzsL3.js.map +0 -1
@@ -19,6 +19,7 @@ import MIcon from "./MIcon.vue";
19
19
  import MDrawerItemList from "./_MDrawerItemList.vue";
20
20
  import { motionSprings, type SpringSpec } from "../composables/motion";
21
21
  import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from "../composables/_springAnimation";
22
+ import { useScrollLock } from '../composables/_useScrollLock'
22
23
 
23
24
  export interface DrawerItem {
24
25
  value: string | number;
@@ -153,14 +154,7 @@ function itemTag(item: DrawerItem) {
153
154
  return item.to && !item.disabled ? "RouterLink" : "button";
154
155
  }
155
156
 
156
- watch(
157
- () => props.modelValue,
158
- (open) => {
159
- if (props.modal) {
160
- document.body.style.overflow = open ? "hidden" : "";
161
- }
162
- },
163
- );
157
+ useScrollLock(() => props.modal && props.modelValue);
164
158
 
165
159
  // ── Modal: slide in/out + swipe to close ────────────────────────────────────────────────────
166
160
  // After ModalNavigationDrawer (NavigationDrawer.kt), which is left/right only; top/bottom add a bottom sheet's
@@ -13,6 +13,7 @@ import MIcon from './MIcon.vue'
13
13
  import MBadge from './MBadge.vue'
14
14
  import { useItemLink, type ItemLink } from '../composables/_useItemLink'
15
15
  import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from '../composables/_springAnimation'
16
+ import { useScrollLock } from '../composables/_useScrollLock'
16
17
 
17
18
  export interface NavRailItem {
18
19
  value: string | number
@@ -338,10 +339,8 @@ watch(
338
339
  (open) => {
339
340
  if (open) {
340
341
  document.addEventListener('keydown', onKeydown)
341
- document.body.style.overflow = 'hidden'
342
342
  } else {
343
343
  document.removeEventListener('keydown', onKeydown)
344
- document.body.style.overflow = ''
345
344
  }
346
345
  },
347
346
  )
@@ -350,8 +349,8 @@ onUnmounted(() => {
350
349
  slideAnim?.cancel()
351
350
  slideScrimAnim?.cancel()
352
351
  document.removeEventListener('keydown', onKeydown)
353
- if (props.modal && props.expanded) document.body.style.overflow = ''
354
352
  })
353
+ useScrollLock(() => props.modal && props.expanded)
355
354
  </script>
356
355
 
357
356
  <template>
@@ -19,6 +19,7 @@ import MIconButton from './MIconButton.vue'
19
19
  import MSpinner from './MSpinner.vue'
20
20
  import { useLocale } from '../composables/useLocale'
21
21
  import { searchAppBarKey } from '../composables/_searchAppBar'
22
+ import { useScrollLock } from '../composables/_useScrollLock'
22
23
 
23
24
  // Root is a fragment (bar div + a sibling Teleport for the expanded overlay panel), so Vue
24
25
  // can't auto-apply a consumer's class/attrs to either one — forward them onto the bar div below.
@@ -149,11 +150,7 @@ onUnmounted(() => {
149
150
  window.removeEventListener('scroll', onScroll, true)
150
151
  })
151
152
 
152
- watch(expanded, (v) => {
153
- if (v && fullscreen.value) document.body.style.overflow = 'hidden'
154
- else document.body.style.overflow = ''
155
- })
156
- onUnmounted(() => { document.body.style.overflow = '' })
153
+ useScrollLock(() => expanded.value && fullscreen.value)
157
154
  </script>
158
155
 
159
156
  <template>
@@ -0,0 +1,593 @@
1
+ <script lang="ts">
2
+ export interface SeekBarChapter {
3
+ /** Where the chapter starts, in seconds */
4
+ start: number
5
+ /** Shown in the preview while the pointer is over it */
6
+ title?: string
7
+ }
8
+ </script>
9
+
10
+ <script setup lang="ts">
11
+ /**
12
+ * A media seek bar (not an M3 spec component; Media3's ProgressSlider is a plain M3 Slider, which this follows in
13
+ * its default look): the playback position on a track with the buffered part, optional chapters, elapsed and
14
+ * total (or remaining) time, and a preview of the time under the pointer. Dragging scrubs locally and seeks once
15
+ * on release (or live, with `live`); a tap or click seeks to that point. While `playing`, it advances on its own
16
+ * between `modelValue` updates, so a 4Hz `timeupdate` still moves it smoothly.
17
+ *
18
+ * @slot preview Replaces the bubble shown above the pointer while hovering or dragging (a frame preview, say).
19
+ * Props: `{ time, formatted, chapter }`
20
+ * @event update:modelValue Emitted on a seek (and continuously while dragging with `live`) — powers v-model
21
+ * @event seek Emitted once per seek with its time: on release of a drag, a tap or click, or a key press
22
+ * @event scrub-start Emitted when a drag starts, with its time
23
+ * @event scrub Emitted while dragging, with the time under the pointer
24
+ * @event update:remaining Emitted when the end label is clicked to switch between total and remaining time
25
+ */
26
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
27
+ import { useLocale } from '../composables/useLocale'
28
+
29
+ const props = withDefaults(defineProps<{
30
+ /** Playback position in seconds (v-model) */
31
+ modelValue: number
32
+ /** Media length in seconds; 0, NaN or Infinity (not loaded yet, a live stream) disables seeking */
33
+ duration: number
34
+ /** How much is loaded: up to a time, or time ranges (`[[start, end], …]`, like a media element's `buffered`) */
35
+ buffered?: number | [number, number][]
36
+ /** Chapter starts, drawn as gaps in the track; their titles show in the preview */
37
+ chapters?: SeekBarChapter[]
38
+ /** The media is playing: the bar moves on its own between updates, and the wavy line waves */
39
+ playing?: boolean
40
+ /** Playback speed, for that motion between updates */
41
+ playbackRate?: number
42
+ /** Updates the v-model continuously while dragging (the media follows the thumb), not only on release */
43
+ live?: boolean
44
+ /** `slider`: the M3 slider (bar thumb with a gap around it). `line`: a plain line with a round thumb. `wavy`: the played part waves while playing */
45
+ variant?: 'slider' | 'line' | 'wavy'
46
+ /** Track thickness: `md` (16px, the smallest M3 slider), `sm` (8px) or `xs` (4px, over a video) */
47
+ size?: 'xs' | 'sm' | 'md'
48
+ /** Where the elapsed and total time go */
49
+ time?: 'below' | 'sides' | 'none'
50
+ /** The end label shows the remaining time (`-1:23`) instead of the total; clicking it switches (v-model:remaining) */
51
+ remaining?: boolean
52
+ /** Formats a time in seconds. Default `m:ss`, or `h:mm:ss` for media an hour or longer */
53
+ format?: (seconds: number) => string
54
+ /** Seconds the arrow keys move; Page Up/Down move 10% */
55
+ keyStep?: number
56
+ /** Shows the time (and chapter) above the pointer while hovering or dragging */
57
+ preview?: boolean
58
+ /** Played part and thumb: an M3 role (`primary`, `secondary`, `tertiary`, `error`) or any CSS color */
59
+ color?: string
60
+ /** Unplayed track: an M3 role or any CSS color. Default secondary-container */
61
+ trackColor?: string
62
+ /** The time labels' color, as a CSS color (white over a video, say). Default on-surface-variant */
63
+ timeColor?: string
64
+ /** Disables seeking */
65
+ disabled?: boolean
66
+ /** Accessible name. Default the locale's `seekBar` */
67
+ label?: string
68
+ }>(), {
69
+ playing: false,
70
+ playbackRate: 1,
71
+ live: false,
72
+ variant: 'slider',
73
+ size: 'md',
74
+ time: 'below',
75
+ remaining: false,
76
+ keyStep: 5,
77
+ preview: true,
78
+ color: 'primary',
79
+ disabled: false,
80
+ })
81
+
82
+ const emit = defineEmits<{
83
+ 'update:modelValue': [number]
84
+ seek: [number]
85
+ 'scrub-start': [number]
86
+ scrub: [number]
87
+ 'update:remaining': [boolean]
88
+ }>()
89
+
90
+ const locale = useLocale()
91
+
92
+ // track: thickness; thumbH: the bar thumb's height (md = MSlider xs, SliderTokens.HandleHeight); dot: the line
93
+ // variant's round thumb under the pointer, dotIdle at rest (the thinner the line, the more it grows, as a
94
+ // video player's does); gap: around the bar thumb; hit: the pointer target's height; wave/amp/length: the wavy line
95
+ const SIZES = {
96
+ xs: { track: 4, thumbH: 16, dot: 21, dotIdle: 10, gap: 4, hit: 32, wave: 4, amp: 2.5, length: 18 },
97
+ sm: { track: 8, thumbH: 28, dot: 20, dotIdle: 14, gap: 5, hit: 40, wave: 6, amp: 3, length: 22 },
98
+ md: { track: 16, thumbH: 44, dot: 28, dotIdle: 21, gap: 6, hit: 46, wave: 8, amp: 4, length: 26 },
99
+ }
100
+ const s = computed(() => SIZES[props.size] ?? SIZES.md)
101
+
102
+ const dur = computed(() => (Number.isFinite(props.duration) && props.duration > 0 ? props.duration : 0))
103
+ const inactive = computed(() => props.disabled || !dur.value)
104
+
105
+ // ── Colors (as in MSlider: a role name or any CSS color; disabled per SliderTokens) ─────────────────────
106
+ const ROLES: Record<string, string> = {
107
+ primary: 'var(--color-primary)',
108
+ secondary: 'var(--color-secondary)',
109
+ tertiary: 'var(--color-tertiary)',
110
+ error: 'var(--color-error)',
111
+ 'secondary-container': 'var(--color-secondary-container)',
112
+ }
113
+ const colors = computed(() => {
114
+ if (props.disabled) {
115
+ return {
116
+ active: 'color-mix(in srgb, var(--color-on-surface) 38%, transparent)',
117
+ inactive: 'color-mix(in srgb, var(--color-on-surface) 12%, transparent)',
118
+ buffered: 'color-mix(in srgb, var(--color-on-surface) 20%, transparent)',
119
+ }
120
+ }
121
+ const active = ROLES[props.color] ?? props.color
122
+ const inactive = props.trackColor ? ROLES[props.trackColor] ?? props.trackColor : 'var(--color-secondary-container)'
123
+ return { active, inactive, buffered: `color-mix(in srgb, ${active} 35%, ${inactive})` }
124
+ })
125
+
126
+ // ── Time ────────────────────────────────────────────────────────────────────────────────────────────────
127
+ const pad = (n: number) => String(n).padStart(2, '0')
128
+ function fmt(t: number) {
129
+ if (props.format) return props.format(t)
130
+ const sec = Math.max(0, Math.floor(t))
131
+ const h = Math.floor(sec / 3600)
132
+ const m = Math.floor((sec % 3600) / 60)
133
+ return (dur.value >= 3600 ? `${h}:${pad(m)}` : String(h * 60 + m)) + ':' + pad(sec % 60)
134
+ }
135
+
136
+ // The shown position: the drag's while scrubbing, else `pos`, which follows modelValue and, while playing,
137
+ // advances on its own (only written when it moves half a pixel or crosses a second, so it re-renders rarely)
138
+ const scrubTime = ref<number | null>(null)
139
+ const pos = ref(props.modelValue)
140
+ const current = computed(() => Math.min(dur.value || Infinity, Math.max(0, scrubTime.value ?? pos.value)))
141
+
142
+ const showRemaining = ref(props.remaining)
143
+ watch(() => props.remaining, (v) => { showRemaining.value = v })
144
+ function toggleRemaining() {
145
+ showRemaining.value = !showRemaining.value
146
+ emit('update:remaining', showRemaining.value)
147
+ }
148
+ const timeStyle = computed(() => (props.timeColor ? { color: props.timeColor } : undefined))
149
+ const startText = computed(() => fmt(current.value))
150
+ const endText = computed(() => {
151
+ if (!dur.value) return '--:--'
152
+ return showRemaining.value ? `-${fmt(dur.value - current.value)}` : fmt(dur.value)
153
+ })
154
+ const valueText = computed(() =>
155
+ locale.seekBarValue.replace('{position}', fmt(current.value)).replace('{duration}', dur.value ? fmt(dur.value) : '--:--'),
156
+ )
157
+
158
+ const chapterAt = (t: number) => {
159
+ let found: SeekBarChapter | null = null
160
+ for (const c of props.chapters ?? []) if (c.start <= t && (!found || c.start >= found.start)) found = c
161
+ return found
162
+ }
163
+
164
+ // ── Geometry (px, from the measured track width) ────────────────────────────────────────────────────────
165
+ const trackEl = ref<HTMLElement>()
166
+ const width = ref(0)
167
+ const toPx = (t: number) => (dur.value ? (width.value * Math.min(1, Math.max(0, t / dur.value))) : 0)
168
+
169
+ // The bar thumb keeps clear of the track's ends, as MSlider's thumbInset; the round one may overhang them
170
+ const thumbX = computed(() => {
171
+ const x = toPx(current.value)
172
+ if (props.variant === 'line') return x
173
+ const inset = Math.max(2, s.value.track * 0.3)
174
+ return Math.min(width.value - inset, Math.max(inset, x))
175
+ })
176
+
177
+ const bufferedRanges = computed<[number, number][]>(() => {
178
+ const b = props.buffered
179
+ if (b === undefined) return []
180
+ return typeof b === 'number' ? [[0, b]] : b
181
+ })
182
+
183
+ interface Piece { a: number; b: number; rl: number; rr: number; fills: { left: number; width: number; color: string }[] }
184
+
185
+ // The track is drawn as pieces between cuts: chapter boundaries, the gap around the bar thumb and, for the wavy
186
+ // line, everything before the thumb (drawn by the SVG). Outer ends get a full radius, cut ends a small one, as
187
+ // the M3 slider's segments.
188
+ const pieces = computed<Piece[]>(() => {
189
+ const W = width.value
190
+ if (!W) return []
191
+ const { gap } = s.value
192
+ const track = trackH.value
193
+ const x = thumbX.value
194
+ const chapterGap = props.size === 'md' ? 4 : 2
195
+ const cuts: [number, number][] = []
196
+ for (const c of props.chapters ?? []) {
197
+ if (c.start <= 0 || c.start >= dur.value) continue
198
+ const cx = toPx(c.start)
199
+ cuts.push([cx - chapterGap / 2, cx + chapterGap / 2])
200
+ }
201
+ if (props.variant === 'slider') cuts.push([x - gap, x + gap])
202
+ if (props.variant === 'wavy') cuts.push([-Infinity, x + gap])
203
+ cuts.sort((p, q) => p[0] - q[0])
204
+ const spans: [number, number][] = []
205
+ let at = 0
206
+ for (const [c0, c1] of cuts) {
207
+ if (c0 > at) spans.push([at, Math.min(c0, W)])
208
+ at = Math.max(at, c1)
209
+ }
210
+ if (at < W) spans.push([at, W])
211
+
212
+ const full = track / 2
213
+ const small = Math.min(full, Math.max(1, track / 8))
214
+ const playedTo = props.variant === 'line' ? toPx(current.value) : x
215
+ return spans.filter(([a, b]) => b - a >= 0.5).map(([a, b]) => {
216
+ const fills: Piece['fills'] = []
217
+ for (const [t0, t1] of bufferedRanges.value) {
218
+ const l = Math.max(a, toPx(t0))
219
+ const r = Math.min(b, toPx(t1))
220
+ if (r > l) fills.push({ left: l - a, width: r - l, color: colors.value.buffered })
221
+ }
222
+ if (playedTo > a) fills.push({ left: 0, width: Math.min(b, playedTo) - a, color: colors.value.active })
223
+ return { a, b, rl: a <= 0.01 ? full : small, rr: b >= W - 0.01 ? full : small, fills }
224
+ })
225
+ })
226
+
227
+ // M3's stop indicator at the far end of a (md) slider track, while the thumb hasn't reached it
228
+ const stopDot = computed(() => props.variant === 'slider' && props.size === 'md' && thumbX.value < width.value - 12 - s.value.gap)
229
+
230
+ // ── Thumb ───────────────────────────────────────────────────────────────────────────────────────────────
231
+ const pressed = ref(false)
232
+ const focused = ref(false)
233
+ const hovered = ref(false)
234
+
235
+ // The line thickens under the pointer, as a video player's does; the wavy line's unplayed part is as thick as the
236
+ // wave's stroke (WavyProgressIndicatorDefaults: the same stroke for indicator and track)
237
+ const trackH = computed(() => {
238
+ if (props.variant === 'wavy') return s.value.wave
239
+ const t = s.value.track
240
+ return props.variant === 'line' && !inactive.value && (hovered.value || pressed.value) ? t + Math.max(2, t / 4) : t
241
+ })
242
+ const thumbStyle = computed(() => {
243
+ const base = { left: `${thumbX.value}px`, backgroundColor: colors.value.active }
244
+ if (props.variant === 'line') {
245
+ const { dot: d, dotIdle } = s.value
246
+ const scale = pressed.value || hovered.value || focused.value ? 1 : dotIdle / d
247
+ return { ...base, width: `${d}px`, height: `${d}px`, borderRadius: '50%', transform: `translate(-50%, -50%) scale(${scale})` }
248
+ }
249
+ // Pinches to half its width while pressed or focused, as MSlider (SliderTokens.PressedHandleWidth)
250
+ const narrow = pressed.value || focused.value ? 0.5 : 1
251
+ return { ...base, width: '4px', height: `${s.value.thumbH}px`, borderRadius: '2px', transform: `translate(-50%, -50%) scaleX(${narrow})` }
252
+ })
253
+
254
+ // ── Preview bubble ──────────────────────────────────────────────────────────────────────────────────────
255
+ const hoverTime = ref<number | null>(null)
256
+ const previewTime = computed(() => (props.preview && !inactive.value ? scrubTime.value ?? hoverTime.value : null))
257
+ const previewChapter = computed(() => (previewTime.value === null ? null : chapterAt(previewTime.value)))
258
+ const bubbleEl = ref<HTMLElement>()
259
+ const bubbleW = ref(0)
260
+ let bubbleObserver: ResizeObserver | null = null
261
+ watch(bubbleEl, (el) => {
262
+ bubbleObserver?.disconnect()
263
+ if (!el || typeof ResizeObserver === 'undefined') return
264
+ bubbleW.value = el.offsetWidth
265
+ bubbleObserver = new ResizeObserver(() => { bubbleW.value = el.offsetWidth })
266
+ bubbleObserver.observe(el)
267
+ })
268
+ // Centered over the time, but kept within the track
269
+ const previewX = computed(() => {
270
+ const x = previewTime.value === null ? 0 : toPx(previewTime.value)
271
+ const half = bubbleW.value / 2
272
+ return half * 2 >= width.value ? width.value / 2 : Math.min(width.value - half, Math.max(half, x))
273
+ })
274
+
275
+ // ── Pointer: mouse/pen scrub from the press; a touch waits to see a horizontal move (touch-action pan-y leaves a
276
+ // vertical one to the page), and a touch that doesn't move is a tap that seeks there ────────────────────────
277
+ let press: { id: number; x: number; y: number; touch: boolean; started: boolean } | null = null
278
+
279
+ function timeAt(clientX: number) {
280
+ const el = trackEl.value
281
+ if (!el || !dur.value) return 0
282
+ const r = el.getBoundingClientRect()
283
+ return (Math.min(1, Math.max(0, (clientX - r.left) / (r.width || 1)))) * dur.value
284
+ }
285
+
286
+ function scrubTo(t: number) {
287
+ scrubTime.value = t
288
+ emit('scrub', t)
289
+ if (props.live) emit('update:modelValue', t)
290
+ }
291
+
292
+ function begin(e: PointerEvent) {
293
+ if (!press) return
294
+ press.started = true
295
+ pressed.value = true
296
+ const t = timeAt(e.clientX)
297
+ emit('scrub-start', t)
298
+ scrubTo(t)
299
+ }
300
+
301
+ function commit(t: number) {
302
+ emit('update:modelValue', t)
303
+ emit('seek', t)
304
+ // Show it at once; a later modelValue resyncs
305
+ pos.value = t
306
+ base = t
307
+ baseAt = now()
308
+ }
309
+
310
+ function onPointerDown(e: PointerEvent) {
311
+ if (inactive.value || e.button !== 0) return
312
+ press = { id: e.pointerId, x: e.clientX, y: e.clientY, touch: e.pointerType === 'touch', started: false }
313
+ window.addEventListener('pointermove', onPointerMove)
314
+ window.addEventListener('pointerup', onPointerUp)
315
+ window.addEventListener('pointercancel', onPointerCancel)
316
+ if (!press.touch) {
317
+ e.preventDefault()
318
+ trackEl.value?.focus({ preventScroll: true })
319
+ begin(e)
320
+ }
321
+ }
322
+
323
+ function onPointerMove(e: PointerEvent) {
324
+ if (!press || e.pointerId !== press.id) return
325
+ if (!press.started) {
326
+ const dx = Math.abs(e.clientX - press.x)
327
+ const dy = Math.abs(e.clientY - press.y)
328
+ if (dy > 8 && dy > dx) return end()
329
+ if (dx < 4) return
330
+ begin(e)
331
+ }
332
+ scrubTo(timeAt(e.clientX))
333
+ }
334
+
335
+ function onPointerUp(e: PointerEvent) {
336
+ if (!press || e.pointerId !== press.id) return
337
+ const t = press.started ? scrubTime.value ?? timeAt(e.clientX) : timeAt(e.clientX)
338
+ end()
339
+ commit(t)
340
+ }
341
+
342
+ // The browser took the touch (a scroll): a drag in progress still lands where it was
343
+ function onPointerCancel(e: PointerEvent) {
344
+ if (!press || e.pointerId !== press.id) return
345
+ const t = press.started ? scrubTime.value : null
346
+ end()
347
+ if (t !== null) commit(t)
348
+ }
349
+
350
+ function end() {
351
+ press = null
352
+ pressed.value = false
353
+ scrubTime.value = null
354
+ window.removeEventListener('pointermove', onPointerMove)
355
+ window.removeEventListener('pointerup', onPointerUp)
356
+ window.removeEventListener('pointercancel', onPointerCancel)
357
+ }
358
+
359
+ function onHover(e: PointerEvent) {
360
+ if (e.pointerType === 'touch') return
361
+ hovered.value = true
362
+ hoverTime.value = timeAt(e.clientX)
363
+ }
364
+ function onLeave() {
365
+ hovered.value = false
366
+ hoverTime.value = null
367
+ }
368
+
369
+ function onKeyDown(e: KeyboardEvent) {
370
+ if (inactive.value) return
371
+ const d = dur.value
372
+ const steps: Record<string, number> = {
373
+ ArrowRight: props.keyStep, ArrowUp: props.keyStep, ArrowLeft: -props.keyStep, ArrowDown: -props.keyStep,
374
+ PageUp: d / 10, PageDown: -d / 10,
375
+ }
376
+ let t: number | null = null
377
+ if (e.key in steps) t = current.value + steps[e.key]!
378
+ else if (e.key === 'Home') t = 0
379
+ else if (e.key === 'End') t = d
380
+ if (t === null) return
381
+ e.preventDefault()
382
+ commit(Math.min(d, Math.max(0, t)))
383
+ }
384
+
385
+ // ── Motion between updates, and the wave (a rAF loop, only while either needs frames) ───────────────────
386
+ const now = () => (typeof performance !== 'undefined' ? performance.now() : 0)
387
+ let base = props.modelValue
388
+ let baseAt = now()
389
+ let raf = 0
390
+ let last = 0
391
+ let amp = 0
392
+ let phase = 0
393
+ const wavePathEl = ref<SVGPathElement>()
394
+ const svgH = computed(() => s.value.wave + 2 * s.value.amp + 2)
395
+ const reducedMotion = typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
396
+
397
+ const ampTarget = () => (props.variant === 'wavy' && props.playing && scrubTime.value === null ? s.value.amp : 0)
398
+ amp = ampTarget()
399
+
400
+ function advancing() {
401
+ return props.playing && scrubTime.value === null && !!dur.value
402
+ }
403
+
404
+ function syncPos(t: number) {
405
+ const W = width.value
406
+ const d = dur.value
407
+ if (Math.floor(t) !== Math.floor(pos.value) || (d && (Math.abs(t - pos.value) * W) / d >= 0.5)) pos.value = t
408
+ }
409
+
410
+ function drawWave() {
411
+ const path = wavePathEl.value
412
+ if (!path) return
413
+ const { wave, length, gap } = s.value
414
+ const half = wave / 2
415
+ const end = thumbX.value - gap - half
416
+ if (end <= half) return path.setAttribute('d', '')
417
+ const mid = svgH.value / 2
418
+ const k = (2 * Math.PI) / length
419
+ let d = ''
420
+ for (let x = half; ; x += 2) {
421
+ const px = Math.min(x, end)
422
+ d += `${d ? 'L' : 'M'}${px.toFixed(1)},${(mid + amp * Math.sin(k * px + phase)).toFixed(2)}`
423
+ if (px >= end) break
424
+ }
425
+ path.setAttribute('d', d)
426
+ }
427
+
428
+ function frame(t: number) {
429
+ const dt = last ? Math.min(0.1, (t - last) / 1000) : 0
430
+ last = t
431
+ if (advancing()) syncPos(Math.min(dur.value, base + ((t - baseAt) / 1000) * props.playbackRate))
432
+ if (props.variant === 'wavy') {
433
+ amp += (ampTarget() - amp) * (1 - Math.exp(-dt / 0.12))
434
+ if (Math.abs(ampTarget() - amp) < 0.01) amp = ampTarget()
435
+ // The wave travels with the playback, one length every 1.25s
436
+ if (props.playing && !reducedMotion) phase -= (dt * 2 * Math.PI) / 1.25
437
+ drawWave()
438
+ }
439
+ if (advancing() || (props.variant === 'wavy' && (amp !== ampTarget() || (props.playing && !reducedMotion)))) {
440
+ raf = requestAnimationFrame(frame)
441
+ } else {
442
+ raf = 0
443
+ last = 0
444
+ }
445
+ }
446
+
447
+ function kick() {
448
+ if (raf || typeof requestAnimationFrame === 'undefined') return
449
+ raf = requestAnimationFrame(frame)
450
+ }
451
+
452
+ watch(() => props.modelValue, (v) => {
453
+ base = v
454
+ baseAt = now()
455
+ pos.value = v
456
+ kick()
457
+ })
458
+ watch(() => props.playing, () => {
459
+ base = pos.value
460
+ baseAt = now()
461
+ kick()
462
+ })
463
+ watch(scrubTime, (v) => {
464
+ if (v === null) {
465
+ base = pos.value
466
+ baseAt = now()
467
+ }
468
+ kick()
469
+ })
470
+ // Paused, the (flat) wave still follows the thumb
471
+ watch([thumbX, () => props.variant, () => props.size], () => {
472
+ if (props.variant === 'wavy') drawWave()
473
+ }, { flush: 'post' })
474
+
475
+ let resizeObserver: ResizeObserver | null = null
476
+ onMounted(() => {
477
+ const el = trackEl.value!
478
+ width.value = el.clientWidth
479
+ if (typeof ResizeObserver !== 'undefined') {
480
+ resizeObserver = new ResizeObserver(() => { width.value = el.clientWidth })
481
+ resizeObserver.observe(el)
482
+ }
483
+ drawWave()
484
+ kick()
485
+ })
486
+ onBeforeUnmount(() => {
487
+ cancelAnimationFrame(raf)
488
+ resizeObserver?.disconnect()
489
+ bubbleObserver?.disconnect()
490
+ end()
491
+ })
492
+ </script>
493
+
494
+ <template>
495
+ <div class="flex min-w-0 select-none" :class="time === 'sides' ? 'items-center gap-3' : 'flex-col'">
496
+ <span v-if="time === 'sides'" class="min-w-[4ch] shrink-0 text-right text-label-medium tabular-nums" :class="!timeColor && 'text-on-surface-variant'" :style="timeStyle">{{ startText }}</span>
497
+
498
+ <div
499
+ ref="trackEl"
500
+ role="slider"
501
+ :tabindex="inactive ? -1 : 0"
502
+ :aria-label="label ?? locale.seekBar"
503
+ aria-valuemin="0"
504
+ :aria-valuemax="Math.round(dur)"
505
+ :aria-valuenow="Math.round(current)"
506
+ :aria-valuetext="valueText"
507
+ :aria-disabled="inactive || undefined"
508
+ class="group relative min-w-0 shrink-0 touch-pan-y outline-none"
509
+ :class="[inactive ? 'cursor-default' : 'cursor-pointer', time === 'sides' && 'flex-1']"
510
+ :style="{ height: `${s.hit}px` }"
511
+ @pointerdown="onPointerDown"
512
+ @pointermove="onHover"
513
+ @pointerleave="onLeave"
514
+ @keydown="onKeyDown"
515
+ @focus="focused = true"
516
+ @blur="focused = false"
517
+ >
518
+ <!-- A grace zone past the hit area (mostly above, where the preview opens), so the pointer doesn't have to
519
+ land exactly on a thin line: its events reach the track like the track's own -->
520
+ <div v-if="!inactive" class="absolute inset-x-0 -top-3 -bottom-1" />
521
+ <div
522
+ v-for="(p, i) in pieces"
523
+ :key="i"
524
+ class="absolute top-1/2 -translate-y-1/2 overflow-hidden transition-[height] duration-100"
525
+ :style="{
526
+ left: `${p.a}px`, width: `${p.b - p.a}px`, height: `${trackH}px`,
527
+ borderRadius: `${p.rl}px ${p.rr}px ${p.rr}px ${p.rl}px`, backgroundColor: colors.inactive,
528
+ }"
529
+ >
530
+ <div
531
+ v-for="(f, j) in p.fills"
532
+ :key="j"
533
+ class="absolute inset-y-0"
534
+ :style="{ left: `${f.left}px`, width: `${f.width}px`, backgroundColor: f.color }"
535
+ />
536
+ </div>
537
+
538
+ <svg
539
+ v-if="variant === 'wavy'"
540
+ class="pointer-events-none absolute top-1/2 left-0 -translate-y-1/2 overflow-visible"
541
+ :width="width"
542
+ :height="svgH"
543
+ aria-hidden="true"
544
+ >
545
+ <path ref="wavePathEl" fill="none" :stroke="colors.active" :stroke-width="s.wave" stroke-linecap="round" />
546
+ </svg>
547
+
548
+ <div
549
+ v-if="stopDot"
550
+ class="pointer-events-none absolute top-1/2 right-3 size-1 -translate-y-1/2 rounded-full"
551
+ :style="{ backgroundColor: colors.active }"
552
+ />
553
+
554
+ <div
555
+ class="pointer-events-none absolute top-1/2 transition-transform duration-75 group-focus-visible:ring-2 group-focus-visible:ring-primary group-focus-visible:ring-offset-2 group-focus-visible:ring-offset-surface"
556
+ :style="thumbStyle"
557
+ />
558
+
559
+ <div
560
+ v-if="previewTime !== null"
561
+ ref="bubbleEl"
562
+ class="pointer-events-none absolute bottom-full z-10 mb-1 -translate-x-1/2"
563
+ :style="{ left: `${previewX}px` }"
564
+ >
565
+ <slot name="preview" :time="previewTime" :formatted="fmt(previewTime)" :chapter="previewChapter">
566
+ <div class="flex h-7 max-w-60 items-center gap-1 rounded-full bg-inverse-surface px-2.5 text-label-medium whitespace-nowrap text-inverse-on-surface tabular-nums">
567
+ <span v-if="previewChapter?.title" class="truncate">{{ previewChapter.title }} ·</span>
568
+ <span>{{ fmt(previewTime) }}</span>
569
+ </div>
570
+ </slot>
571
+ </div>
572
+ </div>
573
+
574
+ <div v-if="time === 'below'" class="flex items-center justify-between text-label-medium tabular-nums" :class="!timeColor && 'text-on-surface-variant'" :style="timeStyle">
575
+ <span>{{ startText }}</span>
576
+ <button
577
+ type="button"
578
+ class="cursor-pointer rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-secondary"
579
+ :aria-label="showRemaining ? locale.seekBarShowDuration : locale.seekBarShowRemaining"
580
+ @click="toggleRemaining"
581
+ >{{ endText }}</button>
582
+ </div>
583
+ <button
584
+ v-else-if="time === 'sides'"
585
+ type="button"
586
+ class="min-w-[4ch] shrink-0 cursor-pointer rounded-sm text-left text-label-medium tabular-nums outline-none focus-visible:ring-2 focus-visible:ring-secondary"
587
+ :class="!timeColor && 'text-on-surface-variant'"
588
+ :style="timeStyle"
589
+ :aria-label="showRemaining ? locale.seekBarShowDuration : locale.seekBarShowRemaining"
590
+ @click="toggleRemaining"
591
+ >{{ endText }}</button>
592
+ </div>
593
+ </template>