@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.
Files changed (86) hide show
  1. package/README.md +41 -0
  2. package/dist/API.md +199 -54
  3. package/dist/{MTextField-pJU1vFaR.js → MTextField-Ck0oWDMk.js} +8 -2
  4. package/dist/MTextField-Ck0oWDMk.js.map +1 -0
  5. package/dist/api.json +1 -1
  6. package/dist/code-editor.js +1 -1
  7. package/dist/components/MBox.vue.d.ts +3 -3
  8. package/dist/components/MColorPicker.vue.d.ts +13 -1
  9. package/dist/components/MColorPickerModal.vue.d.ts +14 -2
  10. package/dist/components/MDragHandle.vue.d.ts +12 -0
  11. package/dist/components/MEmojiSelector.vue.d.ts +1 -1
  12. package/dist/components/MFab.vue.d.ts +1 -1
  13. package/dist/components/MFabMenu.vue.d.ts +1 -1
  14. package/dist/components/MListItem.vue.d.ts +5 -5
  15. package/dist/components/MOverlay.vue.d.ts +1 -1
  16. package/dist/components/MScrollField.vue.d.ts +40 -0
  17. package/dist/components/MScrollable.vue.d.ts +9 -0
  18. package/dist/components/MSplitter.vue.d.ts +2 -2
  19. package/dist/components/MSwipeToDismiss.vue.d.ts +49 -0
  20. package/dist/components/MTransition.vue.d.ts +34 -0
  21. package/dist/components/MWindow.vue.d.ts +1 -1
  22. package/dist/components/_MColorPanel.vue.d.ts +18 -0
  23. package/dist/composables/_colorUtils.d.ts +34 -0
  24. package/dist/composables/_themeColor.d.ts +1 -0
  25. package/dist/composables/_useSpring.d.ts +15 -0
  26. package/dist/composables/dynamicColor.d.ts +18 -0
  27. package/dist/composables/useColorPalette.d.ts +27 -0
  28. package/dist/composables/useLocale.d.ts +10 -0
  29. package/dist/index.d.ts +9 -1
  30. package/dist/locales/index.js +70 -0
  31. package/dist/locales/index.js.map +1 -1
  32. package/dist/m3ui-vue.css +1 -1
  33. package/dist/m3ui.js +4808 -3622
  34. package/dist/m3ui.js.map +1 -1
  35. package/dist/material-color-utilities-Cktu8DBU.js +3160 -0
  36. package/dist/material-color-utilities-Cktu8DBU.js.map +1 -0
  37. package/dist/plugin.d.ts +18 -0
  38. package/dist/rich-text-editor.js +2 -2
  39. package/dist/styles/palettes.css +546 -0
  40. package/dist/styles/theme.css +77 -0
  41. package/dist/styles.css +1 -1
  42. package/dist/{useLocale-Drihe5G2.js → useLocale-CFW8cIzL.js} +11 -1
  43. package/dist/useLocale-CFW8cIzL.js.map +1 -0
  44. package/package.json +2 -1
  45. package/src/__tests__/MButtonGroup.test.ts +51 -0
  46. package/src/__tests__/MColorPicker.test.ts +141 -0
  47. package/src/__tests__/MDragHandle.test.ts +57 -0
  48. package/src/__tests__/MListItemShape.test.ts +76 -0
  49. package/src/__tests__/MScrollField.test.ts +41 -0
  50. package/src/__tests__/MScrollable.test.ts +42 -0
  51. package/src/__tests__/MSwipeToDismiss.test.ts +104 -0
  52. package/src/__tests__/MTransition.test.ts +27 -0
  53. package/src/__tests__/dynamicColor.test.ts +191 -0
  54. package/src/components/MBox.vue +4 -2
  55. package/src/components/MButtonGroup.vue +116 -35
  56. package/src/components/MColorPicker.vue +114 -210
  57. package/src/components/MColorPickerModal.vue +50 -251
  58. package/src/components/MDragHandle.vue +54 -0
  59. package/src/components/MList.vue +33 -3
  60. package/src/components/MListItem.vue +75 -5
  61. package/src/components/MScrollField.vue +302 -0
  62. package/src/components/MScrollable.vue +160 -5
  63. package/src/components/MSplitter.vue +33 -10
  64. package/src/components/MSwipeToDismiss.vue +247 -0
  65. package/src/components/MTransition.vue +149 -0
  66. package/src/components/_MColorPanel.vue +478 -0
  67. package/src/composables/_colorUtils.ts +107 -0
  68. package/src/composables/_themeColor.ts +33 -0
  69. package/src/composables/_useSpring.ts +89 -0
  70. package/src/composables/dynamicColor.ts +85 -0
  71. package/src/composables/useColorPalette.ts +171 -25
  72. package/src/composables/useFieldBg.ts +6 -0
  73. package/src/composables/useLocale.ts +22 -0
  74. package/src/index.ts +9 -1
  75. package/src/locales/de.ts +10 -0
  76. package/src/locales/es.ts +10 -0
  77. package/src/locales/fr.ts +10 -0
  78. package/src/locales/ja.ts +10 -0
  79. package/src/locales/ko.ts +10 -0
  80. package/src/locales/pt.ts +10 -0
  81. package/src/locales/zh.ts +10 -0
  82. package/src/plugin.ts +28 -7
  83. package/src/styles/palettes.css +546 -0
  84. package/src/styles/theme.css +77 -0
  85. package/dist/MTextField-pJU1vFaR.js.map +0 -1
  86. package/dist/useLocale-Drihe5G2.js.map +0 -1
@@ -0,0 +1,247 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Content that can be swiped left or right to dismiss it, uncovering a background behind it (a port of
4
+ * Compose's `SwipeToDismissBox`), typically a list item with "archive" and "delete" behind. Once swiped away it
5
+ * stays there until you reset it (`v-model` back to `'settled'`, or `reset()`), so you decide what happens:
6
+ * remove the item, or undo.
7
+ *
8
+ * @slot default The content that moves
9
+ * @slot background What the swipe uncovers. Receives `{ direction, target, progress }`: the side being
10
+ * swiped (`'start-to-end'`, `'end-to-start'` or `'settled'`), the state it would settle in if released now,
11
+ * and how far it has moved (0 to 1)
12
+ * @event update:modelValue Emitted when it settles in a new state — powers v-model
13
+ * @event dismiss Emitted with the direction when the content is swiped away
14
+ */
15
+ import { computed, getCurrentInstance, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
16
+ import { fastOutSlowIn } from '../composables/_useSpring'
17
+
18
+ export type SwipeToDismissValue = 'settled' | 'start-to-end' | 'end-to-start'
19
+
20
+ const props = withDefaults(defineProps<{
21
+ /** State (v-model): `'settled'`, or dismissed towards `'start-to-end'` / `'end-to-start'`. Set it to animate there. */
22
+ modelValue?: SwipeToDismissValue
23
+ /** Allow dismissing by swiping in the reading direction (right in LTR) */
24
+ startToEnd?: boolean
25
+ /** Allow dismissing by swiping against the reading direction (left in LTR) */
26
+ endToStart?: boolean
27
+ /** Turns the swipe gesture off (setting v-model still works) */
28
+ disabled?: boolean
29
+ /** How far, in px, the content has to move before releasing dismisses it (SwipeToDismissBoxDefaults: 56dp) */
30
+ threshold?: number
31
+ }>(), {
32
+ modelValue: 'settled',
33
+ startToEnd: true,
34
+ endToStart: true,
35
+ disabled: false,
36
+ threshold: 56,
37
+ })
38
+
39
+ const emit = defineEmits<{
40
+ 'update:modelValue': [SwipeToDismissValue]
41
+ dismiss: [Exclude<SwipeToDismissValue, 'settled'>]
42
+ }>()
43
+
44
+ const vnodeProps = getCurrentInstance()?.vnode.props ?? {}
45
+ const controlled = 'onUpdate:modelValue' in vnodeProps
46
+ const rootEl = ref<HTMLElement>()
47
+ const contentEl = ref<HTMLElement>()
48
+ // Offset in px along the reading direction (positive = towards the end)
49
+ const offset = ref(0)
50
+ const width = ref(0)
51
+ const settled = ref<SwipeToDismissValue>(props.modelValue)
52
+ let rtl = false
53
+
54
+ const anchorOf = (v: SwipeToDismissValue) => (v === 'start-to-end' ? width.value : v === 'end-to-start' ? -width.value : 0)
55
+
56
+ const direction = computed<SwipeToDismissValue>(() =>
57
+ offset.value > 0 ? 'start-to-end' : offset.value < 0 ? 'end-to-start' : 'settled')
58
+ // targetValue without a fling: the closest anchor once past the threshold from where the drag started
59
+ const target = computed<SwipeToDismissValue>(() => {
60
+ if (settled.value !== 'settled') return settled.value
61
+ if (offset.value >= props.threshold && props.startToEnd) return 'start-to-end'
62
+ if (offset.value <= -props.threshold && props.endToStart) return 'end-to-start'
63
+ return 'settled'
64
+ })
65
+ const progress = computed(() => (width.value ? Math.min(1, Math.abs(offset.value) / width.value) : 0))
66
+
67
+ function write() {
68
+ if (contentEl.value) contentEl.value.style.transform = offset.value ? `translateX(${rtl ? -offset.value : offset.value}px)` : ''
69
+ }
70
+ watch(offset, write)
71
+
72
+ // ── Settling: AnchoredDraggableDefaults.SnapAnimationSpec is tween() (300ms, FastOutSlowIn) ─────
73
+ let raf = 0
74
+ function animateTo(value: SwipeToDismissValue) {
75
+ cancelAnimationFrame(raf)
76
+ width.value = rootEl.value?.offsetWidth ?? width.value
77
+ const from = offset.value
78
+ const to = anchorOf(value)
79
+ const start = performance.now()
80
+ const step = (now: number) => {
81
+ const t = Math.min(1, (now - start) / 300)
82
+ offset.value = from + (to - from) * fastOutSlowIn(t)
83
+ if (t < 1) raf = requestAnimationFrame(step)
84
+ else settle(value)
85
+ }
86
+ if (from === to) settle(value)
87
+ else raf = requestAnimationFrame(step)
88
+ }
89
+
90
+ function settle(value: SwipeToDismissValue) {
91
+ raf = 0
92
+ offset.value = anchorOf(value)
93
+ if (settled.value === value) return
94
+ settled.value = value
95
+ emit('update:modelValue', value)
96
+ if (value !== 'settled') emit('dismiss', value)
97
+ // With v-model, a parent that sets it straight back (inside @dismiss, say) leaves the prop unchanged, so
98
+ // the watcher below never fires: compare once the parent has updated
99
+ if (controlled) nextTick(() => { if (props.modelValue !== settled.value) animateTo(props.modelValue) })
100
+ }
101
+
102
+ watch(() => props.modelValue, (v) => { if (v !== settled.value) animateTo(v) })
103
+
104
+ // ── Gesture ─────────────────────────────────────────────────────────────────────────────────────
105
+ // Touch keeps vertical scrolling (touch-action: pan-y); a drag only starts once the pointer has moved
106
+ // sideways more than it has vertically.
107
+ const SLOP = 8
108
+ // AnchoredDraggableMinFlingVelocity
109
+ const FLING_VELOCITY = 125
110
+ let pointerId: number | null = null
111
+ let startX = 0
112
+ let startY = 0
113
+ let startOffset = 0
114
+ let dragging = false
115
+ let samples: { t: number; x: number }[] = []
116
+ let suppressClick = false
117
+
118
+ function onPointerDown(e: PointerEvent) {
119
+ // Gestures are off while dismissed, as in SwipeToDismissBox
120
+ if (props.disabled || settled.value !== 'settled' || e.button !== 0) return
121
+ pointerId = e.pointerId
122
+ startX = e.clientX
123
+ startY = e.clientY
124
+ startOffset = offset.value
125
+ dragging = false
126
+ window.addEventListener('pointermove', onPointerMove)
127
+ window.addEventListener('pointerup', onPointerUp)
128
+ window.addEventListener('pointercancel', onPointerUp)
129
+ }
130
+
131
+ function along(e: PointerEvent) {
132
+ return rtl ? startX - e.clientX : e.clientX - startX
133
+ }
134
+
135
+ function onPointerMove(e: PointerEvent) {
136
+ if (e.pointerId !== pointerId) return
137
+ const dx = e.clientX - startX
138
+ const dy = e.clientY - startY
139
+ if (!dragging) {
140
+ if (Math.abs(dy) > SLOP && Math.abs(dy) > Math.abs(dx)) return release()
141
+ if (Math.abs(dx) <= SLOP) return
142
+ dragging = true
143
+ cancelAnimationFrame(raf)
144
+ raf = 0
145
+ width.value = rootEl.value?.offsetWidth ?? 0
146
+ document.body.style.userSelect = 'none'
147
+ }
148
+ const min = props.endToStart ? -width.value : 0
149
+ const max = props.startToEnd ? width.value : 0
150
+ offset.value = Math.min(max, Math.max(min, startOffset + along(e)))
151
+ samples.push({ t: e.timeStamp, x: rtl ? -e.clientX : e.clientX })
152
+ samples = samples.filter((s) => e.timeStamp - s.t < 100)
153
+ }
154
+
155
+ function onPointerUp(e: PointerEvent) {
156
+ if (e.pointerId !== pointerId) return
157
+ if (!dragging) return release()
158
+ const velocity = releaseVelocity(e.timeStamp)
159
+ suppressClick = true
160
+ release()
161
+ animateTo(computeTarget(velocity))
162
+ }
163
+
164
+ // Like Compose's VelocityTracker: the last 100ms of movement, and no velocity at all if the pointer had
165
+ // stopped (AssumePointerMoveStoppedMilliseconds = 40) before it was released
166
+ function releaseVelocity(upTime: number) {
167
+ const recent = samples.filter((s) => upTime - s.t < 100)
168
+ const first = recent[0]
169
+ const last = recent[recent.length - 1]
170
+ if (!first || !last || upTime - last.t > 40) return 0
171
+ const dt = (last.t - first.t) / 1000
172
+ return dt > 0 ? (last.x - first.x) / dt : 0
173
+ }
174
+
175
+ // AnchoredDraggable's computeTarget(): at rest the closest anchor; a fast fling goes to the next anchor in
176
+ // its direction; otherwise the next anchor once past the positional threshold from the previous one.
177
+ function computeTarget(velocity: number): SwipeToDismissValue {
178
+ const anchors: [SwipeToDismissValue, number][] = [['settled', 0]]
179
+ if (props.startToEnd) anchors.push(['start-to-end', width.value])
180
+ if (props.endToStart) anchors.push(['end-to-start', -width.value])
181
+ anchors.sort((a, b) => a[1] - b[1])
182
+ const x = offset.value
183
+ if (velocity === 0) {
184
+ return anchors.reduce((best, a) => (Math.abs(a[1] - x) < Math.abs(best[1] - x) ? a : best))[0]
185
+ }
186
+ const forward = velocity > 0
187
+ const below = [...anchors].reverse().find((a) => a[1] <= x) ?? anchors[0]!
188
+ const above = anchors.find((a) => a[1] >= x) ?? anchors[anchors.length - 1]!
189
+ if (Math.abs(velocity) >= FLING_VELOCITY) return (forward ? above : below)[0]
190
+ const from = forward ? below : above
191
+ return Math.abs(x - from[1]) >= props.threshold ? (forward ? above : below)[0] : from[0]
192
+ }
193
+
194
+ function release() {
195
+ pointerId = null
196
+ dragging = false
197
+ samples = []
198
+ document.body.style.userSelect = ''
199
+ window.removeEventListener('pointermove', onPointerMove)
200
+ window.removeEventListener('pointerup', onPointerUp)
201
+ window.removeEventListener('pointercancel', onPointerUp)
202
+ }
203
+
204
+ // A drag that ends over a button inside the content shouldn't also click it
205
+ function onClickCapture(e: MouseEvent) {
206
+ if (!suppressClick) return
207
+ suppressClick = false
208
+ e.stopPropagation()
209
+ e.preventDefault()
210
+ }
211
+
212
+ let resizeObserver: ResizeObserver | undefined
213
+ onMounted(() => {
214
+ rtl = rootEl.value ? getComputedStyle(rootEl.value).direction === 'rtl' : false
215
+ width.value = rootEl.value?.offsetWidth ?? 0
216
+ offset.value = anchorOf(settled.value)
217
+ write()
218
+ resizeObserver = new ResizeObserver(() => {
219
+ width.value = rootEl.value?.offsetWidth ?? 0
220
+ if (!dragging && !raf) offset.value = anchorOf(settled.value)
221
+ })
222
+ if (rootEl.value) resizeObserver.observe(rootEl.value)
223
+ })
224
+ onUnmounted(() => {
225
+ cancelAnimationFrame(raf)
226
+ release()
227
+ resizeObserver?.disconnect()
228
+ })
229
+
230
+ defineExpose({
231
+ /** Animates back to `'settled'` */
232
+ reset: () => animateTo('settled'),
233
+ /** Animates away in the given direction */
234
+ dismiss: (dir: Exclude<SwipeToDismissValue, 'settled'>) => animateTo(dir),
235
+ })
236
+ </script>
237
+
238
+ <template>
239
+ <div ref="rootEl" class="relative touch-pan-y overflow-hidden" @pointerdown="onPointerDown" @click.capture="onClickCapture">
240
+ <div class="absolute inset-0 flex">
241
+ <slot name="background" :direction="direction" :target="target" :progress="progress" />
242
+ </div>
243
+ <div ref="contentEl" class="relative">
244
+ <slot />
245
+ </div>
246
+ </div>
247
+ </template>
@@ -0,0 +1,149 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * M3's navigation transitions between two views (a port of Compose's `MaterialTransitions`): `shared-axis`
4
+ * slides the new view in from the side as the old one slides out (use `reverse` when going back), and
5
+ * `fade-through` fades the old view out and the new one in with a slight scale, for destinations without a
6
+ * spatial relationship such as navigation bar tabs. Wrap content that changes its `key` (or a `v-if`/`v-else`
7
+ * pair, or a `<RouterView>` slot's component); both views overlap while they animate.
8
+ *
9
+ * @slot default The current view; give it a `key` that changes with it
10
+ */
11
+ const props = withDefaults(defineProps<{
12
+ /** `shared-axis` (horizontal, for forward/back navigation) or `fade-through` (unrelated destinations) */
13
+ type?: 'shared-axis' | 'fade-through'
14
+ /** Shared axis: play the backward transition (the new view comes from the start side) */
15
+ reverse?: boolean
16
+ /** Animate the first view too */
17
+ appear?: boolean
18
+ }>(), { type: 'shared-axis', reverse: false, appear: false })
19
+
20
+ // slideIn/OutHorizontally and scaleIn/Out default to spring(stiffness = StiffnessMediumLow), no bounce
21
+ const STIFFNESS = 400
22
+ const DAMPING = 2 * Math.sqrt(STIFFNESS)
23
+ // HorizontalSharedAxis
24
+ const SLIDE_FRACTION = 0.2
25
+ const AXIS_FADE = { duration: 90, delay: 50 }
26
+ // FadeThrough
27
+ const FADE_OUT = 90
28
+ const FADE_IN = 210
29
+ const INITIAL_SCALE = 0.8
30
+ const TARGET_SCALE = 1.05
31
+
32
+ interface Motion {
33
+ /** Spring from `from` to `to` (an x offset in px, or a scale) */
34
+ from: number
35
+ to: number
36
+ apply: (value: number, el: HTMLElement) => void
37
+ /** Linear opacity tween */
38
+ fade: { from: number; to: number; duration: number; delay: number }
39
+ }
40
+
41
+ const running = new WeakMap<Element, () => void>()
42
+
43
+ function run(el: HTMLElement, m: Motion, done: () => void) {
44
+ running.get(el)?.()
45
+ let raf = 0
46
+ let value = m.from
47
+ let velocity = 0
48
+ const start = performance.now()
49
+ let last = start
50
+ const threshold = Math.abs(m.to - m.from) > 2 ? 0.5 : 0.001
51
+ const finish = () => {
52
+ cancelAnimationFrame(raf)
53
+ running.delete(el)
54
+ done()
55
+ }
56
+ running.set(el, finish)
57
+ const frame = (now: number) => {
58
+ let dt = Math.min((now - last) / 1000, 1 / 30)
59
+ last = now
60
+ while (dt > 0) {
61
+ const h = Math.min(dt, 0.004)
62
+ dt -= h
63
+ velocity += (-STIFFNESS * (value - m.to) - DAMPING * velocity) * h
64
+ value += velocity * h
65
+ }
66
+ const springDone = Math.abs(value - m.to) < threshold && Math.abs(velocity) < threshold * 10
67
+ if (springDone) value = m.to
68
+ m.apply(value, el)
69
+ const t = Math.min(1, Math.max(0, (now - start - m.fade.delay) / m.fade.duration))
70
+ el.style.opacity = String(m.fade.from + (m.fade.to - m.fade.from) * t)
71
+ if (springDone && t >= 1) finish()
72
+ else raf = requestAnimationFrame(frame)
73
+ }
74
+ m.apply(m.from, el)
75
+ el.style.opacity = String(m.fade.from)
76
+ raf = requestAnimationFrame(frame)
77
+ }
78
+
79
+ const translate = (x: number, el: HTMLElement) => { el.style.transform = x ? `translateX(${x}px)` : '' }
80
+ const scale = (s: number, el: HTMLElement) => { el.style.transform = s !== 1 ? `scale(${s})` : '' }
81
+
82
+ function clear(el: HTMLElement) {
83
+ el.style.transform = ''
84
+ el.style.opacity = ''
85
+ el.style.zIndex = ''
86
+ }
87
+
88
+ function onEnter(element: Element, done: () => void) {
89
+ const el = element as HTMLElement
90
+ const finish = () => { clear(el); done() }
91
+ if (props.type === 'fade-through') {
92
+ run(el, { from: INITIAL_SCALE, to: 1, apply: scale, fade: { from: 0, to: 1, duration: FADE_IN, delay: FADE_OUT } }, finish)
93
+ return
94
+ }
95
+ const w = el.offsetWidth
96
+ // Backward: the entering view sits under the leaving one (targetContentZIndex = -10)
97
+ if (props.reverse) el.style.zIndex = '-1'
98
+ run(el, {
99
+ from: (props.reverse ? -1 : 1) * w * SLIDE_FRACTION,
100
+ to: 0,
101
+ apply: translate,
102
+ fade: { from: 0, to: 1, ...AXIS_FADE },
103
+ }, finish)
104
+ }
105
+
106
+ function onLeave(element: Element, done: () => void) {
107
+ const el = element as HTMLElement
108
+ const finish = () => { clear(el); done() }
109
+ if (props.type === 'fade-through') {
110
+ run(el, { from: 1, to: TARGET_SCALE, apply: scale, fade: { from: 1, to: 0, duration: FADE_OUT, delay: 0 } }, finish)
111
+ return
112
+ }
113
+ const w = el.offsetWidth
114
+ run(el, {
115
+ from: 0,
116
+ to: (props.reverse ? 1 : -1) * w * SLIDE_FRACTION,
117
+ apply: translate,
118
+ fade: { from: 1, to: 0, ...AXIS_FADE },
119
+ }, finish)
120
+ }
121
+
122
+ function onCancel(el: Element) {
123
+ running.get(el)?.()
124
+ }
125
+ </script>
126
+
127
+ <template>
128
+ <!-- Clipped, as AnimatedContent clips by default (SizeTransform(clip = true)) -->
129
+ <div class="m3-transition isolate grid overflow-hidden">
130
+ <Transition
131
+ :css="false"
132
+ :appear="appear"
133
+ @enter="onEnter"
134
+ @leave="onLeave"
135
+ @enter-cancelled="onCancel"
136
+ @leave-cancelled="onCancel"
137
+ >
138
+ <slot />
139
+ </Transition>
140
+ </div>
141
+ </template>
142
+
143
+ <style>
144
+ /* Both views share one grid cell while they animate */
145
+ .m3-transition > * {
146
+ grid-area: 1 / 1;
147
+ min-width: 0;
148
+ }
149
+ </style>