@m3ui-vue/m3ui-vue 1.1.3 → 1.1.4

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 (78) hide show
  1. package/README.md +1 -1
  2. package/dist/API.md +70 -8
  3. package/dist/{MMenuItem-BckcR0nW.js → MMenuItem-BfjqwcYP.js} +84 -63
  4. package/dist/MMenuItem-BfjqwcYP.js.map +1 -0
  5. package/dist/MSlider-JflHJ70q.js +1525 -0
  6. package/dist/MSlider-JflHJ70q.js.map +1 -0
  7. package/dist/{MTextField-FCPOTB0o.js → MTextField-NaIB6RbM.js} +5 -4
  8. package/dist/MTextField-NaIB6RbM.js.map +1 -0
  9. package/dist/api.json +1 -1
  10. package/dist/code-editor.js +1 -1
  11. package/dist/components/MAnimatedNumber.vue.d.ts +3 -1
  12. package/dist/components/MButton.vue.d.ts +2 -0
  13. package/dist/components/MContainerTransform.vue.d.ts +59 -0
  14. package/dist/components/MDialog.vue.d.ts +8 -5
  15. package/dist/components/MFlex.vue.d.ts +1 -1
  16. package/dist/components/MIconButton.vue.d.ts +2 -0
  17. package/dist/components/MListItem.vue.d.ts +5 -5
  18. package/dist/components/MNavigationRail.vue.d.ts +1 -1
  19. package/dist/components/MSplitter.vue.d.ts +1 -1
  20. package/dist/components/MStack.vue.d.ts +1 -1
  21. package/dist/components/MToolbar.vue.d.ts +32 -5
  22. package/dist/components/_MToolbarFab.vue.d.ts +16 -0
  23. package/dist/composables/_containerTransform.d.ts +48 -0
  24. package/dist/composables/useContainerTransform.d.ts +30 -0
  25. package/dist/composables/useLocale.d.ts +3 -0
  26. package/dist/index.d.ts +3 -0
  27. package/dist/locales/index.js +14 -0
  28. package/dist/locales/index.js.map +1 -1
  29. package/dist/m3ui-vue.css +1 -1
  30. package/dist/m3ui.js +4217 -3713
  31. package/dist/m3ui.js.map +1 -1
  32. package/dist/map.js +38 -38
  33. package/dist/rich-text-editor.js +10 -10
  34. package/dist/styles.css +1 -1
  35. package/dist/{useLocale-SfQHnJfp.js → useLocale-CXibICQA.js} +3 -1
  36. package/dist/useLocale-CXibICQA.js.map +1 -0
  37. package/package.json +2 -1
  38. package/src/__tests__/MAnimatedNumber.test.ts +24 -2
  39. package/src/__tests__/MContainerTransform.test.ts +279 -0
  40. package/src/__tests__/MDialog.test.ts +27 -0
  41. package/src/__tests__/MListItemExpandable.test.ts +48 -0
  42. package/src/__tests__/MListItemShape.test.ts +9 -6
  43. package/src/__tests__/MListKeyboard.test.ts +90 -0
  44. package/src/__tests__/MMapTools.test.ts +2 -1
  45. package/src/__tests__/MNavigationRail.test.ts +33 -0
  46. package/src/__tests__/MSlider.test.ts +161 -0
  47. package/src/__tests__/MToolbar.test.ts +157 -0
  48. package/src/__tests__/attrsFallthrough.test.ts +6 -0
  49. package/src/__tests__/hydration.test.ts +25 -0
  50. package/src/components/MAnimatedNumber.vue +32 -6
  51. package/src/components/MButton.vue +4 -2
  52. package/src/components/MContainerTransform.vue +321 -0
  53. package/src/components/MDialog.vue +31 -6
  54. package/src/components/MFab.vue +10 -5
  55. package/src/components/MIconButton.vue +3 -1
  56. package/src/components/MList.vue +26 -2
  57. package/src/components/MListItem.vue +138 -84
  58. package/src/components/MNavigationRail.vue +40 -10
  59. package/src/components/MSlider.vue +161 -42
  60. package/src/components/MToolbar.vue +256 -15
  61. package/src/components/_MToolbarFab.vue +14 -0
  62. package/src/composables/_containerTransform.ts +179 -0
  63. package/src/composables/_useEscapeStack.ts +2 -0
  64. package/src/composables/useContainerTransform.ts +59 -0
  65. package/src/composables/useLocale.ts +5 -0
  66. package/src/index.ts +3 -0
  67. package/src/locales/de.ts +2 -0
  68. package/src/locales/es.ts +2 -0
  69. package/src/locales/fr.ts +2 -0
  70. package/src/locales/ja.ts +2 -0
  71. package/src/locales/ko.ts +2 -0
  72. package/src/locales/pt.ts +2 -0
  73. package/src/locales/zh.ts +2 -0
  74. package/dist/MMenuItem-BckcR0nW.js.map +0 -1
  75. package/dist/MSlider-LyH_gy8D.js +0 -1411
  76. package/dist/MSlider-LyH_gy8D.js.map +0 -1
  77. package/dist/MTextField-FCPOTB0o.js.map +0 -1
  78. package/dist/useLocale-SfQHnJfp.js.map +0 -1
@@ -1,12 +1,20 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
3
  * A container for grouping icon buttons and actions — docked to an edge or floating as a rounded pill,
4
- * horizontal or vertical, with an optional paired FAB.
4
+ * horizontal or vertical, with an optional paired FAB. A floating toolbar collapses (`v-model:expanded`): with a
5
+ * FAB, into the FAB, which grows to 80px; without one, to its center (the default slot), closing `#leading` and
6
+ * `#trailing`. `collapseOnScroll` does it as the content scrolls.
5
7
  *
6
- * @slot fab A FAB rendered alongside the toolbar body, positioned by fabPosition
7
- * @slot default Toolbar items — typically MIconButton/MButton, separated with MDivider
8
+ * @slot fab A FAB rendered alongside the toolbar body, positioned by fabPosition. The toolbar sizes an MFab in it
9
+ * @slot default Toolbar items — typically MIconButton/MButton, separated with MDivider. With #leading or #trailing,
10
+ * the center, which stays when a floating toolbar without a FAB collapses
11
+ * @slot leading Items before the center, hidden when a floating toolbar without a FAB collapses
12
+ * @slot trailing Items after the center, hidden when a floating toolbar without a FAB collapses
13
+ * @event update:expanded The toolbar collapsed or expanded on scroll (`collapseOnScroll`)
8
14
  */
9
- import { computed } from 'vue'
15
+ import { computed, onMounted, onUnmounted, onUpdated, ref, useSlots, watch } from 'vue'
16
+ import { motionSprings } from '../composables/motion'
17
+ import MToolbarFab from './_MToolbarFab.vue'
10
18
 
11
19
  const props = withDefaults(defineProps<{
12
20
  /** Docked (edge-attached, full width/height, bordered) or floating (rounded pill with shadow) */
@@ -19,14 +27,25 @@ const props = withDefaults(defineProps<{
19
27
  fabPosition?: 'start' | 'end' | 'none'
20
28
  /** Item alignment along the main axis */
21
29
  align?: 'start' | 'center' | 'end' | 'between'
30
+ /**
31
+ * Collapse a floating toolbar as the content scrolls down and expand it as it scrolls back up (40px each way):
32
+ * `true` follows the window, or pass the scrolling element (or a CSS selector for it). Off by default
33
+ */
34
+ collapseOnScroll?: boolean | string | HTMLElement | null
22
35
  }>(), {
23
36
  variant: 'docked',
24
37
  orientation: 'horizontal',
25
38
  color: 'surface',
26
39
  fabPosition: 'none',
27
40
  align: 'center',
41
+ collapseOnScroll: false,
28
42
  })
29
43
 
44
+ /** A floating toolbar's state: collapsed, it shows only the FAB, or only its center without one. Default `true` */
45
+ const expanded = defineModel<boolean>('expanded', { default: true })
46
+
47
+ const slots = useSlots()
48
+
30
49
  const colorClasses = computed(() => {
31
50
  const map: Record<string, string> = {
32
51
  surface: 'bg-surface-container text-on-surface',
@@ -48,6 +67,167 @@ const alignClass = computed(() => {
48
67
  })
49
68
 
50
69
  const isVertical = computed(() => props.orientation === 'vertical')
70
+ const floating = computed(() => props.variant === 'floating')
71
+ const hasFab = computed(() => !!slots.fab && props.fabPosition !== 'none')
72
+ // A floating toolbar with a FAB collapses into it; one without, to its center
73
+ const fabMode = computed(() => floating.value && hasFab.value)
74
+ const sections = computed(() => !!(slots.leading || slots.trailing))
75
+ const collapsible = computed(() => floating.value && (hasFab.value || sections.value))
76
+ // The standard floating toolbar's FAB is PrimaryContainer, the vibrant one's TertiaryContainer
77
+ const fabColor = computed(() => (props.color === 'surface' ? 'primary' : 'tertiary'))
78
+ // What a collapsed toolbar hides is out of reach for keyboard and assistive technology
79
+ const hidden = computed(() => collapsible.value && !expanded.value)
80
+
81
+ // ── Collapse: one spring (FloatingToolbarDefaults.animationSpec = Expressive FastSpatial) for the progress, in a
82
+ // rAF loop that writes sizes (layout, so not a Web Animation). Like Compose: with a FAB, the toolbar keeps its
83
+ // footprint and clips toward the FAB, which grows from 56 to 80dp; without, #leading/#trailing shrink toward the
84
+ // center. Settled expanded, every size is left to the layout (style cleared), so content changes reflow normally.
85
+ const FAB_MIN = 56
86
+ const FAB_MAX = 80
87
+ const region = ref<HTMLElement>()
88
+ const body = ref<HTMLElement>()
89
+ const inner = ref<HTMLElement>()
90
+ const fabBox = ref<HTMLElement>()
91
+ const leadClip = ref<HTMLElement>()
92
+ const leadInner = ref<HTMLElement>()
93
+ const trailClip = ref<HTMLElement>()
94
+ const trailInner = ref<HTMLElement>()
95
+
96
+ let progress = expanded.value ? 1 : 0
97
+ let velocity = 0
98
+ let raf = 0
99
+ let last = 0
100
+ let full = 0
101
+ let lead = 0
102
+ let trail = 0
103
+ const reduceMotion = () => typeof matchMedia !== 'undefined' && matchMedia('(prefers-reduced-motion: reduce)').matches
104
+ const size = () => (isVertical.value ? 'height' : 'width')
105
+ const measure = (el?: HTMLElement) => (el ? (isVertical.value ? el.offsetHeight : el.offsetWidth) : 0)
106
+
107
+ function write() {
108
+ const settled = !raf
109
+ const p = Math.max(0, progress)
110
+ const axis = size()
111
+ const open = settled && progress === 1
112
+ if (fabMode.value) {
113
+ if (region.value) region.value.style[axis] = open ? '' : `${full}px`
114
+ if (body.value) body.value.style[axis] = open ? '' : `${full * p}px`
115
+ const fab = Math.max(0, FAB_MIN + (FAB_MAX - FAB_MIN) * (1 - progress))
116
+ if (fabBox.value) {
117
+ fabBox.value.style.width = `${fab}px`
118
+ fabBox.value.style.height = `${fab}px`
119
+ }
120
+ } else if (floating.value) {
121
+ if (leadClip.value) leadClip.value.style[axis] = open ? '' : `${lead * p}px`
122
+ if (trailClip.value) trailClip.value.style[axis] = open ? '' : `${trail * p}px`
123
+ }
124
+ }
125
+
126
+ function frame(now: number) {
127
+ let dt = Math.min((now - last) / 1000, 1 / 30)
128
+ last = now
129
+ const { stiffness: k, dampingRatio } = motionSprings.expressive.fastSpatial
130
+ const c = 2 * dampingRatio * Math.sqrt(k)
131
+ const target = expanded.value ? 1 : 0
132
+ while (dt > 0) {
133
+ const h = Math.min(dt, 0.004)
134
+ dt -= h
135
+ velocity += (-k * (progress - target) - c * velocity) * h
136
+ progress += velocity * h
137
+ }
138
+ if (Math.abs(progress - target) < 0.001 && Math.abs(velocity) < 0.01) {
139
+ progress = target
140
+ velocity = 0
141
+ raf = 0
142
+ } else raf = requestAnimationFrame(frame)
143
+ write()
144
+ }
145
+
146
+ function animate() {
147
+ // Natural sizes, read while every size is still the layout's (expanded) or from the unclipped inner content
148
+ full = measure(inner.value)
149
+ lead = measure(leadInner.value)
150
+ trail = measure(trailInner.value)
151
+ if (!collapsible.value || reduceMotion()) {
152
+ cancelAnimationFrame(raf)
153
+ raf = 0
154
+ progress = expanded.value ? 1 : 0
155
+ velocity = 0
156
+ write()
157
+ return
158
+ }
159
+ if (!raf) {
160
+ last = performance.now()
161
+ raf = requestAnimationFrame(frame)
162
+ }
163
+ }
164
+
165
+ watch(expanded, animate)
166
+ onMounted(() => {
167
+ full = measure(inner.value)
168
+ lead = measure(leadInner.value)
169
+ trail = measure(trailInner.value)
170
+ write()
171
+ })
172
+ // A re-render (new items) may change the natural sizes: a collapsed toolbar keeps the new ones
173
+ onUpdated(() => {
174
+ if (raf || progress === 1) return
175
+ full = measure(inner.value)
176
+ lead = measure(leadInner.value)
177
+ trail = measure(trailInner.value)
178
+ write()
179
+ })
180
+
181
+ // ── collapseOnScroll: Compose's floatingToolbarVerticalNestedScroll. The content's offset (minus the scroll
182
+ // position) is compared with a threshold 40dp behind the last switch, so small back-and-forth scrolls don't flicker.
183
+ const SCROLL_THRESHOLD = 40
184
+ let scroller: HTMLElement | Window | null = null
185
+ let lastTop = 0
186
+ let threshold = 0
187
+ const scrollTop = () => (!scroller ? 0 : scroller === window ? window.scrollY : (scroller as HTMLElement).scrollTop)
188
+ const resetThreshold = () => (threshold = -lastTop + (expanded.value ? -SCROLL_THRESHOLD : SCROLL_THRESHOLD))
189
+
190
+ function onScroll() {
191
+ const top = scrollTop()
192
+ const delta = lastTop - top
193
+ lastTop = top
194
+ const offset = -top
195
+ if (delta < 0 && offset <= threshold) {
196
+ threshold = offset + SCROLL_THRESHOLD
197
+ expanded.value = false
198
+ } else if (delta > 0 && offset >= threshold) {
199
+ threshold = offset - SCROLL_THRESHOLD
200
+ expanded.value = true
201
+ }
202
+ }
203
+
204
+ function attachScroll() {
205
+ scroller?.removeEventListener('scroll', onScroll)
206
+ scroller = null
207
+ const target = props.collapseOnScroll
208
+ if (!target || typeof window === 'undefined') return
209
+ scroller = target === true ? window : typeof target === 'string' ? document.querySelector<HTMLElement>(target) : target
210
+ if (!scroller) return
211
+ scroller.addEventListener('scroll', onScroll, { passive: true })
212
+ lastTop = scrollTop()
213
+ resetThreshold()
214
+ }
215
+
216
+ onMounted(() => {
217
+ attachScroll()
218
+ watch(() => props.collapseOnScroll, attachScroll)
219
+ // Expanded or collapsed some other way (the FAB): scrolling switches it again from here
220
+ watch(expanded, resetThreshold)
221
+ })
222
+ onUnmounted(() => {
223
+ cancelAnimationFrame(raf)
224
+ scroller?.removeEventListener('scroll', onScroll)
225
+ })
226
+
227
+ // Starting sizes for the server render and the first paint, set once (the loop writes them from then on)
228
+ const startCollapsed = !expanded.value
229
+ const fabStart = { width: `${startCollapsed ? FAB_MAX : FAB_MIN}px`, height: `${startCollapsed ? FAB_MAX : FAB_MIN}px` }
230
+ const clipStart = startCollapsed ? { [props.orientation === 'vertical' ? 'height' : 'width']: '0px' } : {}
51
231
  </script>
52
232
 
53
233
  <template>
@@ -55,35 +235,96 @@ const isVertical = computed(() => props.orientation === 'vertical')
55
235
  class="flex items-center gap-2"
56
236
  :class="[
57
237
  isVertical ? 'flex-col' : 'flex-row',
58
- fabPosition === 'start' && !isVertical ? 'flex-row' : '',
59
- fabPosition === 'start' && isVertical ? 'flex-col' : '',
238
+ fabMode ? (isVertical ? 'min-w-20' : 'min-h-20') : '',
60
239
  ]"
61
240
  >
62
241
  <!-- FAB start -->
63
- <div v-if="$slots.fab && fabPosition === 'start'" class="shrink-0">
64
- <slot name="fab" />
242
+ <div v-if="hasFab && fabPosition === 'start'" class="relative size-14 shrink-0">
243
+ <div ref="fabBox" class="absolute" :class="isVertical ? 'top-0 left-1/2 -translate-x-1/2' : 'left-0 top-1/2 -translate-y-1/2'" :style="fabStart">
244
+ <MToolbarFab :color="fabColor"><slot name="fab" /></MToolbarFab>
245
+ </div>
246
+ </div>
247
+
248
+ <!-- Floating with a FAB: the pill clips toward the FAB inside a region that keeps the toolbar's footprint -->
249
+ <div
250
+ v-if="fabMode"
251
+ ref="region"
252
+ class="flex shrink-0"
253
+ :class="[isVertical ? 'flex-col' : 'flex-row', fabPosition === 'end' ? 'justify-end' : 'justify-start']"
254
+ >
255
+ <div
256
+ ref="body"
257
+ class="overflow-hidden rounded-full shadow-elevation-1"
258
+ :class="[isVertical ? 'w-16' : 'h-16', colorClasses]"
259
+ :style="clipStart"
260
+ :inert="hidden || undefined"
261
+ >
262
+ <div
263
+ ref="inner"
264
+ class="flex shrink-0 items-center gap-1"
265
+ :class="[isVertical ? 'h-max w-16 flex-col py-2' : 'h-16 w-max flex-row px-2', alignClass]"
266
+ >
267
+ <slot name="leading" />
268
+ <slot />
269
+ <slot name="trailing" />
270
+ </div>
271
+ </div>
65
272
  </div>
66
273
 
67
274
  <!-- Toolbar body -->
68
275
  <div
69
- class="flex items-center gap-1"
276
+ v-else
277
+ class="flex items-center"
70
278
  :class="[
279
+ sections ? '' : 'gap-1',
71
280
  isVertical ? 'flex-col w-16' : 'flex-row h-16',
72
- isVertical ? (variant === 'floating' ? 'py-2' : 'py-4') : (variant === 'floating' ? 'px-2' : 'px-4'),
281
+ isVertical ? (floating ? 'py-2' : 'py-4') : (floating ? 'px-2' : 'px-4'),
73
282
  alignClass,
74
283
  colorClasses,
75
- variant === 'floating' ? 'rounded-full' : '',
76
- variant === 'floating' && fabPosition !== 'none' ? 'shadow-elevation-1' : '',
284
+ floating ? 'rounded-full' : '',
285
+ floating && hasFab ? 'shadow-elevation-1' : '',
77
286
  variant === 'docked' ? 'w-full border-t border-outline-variant' : '',
78
287
  variant === 'docked' && isVertical ? 'h-full w-16 border-t-0 border-r border-outline-variant' : '',
79
288
  ]"
80
289
  >
81
- <slot />
290
+ <template v-if="sections">
291
+ <!-- Each side closes toward the center: its content is anchored to the center-facing edge -->
292
+ <div
293
+ v-if="$slots.leading"
294
+ ref="leadClip"
295
+ class="flex shrink-0 overflow-hidden"
296
+ :class="isVertical ? 'flex-col justify-end' : 'flex-row justify-end'"
297
+ :style="floating ? clipStart : undefined"
298
+ :inert="hidden || undefined"
299
+ >
300
+ <div ref="leadInner" class="flex shrink-0 items-center gap-1" :class="isVertical ? 'flex-col pb-1' : 'flex-row pe-1'">
301
+ <slot name="leading" />
302
+ </div>
303
+ </div>
304
+ <div class="flex items-center gap-1" :class="isVertical ? 'flex-col' : 'flex-row'">
305
+ <slot />
306
+ </div>
307
+ <div
308
+ v-if="$slots.trailing"
309
+ ref="trailClip"
310
+ class="flex shrink-0 overflow-hidden"
311
+ :class="isVertical ? 'flex-col justify-start' : 'flex-row justify-start'"
312
+ :style="floating ? clipStart : undefined"
313
+ :inert="hidden || undefined"
314
+ >
315
+ <div ref="trailInner" class="flex shrink-0 items-center gap-1" :class="isVertical ? 'flex-col pt-1' : 'flex-row ps-1'">
316
+ <slot name="trailing" />
317
+ </div>
318
+ </div>
319
+ </template>
320
+ <slot v-else />
82
321
  </div>
83
322
 
84
323
  <!-- FAB end -->
85
- <div v-if="$slots.fab && fabPosition === 'end'" class="shrink-0">
86
- <slot name="fab" />
324
+ <div v-if="hasFab && fabPosition === 'end'" class="relative size-14 shrink-0">
325
+ <div ref="fabBox" class="absolute" :class="isVertical ? 'bottom-0 left-1/2 -translate-x-1/2' : 'right-0 top-1/2 -translate-y-1/2'" :style="fabStart">
326
+ <MToolbarFab :color="fabColor"><slot name="fab" /></MToolbarFab>
327
+ </div>
87
328
  </div>
88
329
  </div>
89
330
  </template>
@@ -0,0 +1,14 @@
1
+ <script setup lang="ts">
2
+ // The box MToolbar's #fab slot renders in: tells an MFab inside that the toolbar sizes it (it fills this box, which
3
+ // grows from 56 to 80px as the toolbar collapses) and which color to default to. A component of its own so only
4
+ // the slot's FAB sees it, not one inside the toolbar's items.
5
+ import { provide } from 'vue'
6
+
7
+ const props = defineProps<{ color: 'primary' | 'tertiary' }>()
8
+
9
+ provide('m-toolbar-fab', props)
10
+ </script>
11
+
12
+ <template>
13
+ <slot />
14
+ </template>
@@ -0,0 +1,179 @@
1
+ /*
2
+ * Internal (not exported from index.ts): Material's container transform, the engine behind MContainerTransform.
3
+ *
4
+ * A port of Material Components for Android's MaterialContainerTransform (transition/MaterialContainerTransform.java,
5
+ * FitModeEvaluators, FadeModeEvaluators, MaskEvaluator, TransitionUtils), with its defaults: linear path between the
6
+ * two boxes' top-centers, FIT_MODE_AUTO, FADE_MODE_IN, the non-arc progress thresholds, a 32% black scrim, 500ms
7
+ * (motionDurationLong2) to open and 400ms (motionDurationMedium4) to close on the emphasized curve. Both views keep
8
+ * their own layout and are scaled uniformly to the container's current width (or height), so nothing stretches;
9
+ * a rounded clip, the union of both masked boxes, morphs between their shapes. Android draws snapshots on a canvas;
10
+ * here the source is a clone, the destination the real panel, and every frame is computed up front and played as
11
+ * Web Animations (see _springAnimation.ts) so the compositor runs the transforms and opacities.
12
+ */
13
+ import { SAMPLE_MS } from './_springAnimation'
14
+
15
+ export interface Box { left: number; top: number; width: number; height: number }
16
+
17
+ interface Thresholds { fade: [number, number]; scale: [number, number]; scaleMask: [number, number]; shapeMask: [number, number] }
18
+ const ENTER: Thresholds = { fade: [0, 0.25], scale: [0, 1], scaleMask: [0, 1], shapeMask: [0, 0.75] }
19
+ const RETURN: Thresholds = { fade: [0.6, 0.9], scale: [0, 1], scaleMask: [0, 0.9], shapeMask: [0.3, 0.9] }
20
+
21
+ export const OPEN_MS = 500
22
+ export const CLOSE_MS = 400
23
+ export const SCRIM_OPACITY = 0x52 / 255
24
+
25
+ // Android's emphasized interpolator (motionEasingEmphasizedInterpolator): the path
26
+ // M 0,0 C 0.05,0 0.133333,0.06 0.166666,0.4 C 0.208333,0.82 0.25,1 1,1, not a single cubic
27
+ function segment(x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x: number) {
28
+ let lo = 0
29
+ let hi = 1
30
+ for (let i = 0; i < 30; i++) {
31
+ const u = (lo + hi) / 2
32
+ const bx = (1 - u) ** 3 * x0 + 3 * (1 - u) ** 2 * u * x1 + 3 * (1 - u) * u ** 2 * x2 + u ** 3 * x3
33
+ if (bx < x) lo = u
34
+ else hi = u
35
+ }
36
+ const u = (lo + hi) / 2
37
+ return (1 - u) ** 3 * y0 + 3 * (1 - u) ** 2 * u * y1 + 3 * (1 - u) * u ** 2 * y2 + u ** 3 * y3
38
+ }
39
+ export function emphasized(t: number) {
40
+ if (t <= 0) return 0
41
+ if (t >= 1) return 1
42
+ return t < 0.166666
43
+ ? segment(0, 0, 0.05, 0, 0.133333, 0.06, 0.166666, 0.4, t)
44
+ : segment(0.166666, 0.4, 0.208333, 0.82, 0.25, 1, 1, 1, t)
45
+ }
46
+
47
+ const lerp = (a: number, b: number, f: number) => a + f * (b - a)
48
+ // TransitionUtils.lerp with start/end fractions (clamped outside them, like Android unless overshoot is allowed)
49
+ function lerpRange(a: number, b: number, [from, to]: [number, number], f: number) {
50
+ if (f < from) return a
51
+ if (f > to) return b
52
+ return lerp(a, b, (f - from) / (to - from))
53
+ }
54
+
55
+ /** Where a view sits: translate from its own resting box, uniform scale from its top-left corner, opacity */
56
+ export interface ViewState { x: number; y: number; scale: number; opacity: number }
57
+ export interface Frame {
58
+ /** The container's clip, in the same coordinates as the boxes, and its corner radius */
59
+ clip: Box
60
+ radius: number
61
+ start: ViewState
62
+ end: ViewState
63
+ scrim: number
64
+ }
65
+
66
+ /**
67
+ * The transition's state at progress `p` (already eased), from the `start` box to the `end` one. `entering` is the
68
+ * direction (Android's TRANSITION_DIRECTION_AUTO: the bigger box is the detail), which picks the thresholds and the
69
+ * fade: entering, the end view fades in on top of the start one; returning, the start one fades out on top.
70
+ */
71
+ export function containerFrame(p: number, start: Box, end: Box, startRadius: number, endRadius: number, entering: boolean): Frame {
72
+ const th = entering ? ENTER : RETURN
73
+
74
+ // FitModeEvaluators: fit to the width unless the end box, scaled to the start's width, ends up shorter
75
+ const fitWidth = entering
76
+ ? (end.height * start.width) / end.width >= start.height
77
+ : (start.height * end.width) / start.width >= end.height
78
+ let startScale: number
79
+ let endScale: number
80
+ if (fitWidth) {
81
+ const width = lerpRange(start.width, end.width, th.scale, p)
82
+ startScale = width / start.width
83
+ endScale = width / end.width
84
+ } else {
85
+ const height = lerpRange(start.height, end.height, th.scale, p)
86
+ startScale = height / start.height
87
+ endScale = height / end.height
88
+ }
89
+ const sw = start.width * startScale
90
+ const sh = start.height * startScale
91
+ const ew = end.width * endScale
92
+ const eh = end.height * endScale
93
+
94
+ // The linear motion path between the top-centers
95
+ const mx = lerp(start.left + start.width / 2, end.left + end.width / 2, p)
96
+ const my = lerp(start.top, end.top, p)
97
+ const startBox = { left: mx - sw / 2, top: my, width: sw, height: sh }
98
+ const endBox = { left: mx - ew / 2, top: my, width: ew, height: eh }
99
+
100
+ // Mask the taller (or wider) of the two down toward the other over the scale-mask range
101
+ const startMasked = { ...startBox }
102
+ const endMasked = { ...endBox }
103
+ const maskProgress = lerpRange(0, 1, th.scaleMask, p)
104
+ if (fitWidth) {
105
+ const maskStart = sh > eh
106
+ const target = maskStart ? startMasked : endMasked
107
+ target.height -= Math.abs(eh - sh) * (maskStart ? maskProgress : 1 - maskProgress)
108
+ } else {
109
+ const maskStart = sw > ew
110
+ const target = maskStart ? startMasked : endMasked
111
+ const diff = Math.abs(ew - sw) * (maskStart ? maskProgress : 1 - maskProgress)
112
+ target.left += diff / 2
113
+ target.width -= diff
114
+ }
115
+ const left = Math.min(startMasked.left, endMasked.left)
116
+ const top = Math.min(startMasked.top, endMasked.top)
117
+ const right = Math.max(startMasked.left + startMasked.width, endMasked.left + endMasked.width)
118
+ const bottom = Math.max(startMasked.top + startMasked.height, endMasked.top + endMasked.height)
119
+
120
+ const fade = lerpRange(0, 1, th.fade, p)
121
+ return {
122
+ clip: { left, top, width: right - left, height: bottom - top },
123
+ radius: lerpRange(startRadius, endRadius, th.shapeMask, p),
124
+ start: { x: startBox.left - start.left, y: startBox.top - start.top, scale: startScale, opacity: entering ? 1 : 1 - fade },
125
+ end: { x: endBox.left - end.left, y: endBox.top - end.top, scale: endScale, opacity: entering ? fade : 1 },
126
+ scrim: (entering ? p : 1 - p) * SCRIM_OPACITY,
127
+ }
128
+ }
129
+
130
+ /** Every frame of a transition, one per 60Hz frame of `duration` ms, on the emphasized curve */
131
+ export function containerFrames(start: Box, end: Box, startRadius: number, endRadius: number, entering: boolean, duration: number): Frame[] {
132
+ const n = Math.max(1, Math.round(duration / SAMPLE_MS))
133
+ return Array.from({ length: n + 1 }, (_, i) => containerFrame(emphasized(i / n), start, end, startRadius, endRadius, entering))
134
+ }
135
+
136
+ const view = (s: ViewState): Keyframe => ({ transform: `translate(${s.x}px, ${s.y}px) scale(${s.scale})`, opacity: s.opacity })
137
+
138
+ /**
139
+ * Plays `frames` on the clip layer (positioned at the viewport's origin, `vw` × `vh`), the two views and the scrim.
140
+ * Returns the running animations (empty without the Web Animations API). `fill: 'forwards'` holds the last frame
141
+ * (a closing run, until the layer is removed).
142
+ */
143
+ export function playContainerFrames(
144
+ frames: Frame[],
145
+ els: { clip: HTMLElement; start?: HTMLElement | null; end: HTMLElement; scrim?: HTMLElement | null },
146
+ vw: number,
147
+ vh: number,
148
+ fill?: FillMode,
149
+ ): Animation[] {
150
+ if (typeof els.end.animate !== 'function') return []
151
+ const duration = (frames.length - 1) * SAMPLE_MS
152
+ const options: KeyframeAnimationOptions = { duration, easing: 'linear', fill }
153
+ const clip = (f: Frame) => {
154
+ const { left, top, width, height } = f.clip
155
+ return `inset(${top}px ${vw - left - width}px ${vh - top - height}px ${left}px round ${f.radius}px)`
156
+ }
157
+ const anims = [
158
+ els.clip.animate(frames.map((f) => ({ clipPath: clip(f) })), options),
159
+ els.end.animate(frames.map((f) => view(f.end)), options),
160
+ ]
161
+ if (els.start) anims.push(els.start.animate(frames.map((f) => view(f.start)), options))
162
+ if (els.scrim) anims.push(els.scrim.animate(frames.map((f) => ({ opacity: f.scrim })), options))
163
+ for (const a of anims) a.finished.catch(() => {})
164
+ return anims
165
+ }
166
+
167
+ /** A resting box in viewport coordinates */
168
+ export function boxOf(el: Element): Box {
169
+ const r = el.getBoundingClientRect()
170
+ return { left: r.left, top: r.top, width: r.width, height: r.height }
171
+ }
172
+
173
+ /** An element's top-left corner radius in px (a percentage resolves against its width) */
174
+ export function radiusOf(el: Element): number {
175
+ const value = getComputedStyle(el).borderTopLeftRadius || '0'
176
+ const n = parseFloat(value)
177
+ if (Number.isNaN(n)) return 0
178
+ return value.trim().endsWith('%') ? (n / 100) * (el as HTMLElement).offsetWidth : n
179
+ }
@@ -11,6 +11,8 @@ const stack: (() => void)[] = []
11
11
 
12
12
  function onKeydown(e: KeyboardEvent) {
13
13
  if (e.key !== 'Escape' || !stack.length) return
14
+ // Handled: the browser then doesn't also run a close watcher with it (MContainerTransform's, under a dialog)
15
+ e.preventDefault()
14
16
  stack[stack.length - 1]!()
15
17
  }
16
18
 
@@ -0,0 +1,59 @@
1
+ import { ref, shallowRef, type ComponentPublicInstance, type Ref, type ShallowRef } from 'vue'
2
+
3
+ /** What a container transform opens from: a click (its element), an element, or a component */
4
+ export type ContainerTransformOrigin = Event | Element | ComponentPublicInstance | null | undefined
5
+
6
+ /** The state MContainerTransform plays, shared between the elements that open it and the panel they open */
7
+ export interface ContainerTransformController<T = unknown> {
8
+ /** Whether the expanded view is open (or opening) */
9
+ isOpen: Ref<boolean>
10
+ /** What `open` was given, for the expanded view to show */
11
+ item: Ref<T | undefined>
12
+ /** The element it grows from and shrinks back into */
13
+ source: ShallowRef<HTMLElement | null>
14
+ /** Opens from `origin` (a click's element, an element or a component), with an optional `item` */
15
+ open: (origin?: ContainerTransformOrigin, item?: T) => void
16
+ /** Shrinks the expanded view back into the element it opened from */
17
+ close: () => void
18
+ }
19
+
20
+ /** The element an origin stands for: a click's `currentTarget` (or `target`), a component's root element */
21
+ export function originElement(origin: ContainerTransformOrigin): HTMLElement | null {
22
+ if (!origin) return null
23
+ if (typeof Event !== 'undefined' && origin instanceof Event) {
24
+ return ((origin.currentTarget as HTMLElement | null) ?? (origin.target as HTMLElement | null)) || null
25
+ }
26
+ if (typeof Element !== 'undefined' && origin instanceof Element) return origin as HTMLElement
27
+ const el = (origin as ComponentPublicInstance).$el
28
+ return el instanceof Element ? (el as HTMLElement) : null
29
+ }
30
+
31
+ /**
32
+ * Material's container transform for elements that live apart from their expanded view: one controller, many
33
+ * elements opening it (a grid of cards), and one `<MContainerTransform :controller>` wherever the expanded view
34
+ * belongs. `open(event, item)` from a click grows that element into the expanded view, with `item` for it to show.
35
+ *
36
+ * ```ts
37
+ * const detail = useContainerTransform<Recipe>()
38
+ * // <MCard v-for="r in recipes" @click="detail.open($event, r)">…</MCard>
39
+ * // <MContainerTransform :controller="detail"><template #expanded="{ item }">…</template></MContainerTransform>
40
+ * ```
41
+ */
42
+ export function useContainerTransform<T = unknown>(): ContainerTransformController<T> {
43
+ const isOpen = ref(false)
44
+ const item = ref<T>() as Ref<T | undefined>
45
+ const source = shallowRef<HTMLElement | null>(null)
46
+ return {
47
+ isOpen,
48
+ item,
49
+ source,
50
+ open(origin, value) {
51
+ source.value = originElement(origin) ?? source.value
52
+ item.value = value
53
+ isOpen.value = true
54
+ },
55
+ close() {
56
+ isOpen.value = false
57
+ },
58
+ }
59
+ }
@@ -201,6 +201,9 @@ export interface M3Locale {
201
201
  carouselItem: string
202
202
  /** MSparkline's summary for screen readers: {first}, {last}, {min}, {max} */
203
203
  sparklineSummary: string
204
+ /** MSlider range: its two thumbs */
205
+ sliderRangeStart: string
206
+ sliderRangeEnd: string
204
207
  // MMap (maplibre controls)
205
208
  mapTitle: string
206
209
  mapMarker: string
@@ -415,6 +418,8 @@ export const defaultLocale: M3Locale = {
415
418
  carousel: 'Carousel',
416
419
  carouselItem: '{index} of {count}',
417
420
  sparklineSummary: 'From {first} to {last}, low {min}, high {max}',
421
+ sliderRangeStart: 'Range start',
422
+ sliderRangeEnd: 'Range end',
418
423
  // MMap (maplibre controls)
419
424
  mapTitle: 'Map',
420
425
  mapMarker: 'Map marker',
package/src/index.ts CHANGED
@@ -9,6 +9,8 @@ export type { Theme } from './composables/useTheme'
9
9
  export { useColorPalette, palettes } from './composables/useColorPalette'
10
10
  export { useConfetti, fireConfetti } from './composables/useConfetti'
11
11
  export type { ConfettiOptions, ConfettiOrigin } from './composables/useConfetti'
12
+ export { useContainerTransform } from './composables/useContainerTransform'
13
+ export type { ContainerTransformController, ContainerTransformOrigin } from './composables/useContainerTransform'
12
14
  export type { Palette, PaletteId, ActiveSeed, ContrastLevel } from './composables/useColorPalette'
13
15
  export { SEED_PALETTE } from './composables/useColorPalette'
14
16
  export { themeInitScript } from './composables/themeInitScript'
@@ -70,6 +72,7 @@ export { default as MColorPicker } from './components/MColorPicker.vue'
70
72
  export { default as MColorPickerModal } from './components/MColorPickerModal.vue'
71
73
  export { default as MCommandPalette } from './components/MCommandPalette.vue'
72
74
  export { default as MConfirmDialog } from './components/MConfirmDialog.vue'
75
+ export { default as MContainerTransform } from './components/MContainerTransform.vue'
73
76
  export { default as MContainer } from './components/MContainer.vue'
74
77
  export { default as MContextMenu } from './components/MContextMenu.vue'
75
78
  export { default as MCopyButton } from './components/MCopyButton.vue'
package/src/locales/de.ts CHANGED
@@ -194,6 +194,8 @@ export const deLocale: M3Locale = {
194
194
  carousel: 'Karussell',
195
195
  carouselItem: '{index} von {count}', // MMap (maplibre controls)
196
196
  sparklineSummary: 'Von {first} bis {last}, Minimum {min}, Maximum {max}',
197
+ sliderRangeStart: 'Bereichsanfang',
198
+ sliderRangeEnd: 'Bereichsende',
197
199
  mapTitle: 'Karte',
198
200
  mapMarker: 'Kartenmarkierung',
199
201
  mapZoomIn: 'Vergrößern',
package/src/locales/es.ts CHANGED
@@ -194,6 +194,8 @@ export const esLocale: M3Locale = {
194
194
  carousel: 'Carrusel',
195
195
  carouselItem: '{index} de {count}', // MMap (maplibre controls)
196
196
  sparklineSummary: 'De {first} a {last}, mínimo {min}, máximo {max}',
197
+ sliderRangeStart: 'Inicio del rango',
198
+ sliderRangeEnd: 'Fin del rango',
197
199
  mapTitle: 'Mapa',
198
200
  mapMarker: 'Marcador del mapa',
199
201
  mapZoomIn: 'Acercar',
package/src/locales/fr.ts CHANGED
@@ -194,6 +194,8 @@ export const frLocale: M3Locale = {
194
194
  carousel: 'Carrousel',
195
195
  carouselItem: '{index} sur {count}', // MMap (maplibre controls)
196
196
  sparklineSummary: 'De {first} à {last}, minimum {min}, maximum {max}',
197
+ sliderRangeStart: 'Début de la plage',
198
+ sliderRangeEnd: 'Fin de la plage',
197
199
  mapTitle: 'Carte',
198
200
  mapMarker: 'Marqueur de carte',
199
201
  mapZoomIn: 'Zoom avant',
package/src/locales/ja.ts CHANGED
@@ -194,6 +194,8 @@ export const jaLocale: M3Locale = {
194
194
  carousel: 'カルーセル',
195
195
  carouselItem: '{count} 件中 {index} 件目', // MMap (maplibre controls)
196
196
  sparklineSummary: '{first} から {last}、最小 {min}、最大 {max}',
197
+ sliderRangeStart: '範囲の開始',
198
+ sliderRangeEnd: '範囲の終了',
197
199
  mapTitle: '地図',
198
200
  mapMarker: '地図のマーカー',
199
201
  mapZoomIn: 'ズームイン',