@m3ui-vue/m3ui-vue 0.9.1 → 0.9.3

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 (139) hide show
  1. package/README.md +43 -0
  2. package/dist/API.md +266 -60
  3. package/dist/{MIcon-CaEooCmZ.js → MIcon-DLOkQduI.js} +10 -3
  4. package/dist/MIcon-DLOkQduI.js.map +1 -0
  5. package/dist/{MTextField-pJU1vFaR.js → MTextField-DKtgPymd.js} +422 -206
  6. package/dist/MTextField-DKtgPymd.js.map +1 -0
  7. package/dist/THIRD_PARTY_LICENSES.md +547 -0
  8. package/dist/api.json +1 -1
  9. package/dist/code-editor.js +2 -2
  10. package/dist/components/MBox.vue.d.ts +3 -3
  11. package/dist/components/MButton.vue.d.ts +4 -2
  12. package/dist/components/MButtonGroup.vue.d.ts +2 -0
  13. package/dist/components/MColorPicker.vue.d.ts +13 -1
  14. package/dist/components/MColorPickerModal.vue.d.ts +14 -2
  15. package/dist/components/MDragHandle.vue.d.ts +12 -0
  16. package/dist/components/MEmojiSelector.vue.d.ts +1 -1
  17. package/dist/components/MFab.vue.d.ts +5 -3
  18. package/dist/components/MFabMenu.vue.d.ts +1 -1
  19. package/dist/components/MIcon.vue.d.ts +3 -0
  20. package/dist/components/MIconButton.vue.d.ts +15 -1
  21. package/dist/components/MListItem.vue.d.ts +5 -5
  22. package/dist/components/MMenuItem.vue.d.ts +17 -4
  23. package/dist/components/MOverlay.vue.d.ts +1 -1
  24. package/dist/components/MScrollField.vue.d.ts +40 -0
  25. package/dist/components/MScrollable.vue.d.ts +9 -0
  26. package/dist/components/MSplitButton.vue.d.ts +2 -0
  27. package/dist/components/MSplitter.vue.d.ts +2 -2
  28. package/dist/components/MSwipeToDismiss.vue.d.ts +49 -0
  29. package/dist/components/MTabs.vue.d.ts +6 -0
  30. package/dist/components/MTimePickerModal.vue.d.ts +3 -0
  31. package/dist/components/MTimeScroll.vue.d.ts +22 -0
  32. package/dist/components/MToggleButton.vue.d.ts +2 -0
  33. package/dist/components/MTransition.vue.d.ts +34 -0
  34. package/dist/components/MTree.vue.d.ts +2 -2
  35. package/dist/components/MWindow.vue.d.ts +1 -1
  36. package/dist/components/_MColorPanel.vue.d.ts +18 -0
  37. package/dist/components/_MDrawerItemList.vue.d.ts +3 -0
  38. package/dist/composables/_colorUtils.d.ts +34 -0
  39. package/dist/composables/_themeColor.d.ts +1 -0
  40. package/dist/composables/_useSpring.d.ts +17 -0
  41. package/dist/composables/dynamicColor.d.ts +18 -0
  42. package/dist/composables/useColorPalette.d.ts +27 -0
  43. package/dist/composables/useLocale.d.ts +16 -0
  44. package/dist/directives/vRipple.d.ts +16 -0
  45. package/dist/emoji-names-de-BphO5XpV.js.map +1 -1
  46. package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -1
  47. package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -1
  48. package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -1
  49. package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -1
  50. package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -1
  51. package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -1
  52. package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -1
  53. package/dist/index.d.ts +12 -1
  54. package/dist/locales/index.js +112 -0
  55. package/dist/locales/index.js.map +1 -1
  56. package/dist/m3ui-vue.css +1 -1
  57. package/dist/m3ui.js +7443 -6150
  58. package/dist/m3ui.js.map +1 -1
  59. package/dist/material-color-utilities-Cktu8DBU.js +3160 -0
  60. package/dist/material-color-utilities-Cktu8DBU.js.map +1 -0
  61. package/dist/plugin.d.ts +18 -0
  62. package/dist/rich-text-editor.js +22 -22
  63. package/dist/styles/palettes.css +546 -0
  64. package/dist/styles/theme.css +90 -0
  65. package/dist/styles.css +1 -1
  66. package/dist/terminal.js +1 -1
  67. package/dist/{useLocale-Drihe5G2.js → useLocale-Cn9MVIdw.js} +17 -1
  68. package/dist/useLocale-Cn9MVIdw.js.map +1 -0
  69. package/package.json +3 -2
  70. package/src/__tests__/MButtonGroup.test.ts +51 -0
  71. package/src/__tests__/MColorPicker.test.ts +141 -0
  72. package/src/__tests__/MDragHandle.test.ts +57 -0
  73. package/src/__tests__/MIconButton.test.ts +60 -0
  74. package/src/__tests__/MListItemShape.test.ts +76 -0
  75. package/src/__tests__/MMenuItem.test.ts +84 -0
  76. package/src/__tests__/MScrollField.test.ts +69 -0
  77. package/src/__tests__/MScrollable.test.ts +42 -0
  78. package/src/__tests__/MSwipeToDismiss.test.ts +104 -0
  79. package/src/__tests__/MTabs.test.ts +39 -0
  80. package/src/__tests__/MTimePickerModal.test.ts +52 -0
  81. package/src/__tests__/MTimeScroll.test.ts +44 -0
  82. package/src/__tests__/MTransition.test.ts +27 -0
  83. package/src/__tests__/dynamicColor.test.ts +191 -0
  84. package/src/__tests__/vRipple.test.ts +92 -0
  85. package/src/components/MBox.vue +4 -2
  86. package/src/components/MButton.vue +4 -13
  87. package/src/components/MButtonGroup.vue +118 -44
  88. package/src/components/MColorPicker.vue +114 -210
  89. package/src/components/MColorPickerModal.vue +50 -251
  90. package/src/components/MDragHandle.vue +54 -0
  91. package/src/components/MFab.vue +8 -15
  92. package/src/components/MIcon.vue +4 -2
  93. package/src/components/MIconButton.vue +96 -24
  94. package/src/components/MList.vue +33 -3
  95. package/src/components/MListItem.vue +75 -5
  96. package/src/components/MMenuItem.vue +115 -13
  97. package/src/components/MScrollField.vue +310 -0
  98. package/src/components/MScrollable.vue +160 -5
  99. package/src/components/MSplitButton.vue +5 -12
  100. package/src/components/MSplitter.vue +33 -10
  101. package/src/components/MSwipeToDismiss.vue +247 -0
  102. package/src/components/MTabs.vue +16 -5
  103. package/src/components/MTimePickerModal.vue +48 -8
  104. package/src/components/MTimeScroll.vue +161 -0
  105. package/src/components/MToggleButton.vue +5 -12
  106. package/src/components/MTransition.vue +149 -0
  107. package/src/components/_MColorPanel.vue +478 -0
  108. package/src/composables/_carouselStrategy.ts +3 -0
  109. package/src/composables/_colorUtils.ts +107 -0
  110. package/src/composables/_themeColor.ts +33 -0
  111. package/src/composables/_useSpring.ts +91 -0
  112. package/src/composables/dynamicColor.ts +85 -0
  113. package/src/composables/useColorPalette.ts +171 -25
  114. package/src/composables/useFieldBg.ts +6 -0
  115. package/src/composables/useLocale.ts +34 -0
  116. package/src/data/emoji-names/emoji-names-de.ts +1 -0
  117. package/src/data/emoji-names/emoji-names-en.ts +1 -0
  118. package/src/data/emoji-names/emoji-names-es.ts +1 -0
  119. package/src/data/emoji-names/emoji-names-fr.ts +1 -0
  120. package/src/data/emoji-names/emoji-names-ja.ts +1 -0
  121. package/src/data/emoji-names/emoji-names-ko.ts +1 -0
  122. package/src/data/emoji-names/emoji-names-pt.ts +1 -0
  123. package/src/data/emoji-names/emoji-names-zh.ts +1 -0
  124. package/src/data/emojis.ts +1 -0
  125. package/src/directives/vRipple.ts +73 -0
  126. package/src/index.ts +14 -1
  127. package/src/locales/de.ts +16 -0
  128. package/src/locales/es.ts +16 -0
  129. package/src/locales/fr.ts +16 -0
  130. package/src/locales/ja.ts +16 -0
  131. package/src/locales/ko.ts +16 -0
  132. package/src/locales/pt.ts +16 -0
  133. package/src/locales/zh.ts +16 -0
  134. package/src/plugin.ts +30 -7
  135. package/src/styles/palettes.css +546 -0
  136. package/src/styles/theme.css +90 -0
  137. package/dist/MIcon-CaEooCmZ.js.map +0 -1
  138. package/dist/MTextField-pJU1vFaR.js.map +0 -1
  139. package/dist/useLocale-Drihe5G2.js.map +0 -1
@@ -0,0 +1,310 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * An endless wheel to pick a number or option by scrolling (M3 Expressive's ScrollField, a port of Compose's
4
+ * `ScrollField`): the centered item is the selected one. Drag, flick, use the mouse wheel, the arrow keys, or
5
+ * click an item. Put two side by side for a time (hours and minutes).
6
+ *
7
+ * @slot item Custom content for each item, instead of the formatted number. Receives `{ index, selected }`.
8
+ * @event update:modelValue Emitted with the index of the centered item, as it changes while scrolling
9
+ */
10
+ import { computed, nextTick, onMounted, onUnmounted, ref, shallowRef, watch } from 'vue'
11
+
12
+ const props = withDefaults(defineProps<{
13
+ /** Index of the selected item, 0 to count - 1 (v-model) */
14
+ modelValue: number
15
+ /** Number of items; the wheel wraps around after the last one */
16
+ count: number
17
+ /**
18
+ * Accessible name of the field. Include the range when it isn't obvious ("Select year between 2000 and
19
+ * 2025"): the wheel wraps, so screen readers can't tell where it ends.
20
+ */
21
+ label?: string
22
+ /** Text for each item (also what screen readers announce). Defaults to the index with at least 2 digits. */
23
+ format?: (index: number) => string
24
+ /** Height in px; three items fit in the default 200 (ScrollFieldDefaults.ScrollFieldHeight) */
25
+ height?: number
26
+ /** Disables the field */
27
+ disabled?: boolean
28
+ }>(), {
29
+ format: (index: number) => String(index).padStart(2, '0'),
30
+ height: 200,
31
+ disabled: false,
32
+ })
33
+
34
+ const emit = defineEmits<{ 'update:modelValue': [number] }>()
35
+ const slots = defineSlots<{ item?: (props: { index: number; selected: boolean }) => unknown }>()
36
+ const custom = computed(() => !!slots.item)
37
+
38
+ // Pager pages are a fixed ScrollFieldHeight / 3, whatever the field's own height
39
+ const ITEM = 200 / 3
40
+ // PagerDefaults: the snap after a fling is spring(StiffnessMediumLow); animateScrollToPage uses spring()
41
+ const SNAP = { stiffness: 400, dampingRatio: 1 }
42
+ const SCROLL_TO = { stiffness: 1500, dampingRatio: 1 }
43
+ // Item text emphasis: MotionScheme.fastSpatialSpec() (Standard)
44
+ const EMPHASIS = { stiffness: 1400, dampingRatio: 0.9 }
45
+ // PagerDefaults' snapVelocityThreshold (400dp/s)
46
+ const FLING_VELOCITY = 400
47
+
48
+ const mod = (n: number) => ((n % props.count) + props.count) % props.count
49
+
50
+ // ── Position (in items, not wrapped) ──────────────────────────────────────────────────────────
51
+ let pos = props.count > 0 ? mod(props.modelValue) : 0
52
+ let vel = 0
53
+ let target: number | null = null
54
+ let spec = SNAP
55
+ const selectedPage = ref(Math.round(pos))
56
+ const range = computed(() => Math.ceil(props.height / 2 / ITEM) + 1)
57
+ const pages = shallowRef<number[]>([])
58
+
59
+ function updatePages() {
60
+ const base = Math.round(pos)
61
+ if (pages.value[range.value] === base) return
62
+ pages.value = Array.from({ length: range.value * 2 + 1 }, (_, i) => base - range.value + i)
63
+ }
64
+ updatePages()
65
+
66
+ // ── Per-item emphasis (0 → 1 as an item becomes the selected one) ───────────────────────────────
67
+ const emphasis = new Map<number, { f: number; v: number }>()
68
+ const itemEls = new Map<number, HTMLElement>()
69
+
70
+ function setItemEl(page: number, el: unknown) {
71
+ if (el instanceof HTMLElement) itemEls.set(page, el)
72
+ else itemEls.delete(page)
73
+ }
74
+
75
+ function write() {
76
+ const center = props.height / 2 - ITEM / 2
77
+ for (const [page, el] of itemEls) {
78
+ el.style.transform = `translateY(${center + (page - pos) * ITEM}px)`
79
+ if (custom.value) continue
80
+ const f = emphasis.get(page)?.f ?? (page === selectedPage.value ? 1 : 0)
81
+ const text = el.firstElementChild as HTMLElement | null
82
+ if (!text) continue
83
+ // displayMedium (45/52, regular) → displayLargeEmphasized (57/64, medium)
84
+ text.style.fontSize = `${45 + 12 * f}px`
85
+ text.style.lineHeight = `${52 + 12 * f}px`
86
+ text.style.fontWeight = String(Math.round(400 + 100 * f))
87
+ text.style.letterSpacing = '0'
88
+ text.style.color = props.disabled
89
+ ? ''
90
+ : `color-mix(in srgb, var(--color-on-surface) ${Math.round(f * 100)}%, var(--color-outline))`
91
+ }
92
+ }
93
+
94
+ // ── Animation loop ──────────────────────────────────────────────────────────────────────────────
95
+ let raf = 0
96
+ let last = 0
97
+ let dragging = false
98
+ // The value this wheel itself last emitted: v-model hands it back while the wheel is still moving
99
+ let lastEmitted: number | null = null
100
+
101
+ function setSelected() {
102
+ const page = Math.round(pos)
103
+ if (page === selectedPage.value) return
104
+ selectedPage.value = page
105
+ updatePages()
106
+ if (props.count > 0) {
107
+ lastEmitted = mod(page)
108
+ emit('update:modelValue', lastEmitted)
109
+ }
110
+ }
111
+
112
+ function frame(now: number) {
113
+ let dt = Math.min((now - last) / 1000, 1 / 30)
114
+ last = now
115
+ let busy = false
116
+ while (dt > 0) {
117
+ const h = Math.min(dt, 0.004)
118
+ dt -= h
119
+ if (target !== null && !dragging) {
120
+ const c = 2 * spec.dampingRatio * Math.sqrt(spec.stiffness)
121
+ vel += (-spec.stiffness * (pos - target) - c * vel) * h
122
+ pos += vel * h
123
+ }
124
+ for (const [page, e] of emphasis) {
125
+ const goal = page === selectedPage.value ? 1 : 0
126
+ const c = 2 * EMPHASIS.dampingRatio * Math.sqrt(EMPHASIS.stiffness)
127
+ e.v += (-EMPHASIS.stiffness * (e.f - goal) - c * e.v) * h
128
+ e.f += e.v * h
129
+ }
130
+ }
131
+ if (target !== null && !dragging) {
132
+ if (Math.abs(pos - target) < 0.001 && Math.abs(vel) < 0.01) {
133
+ pos = target
134
+ vel = 0
135
+ target = null
136
+ } else busy = true
137
+ }
138
+ for (const [page, e] of emphasis) {
139
+ const goal = page === selectedPage.value ? 1 : 0
140
+ if (Math.abs(e.f - goal) < 0.002 && Math.abs(e.v) < 0.01) {
141
+ e.f = goal
142
+ e.v = 0
143
+ if (goal === 0) emphasis.delete(page)
144
+ } else busy = true
145
+ }
146
+ setSelected()
147
+ write()
148
+ raf = busy || dragging ? requestAnimationFrame(frame) : 0
149
+ }
150
+
151
+ function kick() {
152
+ if (raf) return
153
+ last = performance.now()
154
+ raf = requestAnimationFrame(frame)
155
+ }
156
+
157
+ function animateTo(page: number, s = SCROLL_TO) {
158
+ target = page
159
+ spec = s
160
+ kick()
161
+ }
162
+
163
+ watch(selectedPage, (page, prev) => {
164
+ // Both the item leaving and the one arriving animate their emphasis
165
+ if (!emphasis.has(prev)) emphasis.set(prev, { f: 1, v: 0 })
166
+ if (!emphasis.has(page)) emphasis.set(page, { f: 0, v: 0 })
167
+ kick()
168
+ })
169
+
170
+ // Outside changes take the short way around the wheel
171
+ watch(() => props.modelValue, (v) => {
172
+ // Our own value coming back through v-model mid-fling isn't an outside change. Treating it as one
173
+ // retargeted the snap toward an item the wheel had already passed, and on a short wheel (4 items)
174
+ // the shortest-way-around math flipped the target back and forth forever.
175
+ if (props.count <= 0 || v === lastEmitted) return
176
+ const current = target ?? Math.round(pos)
177
+ let d = mod(v) - mod(current)
178
+ if (d > props.count / 2) d -= props.count
179
+ if (d < -props.count / 2) d += props.count
180
+ if (d !== 0) animateTo(current + d)
181
+ })
182
+
183
+ watch(pages, () => nextTick(write))
184
+ watch(() => [props.height, props.disabled], () => { updatePages(); nextTick(write) })
185
+
186
+ // ── Pointer ─────────────────────────────────────────────────────────────────────────────────────
187
+ const rootEl = ref<HTMLElement>()
188
+ let startY = 0
189
+ let startPos = 0
190
+ let moved = false
191
+ let samples: { t: number; y: number }[] = []
192
+
193
+ function onPointerDown(e: PointerEvent) {
194
+ if (props.disabled || e.button !== 0) return
195
+ rootEl.value?.setPointerCapture?.(e.pointerId)
196
+ dragging = true
197
+ moved = false
198
+ startY = e.clientY
199
+ startPos = pos
200
+ target = null
201
+ vel = 0
202
+ samples = [{ t: e.timeStamp, y: e.clientY }]
203
+ kick()
204
+ }
205
+
206
+ function onPointerMove(e: PointerEvent) {
207
+ if (!dragging) return
208
+ if (Math.abs(e.clientY - startY) > 4) moved = true
209
+ pos = startPos - (e.clientY - startY) / ITEM
210
+ samples.push({ t: e.timeStamp, y: e.clientY })
211
+ samples = samples.filter((s) => e.timeStamp - s.t < 100)
212
+ }
213
+
214
+ function onPointerUp(e: PointerEvent) {
215
+ if (!dragging) return
216
+ dragging = false
217
+ if (!moved) {
218
+ // A click scrolls the clicked item to the center
219
+ const rect = rootEl.value!.getBoundingClientRect()
220
+ animateTo(Math.round(pos + (e.clientY - rect.top - props.height / 2) / ITEM))
221
+ return
222
+ }
223
+ // VelocityTracker: the last 100ms of movement, none if the pointer had stopped for 40ms before release
224
+ const recent = samples.filter((s) => e.timeStamp - s.t < 100)
225
+ const first = recent[0]
226
+ const lastSample = recent[recent.length - 1]
227
+ const dt = first && lastSample && e.timeStamp - lastSample.t <= 40 ? (lastSample.t - first.t) / 1000 : 0
228
+ const vy = dt > 0 ? (lastSample!.y - first!.y) / dt : 0
229
+ vel = -vy / ITEM
230
+ // PagerSnapDistance.atMost(1): a fling moves on to the next item in its direction, no further
231
+ let page: number
232
+ if (Math.abs(vy) > FLING_VELOCITY) page = vy < 0 ? Math.floor(pos) + 1 : Math.ceil(pos) - 1
233
+ else page = Math.round(pos)
234
+ animateTo(page, SNAP)
235
+ }
236
+
237
+ // ── Wheel and keys ──────────────────────────────────────────────────────────────────────────────
238
+ let wheelAcc = 0
239
+ function onWheel(e: WheelEvent) {
240
+ if (props.disabled) return
241
+ e.preventDefault()
242
+ wheelAcc += e.deltaMode === 1 ? e.deltaY * 16 : e.deltaY
243
+ const steps = Math.trunc(wheelAcc / 40)
244
+ if (!steps) return
245
+ wheelAcc -= steps * 40
246
+ animateTo((target ?? Math.round(pos)) + steps)
247
+ }
248
+
249
+ function onKeydown(e: KeyboardEvent) {
250
+ if (props.disabled) return
251
+ const step = e.key === 'ArrowDown' ? 1 : e.key === 'ArrowUp' ? -1 : 0
252
+ if (!step) return
253
+ e.preventDefault()
254
+ animateTo((target ?? Math.round(pos)) + step)
255
+ }
256
+
257
+ onMounted(() => {
258
+ rootEl.value?.addEventListener('wheel', onWheel, { passive: false })
259
+ write()
260
+ })
261
+ onUnmounted(() => {
262
+ cancelAnimationFrame(raf)
263
+ rootEl.value?.removeEventListener('wheel', onWheel)
264
+ })
265
+
266
+ const selectedIndex = computed(() => (props.count > 0 ? mod(selectedPage.value) : 0))
267
+ // The widest label sets the width (the first and last are the longest for numbers)
268
+ const sizer = computed(() => {
269
+ if (props.count <= 0) return ''
270
+ const a = props.format(0)
271
+ const b = props.format(props.count - 1)
272
+ return a.length >= b.length ? a : b
273
+ })
274
+ </script>
275
+
276
+ <template>
277
+ <div
278
+ ref="rootEl"
279
+ role="slider"
280
+ :tabindex="disabled ? -1 : 0"
281
+ :aria-label="label"
282
+ :aria-valuenow="selectedIndex"
283
+ aria-valuemin="0"
284
+ :aria-valuemax="Math.max(count - 1, 0)"
285
+ :aria-valuetext="count > 0 ? format(selectedIndex) : undefined"
286
+ :aria-disabled="disabled || undefined"
287
+ class="relative inline-block touch-none overflow-hidden rounded-lg outline-none select-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
288
+ :class="disabled ? 'cursor-default bg-surface-container-lowest/38 text-on-surface/38' : 'cursor-grab bg-surface-container-lowest'"
289
+ :style="{ height: `${height}px` }"
290
+ @pointerdown="onPointerDown"
291
+ @pointermove="onPointerMove"
292
+ @pointerup="onPointerUp"
293
+ @pointercancel="onPointerUp"
294
+ @keydown="onKeydown"
295
+ >
296
+ <!-- Sets the width: the widest label in the selected style -->
297
+ <span v-if="!custom" aria-hidden="true" class="invisible block px-4 font-medium" style="font-size: 57px; line-height: 0; height: 0">{{ sizer }}</span>
298
+ <div
299
+ v-for="page in pages"
300
+ :key="page"
301
+ :ref="(el) => setItemEl(page, el)"
302
+ aria-hidden="true"
303
+ class="pointer-events-none absolute inset-x-0 top-0 flex items-center justify-center"
304
+ :style="{ height: `${ITEM}px` }"
305
+ >
306
+ <slot v-if="custom" name="item" :index="mod(page)" :selected="page === selectedPage" />
307
+ <span v-else class="font-sans tabular-nums">{{ count > 0 ? format(mod(page)) : '' }}</span>
308
+ </div>
309
+ </div>
310
+ </template>
@@ -1,10 +1,13 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
- * A constrained scroll area — vertical, horizontal, or both directions.
3
+ * A constrained scroll area — vertical, horizontal, or both directions. Shows the browser's scrollbar, or with
4
+ * `scrollbar="indicator"` M3's thin visual-only scrollbar (Compose's `Modifier.nonInteractiveScrollbar`), which
5
+ * appears while scrolling and fades out after.
4
6
  *
5
7
  * @slot default The scrolling content.
6
8
  */
7
- import { computed } from 'vue'
9
+ import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
10
+ import { fastOutSlowIn } from '../composables/_useSpring'
8
11
 
9
12
  const props = withDefaults(
10
13
  defineProps<{
@@ -14,8 +17,15 @@ const props = withDefaults(
14
17
  height?: string
15
18
  /** Maximum height (CSS value) */
16
19
  maxHeight?: string
20
+ /**
21
+ * `native`: the browser's scrollbar. `indicator`: M3's thin scrollbar, visual only (scroll with the wheel,
22
+ * touch or keys). `none`: no scrollbar at all.
23
+ */
24
+ scrollbar?: 'native' | 'indicator' | 'none'
25
+ /** With `scrollbar="indicator"`: show it only while scrolling, fading out after (false keeps it visible) */
26
+ scrollbarFade?: boolean
17
27
  }>(),
18
- { direction: 'vertical' },
28
+ { direction: 'vertical', scrollbar: 'native', scrollbarFade: true },
19
29
  )
20
30
 
21
31
  const overflowClasses: Record<string, string> = {
@@ -24,16 +34,161 @@ const overflowClasses: Record<string, string> = {
24
34
  both: 'overflow-auto',
25
35
  }
26
36
 
27
- const classes = computed(() => overflowClasses[props.direction])
37
+ const classes = computed(() => [
38
+ overflowClasses[props.direction],
39
+ props.scrollbar !== 'native' && 'm3-scrollable-hidden relative',
40
+ ])
28
41
 
29
42
  const style = computed(() => ({
30
43
  height: props.height,
31
44
  maxHeight: props.maxHeight,
32
45
  }))
46
+
47
+ // ── Indicator (NonInteractiveScrollbarDefaults) ─────────────────────────────────────────────────
48
+ const THICKNESS = 4
49
+ const MIN_LENGTH = 24
50
+ const MAX_FRACTION = 0.9
51
+ const MAIN_INSET = 2
52
+ const FADE_DELAY = 400
53
+ const FADE_DURATION = 250
54
+
55
+ const rootEl = ref<HTMLElement>()
56
+ const vThumb = ref<HTMLElement>()
57
+ const hThumb = ref<HTMLElement>()
58
+ const showV = computed(() => props.scrollbar === 'indicator' && props.direction !== 'horizontal')
59
+ const showH = computed(() => props.scrollbar === 'indicator' && props.direction !== 'vertical')
60
+
61
+ let alpha = props.scrollbarFade ? 0 : 1
62
+ let fadeTimer = 0
63
+ let fadeRaf = 0
64
+ let rtl = false
65
+
66
+ // One axis: thumb length and offset along a track inset by MAIN_INSET at both ends
67
+ function thumb(viewport: number, content: number, offset: number) {
68
+ if (content <= viewport || viewport === 0) return null
69
+ const track = viewport - 2 * MAIN_INSET
70
+ if (track <= 0 || track < MIN_LENGTH) return null
71
+ const length = Math.min(Math.max(track * (viewport / content), MIN_LENGTH), track * MAX_FRACTION)
72
+ const max = content - viewport
73
+ return { length, offset: MAIN_INSET + (max > 0 ? (offset / max) * (track - length) : 0) }
74
+ }
75
+
76
+ function draw() {
77
+ const el = rootEl.value
78
+ if (!el) return
79
+ const { scrollTop, scrollHeight, clientHeight, clientWidth, scrollWidth } = el
80
+ // scrollLeft is negative (or reversed) in RTL; work with the distance from the start
81
+ const scrollLeft = Math.abs(el.scrollLeft)
82
+ if (vThumb.value) {
83
+ const t = thumb(clientHeight, scrollHeight, scrollTop)
84
+ const s = vThumb.value.style
85
+ s.display = t ? '' : 'none'
86
+ if (t) {
87
+ // The thumb lives in the scrolled content, so it's moved back by the scroll offset
88
+ const x = el.scrollLeft
89
+ s.height = `${t.length}px`
90
+ s.transform = `translate(${x}px, ${scrollTop + t.offset}px)`
91
+ s.opacity = String(alpha)
92
+ }
93
+ }
94
+ if (hThumb.value) {
95
+ const t = thumb(clientWidth, scrollWidth, scrollLeft)
96
+ const s = hThumb.value.style
97
+ s.display = t ? '' : 'none'
98
+ if (t) {
99
+ const start = rtl ? clientWidth - t.offset - t.length : t.offset
100
+ s.width = `${t.length}px`
101
+ s.transform = `translate(${el.scrollLeft + start}px, ${scrollTop + clientHeight - THICKNESS}px)`
102
+ s.opacity = String(alpha)
103
+ }
104
+ }
105
+ }
106
+
107
+ // Shown at full opacity while scrolling; FADE_DELAY after the last scroll it fades out over FADE_DURATION
108
+ function onScroll() {
109
+ if (props.scrollbar !== 'indicator') return
110
+ if (props.scrollbarFade) {
111
+ cancelAnimationFrame(fadeRaf)
112
+ alpha = 1
113
+ clearTimeout(fadeTimer)
114
+ fadeTimer = window.setTimeout(fadeOut, FADE_DELAY)
115
+ }
116
+ draw()
117
+ }
118
+
119
+ function fadeOut() {
120
+ const start = performance.now()
121
+ const step = (now: number) => {
122
+ const t = Math.min(1, (now - start) / FADE_DURATION)
123
+ // tween()'s default easing
124
+ alpha = 1 - fastOutSlowIn(t)
125
+ draw()
126
+ if (t < 1) fadeRaf = requestAnimationFrame(step)
127
+ }
128
+ fadeRaf = requestAnimationFrame(step)
129
+ }
130
+
131
+ let resizeObserver: ResizeObserver | undefined
132
+ onMounted(() => {
133
+ if (!rootEl.value) return
134
+ rtl = getComputedStyle(rootEl.value).direction === 'rtl'
135
+ resizeObserver = new ResizeObserver(draw)
136
+ resizeObserver.observe(rootEl.value)
137
+ if (rootEl.value.firstElementChild) resizeObserver.observe(rootEl.value.firstElementChild)
138
+ draw()
139
+ })
140
+ onUnmounted(() => {
141
+ resizeObserver?.disconnect()
142
+ clearTimeout(fadeTimer)
143
+ cancelAnimationFrame(fadeRaf)
144
+ })
145
+ watch(() => [props.scrollbar, props.scrollbarFade, props.direction], () => {
146
+ alpha = props.scrollbarFade ? 0 : 1
147
+ requestAnimationFrame(draw)
148
+ })
33
149
  </script>
34
150
 
35
151
  <template>
36
- <div :class="classes" :style="style">
152
+ <div ref="rootEl" :class="classes" :style="style" @scroll.passive="onScroll">
37
153
  <slot />
154
+ <span
155
+ v-if="showV"
156
+ ref="vThumb"
157
+ aria-hidden="true"
158
+ class="m3-scrollable-thumb m3-scrollable-thumb-v"
159
+ />
160
+ <span
161
+ v-if="showH"
162
+ ref="hThumb"
163
+ aria-hidden="true"
164
+ class="m3-scrollable-thumb m3-scrollable-thumb-h"
165
+ />
38
166
  </div>
39
167
  </template>
168
+
169
+ <style>
170
+ .m3-scrollable-hidden {
171
+ scrollbar-width: none;
172
+ }
173
+ .m3-scrollable-hidden::-webkit-scrollbar {
174
+ display: none;
175
+ }
176
+ .m3-scrollable-thumb {
177
+ position: absolute;
178
+ top: 0;
179
+ border-radius: 9999px;
180
+ /* NonInteractiveScrollbarDefaults.thumbColor: Outline at 70% */
181
+ background: color-mix(in srgb, var(--color-outline) 70%, transparent);
182
+ pointer-events: none;
183
+ opacity: 0;
184
+ }
185
+ /* Thickness 4dp; the vertical one sits at the end edge, the horizontal one at the bottom */
186
+ .m3-scrollable-thumb-v {
187
+ inset-inline-end: 0;
188
+ width: 4px;
189
+ }
190
+ .m3-scrollable-thumb-h {
191
+ left: 0;
192
+ height: 4px;
193
+ }
194
+ </style>
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { computed, onMounted, onUnmounted, ref } from 'vue'
10
10
  import MIcon from './MIcon.vue'
11
+ import { vRipple } from '../directives/vRipple'
11
12
 
12
13
  export interface SplitButtonItem {
13
14
  label: string
@@ -33,6 +34,8 @@ const props = withDefaults(defineProps<{
33
34
  disabled?: boolean
34
35
  /** Dropdown menu items; see `SplitButtonItem` */
35
36
  items?: SplitButtonItem[]
37
+ /** Any CSS color for the press ripple; defaults to the button's content color */
38
+ rippleColor?: string
36
39
  }>(), {
37
40
  variant: 'tonal',
38
41
  color: 'primary',
@@ -134,16 +137,6 @@ function toggleMenu() {
134
137
  open.value = !open.value
135
138
  }
136
139
 
137
- function createRipple(event: PointerEvent) {
138
- const button = event.currentTarget as HTMLElement
139
- const rect = button.getBoundingClientRect()
140
- const d = Math.max(rect.width, rect.height) * 2
141
- const el = document.createElement('span')
142
- el.className = 'm3-ripple'
143
- el.style.cssText = `width:${d}px;height:${d}px;top:${event.clientY - rect.top - d / 2}px;left:${event.clientX - rect.left - d / 2}px`
144
- button.appendChild(el)
145
- el.addEventListener('animationend', () => el.remove(), { once: true })
146
- }
147
140
 
148
141
  function selectItem(item: SplitButtonItem) {
149
142
  if (item.disabled) return
@@ -187,7 +180,7 @@ onUnmounted(() => {
187
180
  ]"
188
181
  :style="{ borderRadius: `${sizeConfig.r}px ${sizeConfig.ri}px ${sizeConfig.ri}px ${sizeConfig.r}px` }"
189
182
  :disabled="disabled"
190
- @pointerdown="createRipple"
183
+ v-ripple="rippleColor"
191
184
  @click="emit('click', $event)"
192
185
  >
193
186
  <MIcon v-if="icon" :name="icon" :size="sizeConfig.icon" />
@@ -205,7 +198,7 @@ onUnmounted(() => {
205
198
  ]"
206
199
  :style="{ borderRadius: open ? `${sizeConfig.r}px` : `${sizeConfig.ri}px ${sizeConfig.r}px ${sizeConfig.r}px ${sizeConfig.ri}px` }"
207
200
  :disabled="disabled"
208
- @pointerdown="createRipple"
201
+ v-ripple="rippleColor"
209
202
  @click="toggleMenu"
210
203
  >
211
204
  <MIcon
@@ -1,11 +1,13 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
- * Two resizable panels divided by a draggable handle.
3
+ * Two resizable panels divided by a draggable bar with M3's drag handle (MDragHandle) on it. The bar is also a
4
+ * keyboard separator: arrow keys move it (Shift for 10%), Home/End jump to the limits.
4
5
  *
5
6
  * @slot first Content for the first (left/top) panel
6
7
  * @slot second Content for the second (right/bottom) panel
7
8
  */
8
9
  import { ref, computed, onBeforeUnmount } from 'vue'
10
+ import MDragHandle from './MDragHandle.vue'
9
11
 
10
12
  const props = withDefaults(
11
13
  defineProps<{
@@ -40,8 +42,23 @@ const panelBStyle = computed(() =>
40
42
  )
41
43
 
42
44
  function onPointerDown(e: PointerEvent) {
45
+ if (e.button !== 0) return
43
46
  dragging.value = true
44
- ;(e.target as HTMLElement).setPointerCapture(e.pointerId)
47
+ ;(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId)
48
+ }
49
+
50
+ function onKeydown(e: KeyboardEvent) {
51
+ const step = e.shiftKey ? 10 : 1
52
+ const back = isHorizontal.value ? 'ArrowLeft' : 'ArrowUp'
53
+ const fwd = isHorizontal.value ? 'ArrowRight' : 'ArrowDown'
54
+ let next: number
55
+ if (e.key === back) next = split.value - step
56
+ else if (e.key === fwd) next = split.value + step
57
+ else if (e.key === 'Home') next = props.min
58
+ else if (e.key === 'End') next = props.max
59
+ else return
60
+ e.preventDefault()
61
+ split.value = Math.min(props.max, Math.max(props.min, next))
45
62
  }
46
63
 
47
64
  function onPointerMove(e: PointerEvent) {
@@ -83,22 +100,28 @@ onBeforeUnmount(() => {
83
100
  </div>
84
101
 
85
102
  <div
86
- class="z-10 flex shrink-0 items-center justify-center transition-colors"
103
+ role="separator"
104
+ tabindex="0"
105
+ :aria-orientation="isHorizontal ? 'vertical' : 'horizontal'"
106
+ :aria-valuenow="Math.round(split)"
107
+ :aria-valuemin="min"
108
+ :aria-valuemax="max"
109
+ class="group z-10 flex shrink-0 items-center justify-center outline-none transition-colors"
87
110
  :class="[
88
- isHorizontal
89
- ? 'w-2 cursor-col-resize flex-col'
90
- : 'h-2 cursor-row-resize flex-row',
91
- dragging ? 'bg-primary/20' : 'bg-outline-variant/40 hover:bg-primary/12',
111
+ isHorizontal ? 'w-2 cursor-col-resize' : 'h-2 cursor-row-resize',
112
+ dragging ? 'bg-primary/20' : 'bg-outline-variant/40 hover:bg-primary/12 focus-visible:bg-primary/12',
92
113
  ]"
93
114
  style="touch-action: none"
94
115
  @pointerdown="onPointerDown"
95
116
  @pointermove="onPointerMove"
96
117
  @pointerup="onPointerUp"
97
118
  @pointercancel="onPointerUp"
119
+ @keydown="onKeydown"
98
120
  >
99
- <div
100
- class="rounded-full bg-outline"
101
- :class="isHorizontal ? 'h-6 w-1' : 'h-1 w-6'"
121
+ <MDragHandle
122
+ :orientation="isHorizontal ? 'vertical' : 'horizontal'"
123
+ :dragging="dragging"
124
+ class="rounded-md group-focus-visible:outline-2 group-focus-visible:outline-offset-2 group-focus-visible:outline-primary"
102
125
  />
103
126
  </div>
104
127