@m3ui-vue/m3ui-vue 0.9.1 → 0.9.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.
- package/README.md +41 -0
- package/dist/API.md +199 -54
- package/dist/{MTextField-pJU1vFaR.js → MTextField-Ck0oWDMk.js} +8 -2
- package/dist/MTextField-Ck0oWDMk.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MBox.vue.d.ts +3 -3
- package/dist/components/MColorPicker.vue.d.ts +13 -1
- package/dist/components/MColorPickerModal.vue.d.ts +14 -2
- package/dist/components/MDragHandle.vue.d.ts +12 -0
- package/dist/components/MEmojiSelector.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +1 -1
- package/dist/components/MFabMenu.vue.d.ts +1 -1
- package/dist/components/MListItem.vue.d.ts +5 -5
- package/dist/components/MOverlay.vue.d.ts +1 -1
- package/dist/components/MScrollField.vue.d.ts +40 -0
- package/dist/components/MScrollable.vue.d.ts +9 -0
- package/dist/components/MSplitter.vue.d.ts +2 -2
- package/dist/components/MSwipeToDismiss.vue.d.ts +49 -0
- package/dist/components/MTransition.vue.d.ts +34 -0
- package/dist/components/MWindow.vue.d.ts +1 -1
- package/dist/components/_MColorPanel.vue.d.ts +18 -0
- package/dist/composables/_colorUtils.d.ts +34 -0
- package/dist/composables/_themeColor.d.ts +1 -0
- package/dist/composables/_useSpring.d.ts +15 -0
- package/dist/composables/dynamicColor.d.ts +18 -0
- package/dist/composables/useColorPalette.d.ts +27 -0
- package/dist/composables/useLocale.d.ts +10 -0
- package/dist/index.d.ts +9 -1
- package/dist/locales/index.js +70 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +4808 -3622
- package/dist/m3ui.js.map +1 -1
- package/dist/material-color-utilities-Cktu8DBU.js +3160 -0
- package/dist/material-color-utilities-Cktu8DBU.js.map +1 -0
- package/dist/plugin.d.ts +18 -0
- package/dist/rich-text-editor.js +2 -2
- package/dist/styles/palettes.css +546 -0
- package/dist/styles/theme.css +77 -0
- package/dist/styles.css +1 -1
- package/dist/{useLocale-Drihe5G2.js → useLocale-CFW8cIzL.js} +11 -1
- package/dist/useLocale-CFW8cIzL.js.map +1 -0
- package/package.json +2 -1
- package/src/__tests__/MButtonGroup.test.ts +51 -0
- package/src/__tests__/MColorPicker.test.ts +141 -0
- package/src/__tests__/MDragHandle.test.ts +57 -0
- package/src/__tests__/MListItemShape.test.ts +76 -0
- package/src/__tests__/MScrollField.test.ts +41 -0
- package/src/__tests__/MScrollable.test.ts +42 -0
- package/src/__tests__/MSwipeToDismiss.test.ts +104 -0
- package/src/__tests__/MTransition.test.ts +27 -0
- package/src/__tests__/dynamicColor.test.ts +191 -0
- package/src/components/MBox.vue +4 -2
- package/src/components/MButtonGroup.vue +116 -35
- package/src/components/MColorPicker.vue +114 -210
- package/src/components/MColorPickerModal.vue +50 -251
- package/src/components/MDragHandle.vue +54 -0
- package/src/components/MList.vue +33 -3
- package/src/components/MListItem.vue +75 -5
- package/src/components/MScrollField.vue +302 -0
- package/src/components/MScrollable.vue +160 -5
- package/src/components/MSplitter.vue +33 -10
- package/src/components/MSwipeToDismiss.vue +247 -0
- package/src/components/MTransition.vue +149 -0
- package/src/components/_MColorPanel.vue +478 -0
- package/src/composables/_colorUtils.ts +107 -0
- package/src/composables/_themeColor.ts +33 -0
- package/src/composables/_useSpring.ts +89 -0
- package/src/composables/dynamicColor.ts +85 -0
- package/src/composables/useColorPalette.ts +171 -25
- package/src/composables/useFieldBg.ts +6 -0
- package/src/composables/useLocale.ts +22 -0
- package/src/index.ts +9 -1
- package/src/locales/de.ts +10 -0
- package/src/locales/es.ts +10 -0
- package/src/locales/fr.ts +10 -0
- package/src/locales/ja.ts +10 -0
- package/src/locales/ko.ts +10 -0
- package/src/locales/pt.ts +10 -0
- package/src/locales/zh.ts +10 -0
- package/src/plugin.ts +28 -7
- package/src/styles/palettes.css +546 -0
- package/src/styles/theme.css +77 -0
- package/dist/MTextField-pJU1vFaR.js.map +0 -1
- package/dist/useLocale-Drihe5G2.js.map +0 -1
|
@@ -0,0 +1,302 @@
|
|
|
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
|
+
|
|
99
|
+
function setSelected() {
|
|
100
|
+
const page = Math.round(pos)
|
|
101
|
+
if (page === selectedPage.value) return
|
|
102
|
+
selectedPage.value = page
|
|
103
|
+
updatePages()
|
|
104
|
+
if (props.count > 0) emit('update:modelValue', mod(page))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function frame(now: number) {
|
|
108
|
+
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
109
|
+
last = now
|
|
110
|
+
let busy = false
|
|
111
|
+
while (dt > 0) {
|
|
112
|
+
const h = Math.min(dt, 0.004)
|
|
113
|
+
dt -= h
|
|
114
|
+
if (target !== null && !dragging) {
|
|
115
|
+
const c = 2 * spec.dampingRatio * Math.sqrt(spec.stiffness)
|
|
116
|
+
vel += (-spec.stiffness * (pos - target) - c * vel) * h
|
|
117
|
+
pos += vel * h
|
|
118
|
+
}
|
|
119
|
+
for (const [page, e] of emphasis) {
|
|
120
|
+
const goal = page === selectedPage.value ? 1 : 0
|
|
121
|
+
const c = 2 * EMPHASIS.dampingRatio * Math.sqrt(EMPHASIS.stiffness)
|
|
122
|
+
e.v += (-EMPHASIS.stiffness * (e.f - goal) - c * e.v) * h
|
|
123
|
+
e.f += e.v * h
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (target !== null && !dragging) {
|
|
127
|
+
if (Math.abs(pos - target) < 0.001 && Math.abs(vel) < 0.01) {
|
|
128
|
+
pos = target
|
|
129
|
+
vel = 0
|
|
130
|
+
target = null
|
|
131
|
+
} else busy = true
|
|
132
|
+
}
|
|
133
|
+
for (const [page, e] of emphasis) {
|
|
134
|
+
const goal = page === selectedPage.value ? 1 : 0
|
|
135
|
+
if (Math.abs(e.f - goal) < 0.002 && Math.abs(e.v) < 0.01) {
|
|
136
|
+
e.f = goal
|
|
137
|
+
e.v = 0
|
|
138
|
+
if (goal === 0) emphasis.delete(page)
|
|
139
|
+
} else busy = true
|
|
140
|
+
}
|
|
141
|
+
setSelected()
|
|
142
|
+
write()
|
|
143
|
+
raf = busy || dragging ? requestAnimationFrame(frame) : 0
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function kick() {
|
|
147
|
+
if (raf) return
|
|
148
|
+
last = performance.now()
|
|
149
|
+
raf = requestAnimationFrame(frame)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function animateTo(page: number, s = SCROLL_TO) {
|
|
153
|
+
target = page
|
|
154
|
+
spec = s
|
|
155
|
+
kick()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
watch(selectedPage, (page, prev) => {
|
|
159
|
+
// Both the item leaving and the one arriving animate their emphasis
|
|
160
|
+
if (!emphasis.has(prev)) emphasis.set(prev, { f: 1, v: 0 })
|
|
161
|
+
if (!emphasis.has(page)) emphasis.set(page, { f: 0, v: 0 })
|
|
162
|
+
kick()
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
// Outside changes take the short way around the wheel
|
|
166
|
+
watch(() => props.modelValue, (v) => {
|
|
167
|
+
if (props.count <= 0) return
|
|
168
|
+
const current = target ?? Math.round(pos)
|
|
169
|
+
let d = mod(v) - mod(current)
|
|
170
|
+
if (d > props.count / 2) d -= props.count
|
|
171
|
+
if (d < -props.count / 2) d += props.count
|
|
172
|
+
if (d !== 0) animateTo(current + d)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
watch(pages, () => nextTick(write))
|
|
176
|
+
watch(() => [props.height, props.disabled], () => { updatePages(); nextTick(write) })
|
|
177
|
+
|
|
178
|
+
// ── Pointer ─────────────────────────────────────────────────────────────────────────────────────
|
|
179
|
+
const rootEl = ref<HTMLElement>()
|
|
180
|
+
let startY = 0
|
|
181
|
+
let startPos = 0
|
|
182
|
+
let moved = false
|
|
183
|
+
let samples: { t: number; y: number }[] = []
|
|
184
|
+
|
|
185
|
+
function onPointerDown(e: PointerEvent) {
|
|
186
|
+
if (props.disabled || e.button !== 0) return
|
|
187
|
+
rootEl.value?.setPointerCapture?.(e.pointerId)
|
|
188
|
+
dragging = true
|
|
189
|
+
moved = false
|
|
190
|
+
startY = e.clientY
|
|
191
|
+
startPos = pos
|
|
192
|
+
target = null
|
|
193
|
+
vel = 0
|
|
194
|
+
samples = [{ t: e.timeStamp, y: e.clientY }]
|
|
195
|
+
kick()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function onPointerMove(e: PointerEvent) {
|
|
199
|
+
if (!dragging) return
|
|
200
|
+
if (Math.abs(e.clientY - startY) > 4) moved = true
|
|
201
|
+
pos = startPos - (e.clientY - startY) / ITEM
|
|
202
|
+
samples.push({ t: e.timeStamp, y: e.clientY })
|
|
203
|
+
samples = samples.filter((s) => e.timeStamp - s.t < 100)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function onPointerUp(e: PointerEvent) {
|
|
207
|
+
if (!dragging) return
|
|
208
|
+
dragging = false
|
|
209
|
+
if (!moved) {
|
|
210
|
+
// A click scrolls the clicked item to the center
|
|
211
|
+
const rect = rootEl.value!.getBoundingClientRect()
|
|
212
|
+
animateTo(Math.round(pos + (e.clientY - rect.top - props.height / 2) / ITEM))
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
// VelocityTracker: the last 100ms of movement, none if the pointer had stopped for 40ms before release
|
|
216
|
+
const recent = samples.filter((s) => e.timeStamp - s.t < 100)
|
|
217
|
+
const first = recent[0]
|
|
218
|
+
const lastSample = recent[recent.length - 1]
|
|
219
|
+
const dt = first && lastSample && e.timeStamp - lastSample.t <= 40 ? (lastSample.t - first.t) / 1000 : 0
|
|
220
|
+
const vy = dt > 0 ? (lastSample!.y - first!.y) / dt : 0
|
|
221
|
+
vel = -vy / ITEM
|
|
222
|
+
// PagerSnapDistance.atMost(1): a fling moves on to the next item in its direction, no further
|
|
223
|
+
let page: number
|
|
224
|
+
if (Math.abs(vy) > FLING_VELOCITY) page = vy < 0 ? Math.floor(pos) + 1 : Math.ceil(pos) - 1
|
|
225
|
+
else page = Math.round(pos)
|
|
226
|
+
animateTo(page, SNAP)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ── Wheel and keys ──────────────────────────────────────────────────────────────────────────────
|
|
230
|
+
let wheelAcc = 0
|
|
231
|
+
function onWheel(e: WheelEvent) {
|
|
232
|
+
if (props.disabled) return
|
|
233
|
+
e.preventDefault()
|
|
234
|
+
wheelAcc += e.deltaMode === 1 ? e.deltaY * 16 : e.deltaY
|
|
235
|
+
const steps = Math.trunc(wheelAcc / 40)
|
|
236
|
+
if (!steps) return
|
|
237
|
+
wheelAcc -= steps * 40
|
|
238
|
+
animateTo((target ?? Math.round(pos)) + steps)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function onKeydown(e: KeyboardEvent) {
|
|
242
|
+
if (props.disabled) return
|
|
243
|
+
const step = e.key === 'ArrowDown' ? 1 : e.key === 'ArrowUp' ? -1 : 0
|
|
244
|
+
if (!step) return
|
|
245
|
+
e.preventDefault()
|
|
246
|
+
animateTo((target ?? Math.round(pos)) + step)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
onMounted(() => {
|
|
250
|
+
rootEl.value?.addEventListener('wheel', onWheel, { passive: false })
|
|
251
|
+
write()
|
|
252
|
+
})
|
|
253
|
+
onUnmounted(() => {
|
|
254
|
+
cancelAnimationFrame(raf)
|
|
255
|
+
rootEl.value?.removeEventListener('wheel', onWheel)
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
const selectedIndex = computed(() => (props.count > 0 ? mod(selectedPage.value) : 0))
|
|
259
|
+
// The widest label sets the width (the first and last are the longest for numbers)
|
|
260
|
+
const sizer = computed(() => {
|
|
261
|
+
if (props.count <= 0) return ''
|
|
262
|
+
const a = props.format(0)
|
|
263
|
+
const b = props.format(props.count - 1)
|
|
264
|
+
return a.length >= b.length ? a : b
|
|
265
|
+
})
|
|
266
|
+
</script>
|
|
267
|
+
|
|
268
|
+
<template>
|
|
269
|
+
<div
|
|
270
|
+
ref="rootEl"
|
|
271
|
+
role="slider"
|
|
272
|
+
:tabindex="disabled ? -1 : 0"
|
|
273
|
+
:aria-label="label"
|
|
274
|
+
:aria-valuenow="selectedIndex"
|
|
275
|
+
aria-valuemin="0"
|
|
276
|
+
:aria-valuemax="Math.max(count - 1, 0)"
|
|
277
|
+
:aria-valuetext="count > 0 ? format(selectedIndex) : undefined"
|
|
278
|
+
:aria-disabled="disabled || undefined"
|
|
279
|
+
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"
|
|
280
|
+
:class="disabled ? 'cursor-default bg-surface-container-lowest/38 text-on-surface/38' : 'cursor-grab bg-surface-container-lowest'"
|
|
281
|
+
:style="{ height: `${height}px` }"
|
|
282
|
+
@pointerdown="onPointerDown"
|
|
283
|
+
@pointermove="onPointerMove"
|
|
284
|
+
@pointerup="onPointerUp"
|
|
285
|
+
@pointercancel="onPointerUp"
|
|
286
|
+
@keydown="onKeydown"
|
|
287
|
+
>
|
|
288
|
+
<!-- Sets the width: the widest label in the selected style -->
|
|
289
|
+
<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>
|
|
290
|
+
<div
|
|
291
|
+
v-for="page in pages"
|
|
292
|
+
:key="page"
|
|
293
|
+
:ref="(el) => setItemEl(page, el)"
|
|
294
|
+
aria-hidden="true"
|
|
295
|
+
class="pointer-events-none absolute inset-x-0 top-0 flex items-center justify-center"
|
|
296
|
+
:style="{ height: `${ITEM}px` }"
|
|
297
|
+
>
|
|
298
|
+
<slot v-if="custom" name="item" :index="mod(page)" :selected="page === selectedPage" />
|
|
299
|
+
<span v-else class="font-sans tabular-nums">{{ count > 0 ? format(mod(page)) : '' }}</span>
|
|
300
|
+
</div>
|
|
301
|
+
</div>
|
|
302
|
+
</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(() =>
|
|
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>
|
|
@@ -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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
<
|
|
100
|
-
|
|
101
|
-
:
|
|
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
|
|