@m3ui-vue/m3ui-vue 1.0.0 → 1.0.1
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/dist/API.md +3 -2
- package/dist/{MTextField-TZ7lfr09.js → MTextField-BPbFeI9M.js} +76 -73
- package/dist/MTextField-BPbFeI9M.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/components/MNavigationDrawer.vue.d.ts +9 -6
- package/dist/components/MTransition.vue.d.ts +0 -9
- package/dist/composables/_springAnimation.d.ts +17 -0
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +2952 -2765
- package/dist/m3ui.js.map +1 -1
- package/dist/rich-text-editor.js +1 -1
- package/dist/styles.css +1 -1
- package/package.json +1 -1
- package/src/__tests__/MAppBarWithSearch.test.ts +12 -0
- package/src/__tests__/MBottomSheetScaffold.test.ts +21 -0
- package/src/__tests__/MNavigationDrawer.test.ts +185 -0
- package/src/__tests__/MNavigationRail.test.ts +17 -1
- package/src/__tests__/MPullToRefresh.test.ts +18 -1
- package/src/__tests__/MTransition.test.ts +12 -0
- package/src/__tests__/sheets.test.ts +19 -15
- package/src/components/MBottomSheetScaffold.vue +23 -2
- package/src/components/MContextMenu.vue +34 -33
- package/src/components/MMenu.vue +34 -33
- package/src/components/MNavigationDrawer.vue +297 -37
- package/src/components/MNavigationRail.vue +69 -15
- package/src/components/MPullToRefresh.vue +18 -0
- package/src/components/MSearchBar.vue +4 -3
- package/src/components/MTransition.vue +36 -41
- package/src/composables/_springAnimation.ts +74 -0
- package/src/composables/_useSheetMotion.ts +101 -88
- package/dist/MTextField-TZ7lfr09.js.map +0 -1
|
@@ -12,6 +12,7 @@ import { computed, ref, watch, onUnmounted } from 'vue'
|
|
|
12
12
|
import MIcon from './MIcon.vue'
|
|
13
13
|
import MBadge from './MBadge.vue'
|
|
14
14
|
import { useItemLink, type ItemLink } from '../composables/_useItemLink'
|
|
15
|
+
import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from '../composables/_springAnimation'
|
|
15
16
|
|
|
16
17
|
export interface NavRailItem {
|
|
17
18
|
value: string | number
|
|
@@ -162,35 +163,79 @@ function springValue(
|
|
|
162
163
|
}
|
|
163
164
|
|
|
164
165
|
// ── Dismissible panel (hideOnCollapse): slide + swipe ─────────────────────────────────────────
|
|
165
|
-
// The panel's x runs from -
|
|
166
|
+
// The panel's x runs from -1 (off-screen, a fraction of its width) to 0, and the scrim's opacity follows it.
|
|
167
|
+
// Slides play as Web Animations (see _springAnimation.ts), on the compositor, so the open isn't janked by the
|
|
168
|
+
// panel mounting at the same moment; only the drag writes the transform directly.
|
|
166
169
|
let panelX = 0
|
|
167
170
|
let releaseVelocity = 0
|
|
171
|
+
let slideAnim: Animation | null = null
|
|
172
|
+
let slideScrimAnim: Animation | null = null
|
|
173
|
+
let slideSamples: number[] = []
|
|
174
|
+
let slidePanel: HTMLElement | null = null
|
|
175
|
+
let slideScrim: HTMLElement | null = null
|
|
176
|
+
let slideDone: (() => void) | null = null
|
|
168
177
|
|
|
169
178
|
function setSlide(panel: HTMLElement, scrim: HTMLElement | null, x: number) {
|
|
170
179
|
panelX = x
|
|
171
|
-
panel.style.transform = `translateX(${x}
|
|
172
|
-
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, 1 + x
|
|
180
|
+
panel.style.transform = `translateX(${x * 100}%)`
|
|
181
|
+
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, 1 + x)))
|
|
173
182
|
}
|
|
174
183
|
|
|
184
|
+
// Freezes the panel where the running slide has it
|
|
185
|
+
function stopSlide() {
|
|
186
|
+
if (!slideAnim) return
|
|
187
|
+
if (slidePanel) setSlide(slidePanel, slideScrim, sampleAt(slideSamples, slideAnim.currentTime))
|
|
188
|
+
slideAnim.cancel()
|
|
189
|
+
slideScrimAnim?.cancel()
|
|
190
|
+
slideAnim = slideScrimAnim = null
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function finishSlide() {
|
|
194
|
+
const cb = slideDone
|
|
195
|
+
slideDone = null
|
|
196
|
+
cb?.()
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Without `done`, a pending Transition callback (an enter interrupted by a swipe) is kept for this slide
|
|
175
200
|
function slide(el: Element, to: number, done?: () => void) {
|
|
176
201
|
const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
|
|
177
202
|
const scrim = (el as HTMLElement).querySelector<HTMLElement>('.wnr-scrim')
|
|
178
|
-
if (
|
|
203
|
+
if (done) {
|
|
204
|
+
finishSlide()
|
|
205
|
+
slideDone = done
|
|
206
|
+
}
|
|
207
|
+
if (!panel) return finishSlide()
|
|
179
208
|
if (scrim) scrim.style.transition = 'none'
|
|
209
|
+
stopSlide()
|
|
210
|
+
const from = panelX
|
|
180
211
|
const v = releaseVelocity
|
|
181
212
|
releaseVelocity = 0
|
|
182
|
-
|
|
213
|
+
// The end state goes inline underneath, so nothing jumps when the animation ends
|
|
214
|
+
setSlide(panel, scrim, to)
|
|
215
|
+
if (prefersReducedMotion() || from === to) return finishSlide()
|
|
216
|
+
slideSamples = sampleSpring(from, to, v, { dampingRatio: SLIDE_DAMPING, stiffness: SLIDE_STIFFNESS }, 0.001)
|
|
217
|
+
const a = playSamples(panel, slideSamples, (x) => ({ transform: `translateX(${x * 100}%)` }))
|
|
218
|
+
if (!a) return finishSlide()
|
|
219
|
+
slideScrimAnim = playSamples(scrim, slideSamples, (x) => ({ opacity: String(Math.min(1, Math.max(0, 1 + x))) }))
|
|
220
|
+
slideAnim = a
|
|
221
|
+
slidePanel = panel
|
|
222
|
+
slideScrim = scrim
|
|
223
|
+
a.onfinish = () => {
|
|
224
|
+
if (slideAnim !== a) return
|
|
225
|
+
slideAnim = slideScrimAnim = null
|
|
226
|
+
finishSlide()
|
|
227
|
+
}
|
|
183
228
|
}
|
|
184
229
|
|
|
185
230
|
// AnchoredDraggableDefaults: settles at the nearer anchor (positionalThreshold 50%) unless released faster
|
|
186
231
|
// than 125dp/s, which goes the way of the fling
|
|
187
232
|
const FLING_VELOCITY = 125
|
|
188
|
-
let drag: { id: number; x: number; y: number; startX: number; active: boolean; samples: { t: number; x: number }[] } | null = null
|
|
233
|
+
let drag: { id: number; x: number; y: number; startX: number; width: number; active: boolean; samples: { t: number; x: number }[] } | null = null
|
|
189
234
|
let suppressClick = false
|
|
190
235
|
|
|
191
236
|
function onPanelPointerDown(e: PointerEvent) {
|
|
192
237
|
if (!props.hideOnCollapse || e.button !== 0) return
|
|
193
|
-
drag = { id: e.pointerId, x: e.clientX, y: e.clientY, startX:
|
|
238
|
+
drag = { id: e.pointerId, x: e.clientX, y: e.clientY, startX: 0, width: 1, active: false, samples: [{ t: e.timeStamp, x: e.clientX }] }
|
|
194
239
|
}
|
|
195
240
|
|
|
196
241
|
function onPanelPointerMove(e: PointerEvent) {
|
|
@@ -202,12 +247,15 @@ function onPanelPointerMove(e: PointerEvent) {
|
|
|
202
247
|
if (Math.abs(dx) < 8) return
|
|
203
248
|
drag.active = true
|
|
204
249
|
;(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId)
|
|
250
|
+
// Stop a running slide; the finger owns the panel from where it is
|
|
251
|
+
stopSlide()
|
|
252
|
+
drag.startX = panelX
|
|
253
|
+
drag.width = (e.currentTarget as HTMLElement).offsetWidth || PANEL_WIDTH
|
|
205
254
|
}
|
|
206
255
|
const panel = e.currentTarget as HTMLElement
|
|
207
|
-
springTokens.set(panel, (springTokens.get(panel) ?? 0) + 1) // stop a running slide
|
|
208
256
|
const scrim = panel.parentElement?.querySelector<HTMLElement>('.wnr-scrim') ?? null
|
|
209
257
|
if (scrim) scrim.style.transition = 'none'
|
|
210
|
-
setSlide(panel, scrim, Math.min(0, Math.max(-
|
|
258
|
+
setSlide(panel, scrim, Math.min(0, Math.max(-1, drag.startX + dx / drag.width)))
|
|
211
259
|
drag.samples.push({ t: e.timeStamp, x: e.clientX })
|
|
212
260
|
drag.samples = drag.samples.filter((p) => e.timeStamp - p.t < 100)
|
|
213
261
|
}
|
|
@@ -219,14 +267,14 @@ function onPanelPointerUp(e: PointerEvent) {
|
|
|
219
267
|
const lastSample = drag.samples[drag.samples.length - 1]!
|
|
220
268
|
const dt = (lastSample.t - first.t) / 1000
|
|
221
269
|
const velocity = dt > 0 ? (lastSample.x - first.x) / dt : 0
|
|
270
|
+
const width = drag.width
|
|
222
271
|
drag = null
|
|
223
272
|
if (!wasActive) return
|
|
224
273
|
suppressClick = true
|
|
225
274
|
setTimeout(() => { suppressClick = false })
|
|
226
275
|
const panel = e.currentTarget as HTMLElement
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
releaseVelocity = velocity
|
|
276
|
+
const dismiss = Math.abs(velocity) > FLING_VELOCITY ? velocity < 0 : panelX < -0.5
|
|
277
|
+
releaseVelocity = velocity / width
|
|
230
278
|
if (dismiss) close()
|
|
231
279
|
else slide(panel.parentElement!, 0)
|
|
232
280
|
}
|
|
@@ -246,9 +294,14 @@ function onPanelEnter(el: Element, done: () => void) {
|
|
|
246
294
|
if (props.hideOnCollapse) {
|
|
247
295
|
// Standalone modal: the items are expanded from the start, the panel slides in from off-screen
|
|
248
296
|
panelExpanded.value = true
|
|
297
|
+
// Reopened mid-close: the old panel goes now
|
|
298
|
+
finishSlide()
|
|
299
|
+
slideAnim?.cancel()
|
|
300
|
+
slideScrimAnim?.cancel()
|
|
301
|
+
slideAnim = slideScrimAnim = null
|
|
249
302
|
const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
|
|
250
303
|
const scrim = (el as HTMLElement).querySelector<HTMLElement>('.wnr-scrim')
|
|
251
|
-
if (panel) setSlide(panel, scrim, -
|
|
304
|
+
if (panel) setSlide(panel, scrim, -1)
|
|
252
305
|
slide(el, 0, done)
|
|
253
306
|
return
|
|
254
307
|
}
|
|
@@ -269,8 +322,7 @@ function onPanelEnter(el: Element, done: () => void) {
|
|
|
269
322
|
}
|
|
270
323
|
function onPanelLeave(el: Element, done: () => void) {
|
|
271
324
|
if (props.hideOnCollapse) {
|
|
272
|
-
|
|
273
|
-
slide(el, -(panel?.offsetWidth || PANEL_WIDTH), done)
|
|
325
|
+
slide(el, -1, done)
|
|
274
326
|
return
|
|
275
327
|
}
|
|
276
328
|
const panel = (el as HTMLElement).querySelector<HTMLElement>('.wnr-panel')
|
|
@@ -295,6 +347,8 @@ watch(
|
|
|
295
347
|
)
|
|
296
348
|
|
|
297
349
|
onUnmounted(() => {
|
|
350
|
+
slideAnim?.cancel()
|
|
351
|
+
slideScrimAnim?.cancel()
|
|
298
352
|
document.removeEventListener('keydown', onKeydown)
|
|
299
353
|
if (props.modal && props.expanded) document.body.style.overflow = ''
|
|
300
354
|
})
|
|
@@ -89,6 +89,18 @@ function canStart() {
|
|
|
89
89
|
return !props.disabled && !props.refreshing && !committed && !!scrollEl.value && scrollEl.value.scrollTop <= 0;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
// While a pull can start, the scroller only allows the finger moving up (pan-down): a touch-action that lets the
|
|
93
|
+
// browser pan along the pull makes Chrome deliver it at 60Hz on a 120Hz phone, as a possible scroll waiting on
|
|
94
|
+
// our touchmove listener (see CLAUDE.md). Scrolled, refreshing or disabled, it pans both ways. Browsers without
|
|
95
|
+
// directional pan values (Safari) keep pan-y.
|
|
96
|
+
function updateTouchAction() {
|
|
97
|
+
const el = scrollEl.value;
|
|
98
|
+
if (!el) return;
|
|
99
|
+
const directional = typeof CSS !== "undefined" && !!CSS.supports?.("touch-action", "pan-down");
|
|
100
|
+
const action = canStart() && directional ? "pan-down" : "pan-y";
|
|
101
|
+
if (el.style.touchAction !== action) el.style.touchAction = action;
|
|
102
|
+
}
|
|
103
|
+
|
|
92
104
|
function beginPull(y: number) {
|
|
93
105
|
startY = y;
|
|
94
106
|
pulling = true;
|
|
@@ -125,6 +137,7 @@ function finishPull() {
|
|
|
125
137
|
hideIndicator(true);
|
|
126
138
|
}
|
|
127
139
|
pullDistance = 0;
|
|
140
|
+
updateTouchAction();
|
|
128
141
|
}
|
|
129
142
|
|
|
130
143
|
// Mouse/pen: pointer events. Touch is handled separately below — a touch's
|
|
@@ -220,8 +233,10 @@ watch(
|
|
|
220
233
|
committed = false;
|
|
221
234
|
hideIndicator(true);
|
|
222
235
|
}
|
|
236
|
+
updateTouchAction();
|
|
223
237
|
},
|
|
224
238
|
);
|
|
239
|
+
watch(() => props.disabled, updateTouchAction);
|
|
225
240
|
|
|
226
241
|
// The indicator's transform/opacity are owned entirely by the functions
|
|
227
242
|
// above, written directly to the DOM — never bound reactively in the
|
|
@@ -238,6 +253,8 @@ onMounted(() => {
|
|
|
238
253
|
// Must be non-passive: preventDefault() here is what stops the native scroll.
|
|
239
254
|
el.addEventListener("touchmove", onTouchMove, { passive: false });
|
|
240
255
|
el.addEventListener("touchend", onTouchEnd);
|
|
256
|
+
el.addEventListener("scroll", updateTouchAction, { passive: true });
|
|
257
|
+
updateTouchAction();
|
|
241
258
|
el.addEventListener("touchcancel", onTouchEnd);
|
|
242
259
|
});
|
|
243
260
|
onBeforeUnmount(() => {
|
|
@@ -247,6 +264,7 @@ onBeforeUnmount(() => {
|
|
|
247
264
|
el.removeEventListener("touchstart", onTouchStart);
|
|
248
265
|
el.removeEventListener("touchmove", onTouchMove);
|
|
249
266
|
el.removeEventListener("touchend", onTouchEnd);
|
|
267
|
+
el.removeEventListener("scroll", updateTouchAction);
|
|
250
268
|
el.removeEventListener("touchcancel", onTouchEnd);
|
|
251
269
|
});
|
|
252
270
|
</script>
|
|
@@ -155,13 +155,14 @@ onUnmounted(() => { document.body.style.overflow = '' })
|
|
|
155
155
|
|
|
156
156
|
<template>
|
|
157
157
|
<div ref="barEl" class="relative w-full" v-bind="$attrs">
|
|
158
|
+
<!-- Flat, told apart by its color: Compose's SearchBarDefaults.ShadowElevation is Level0 for every variant
|
|
159
|
+
(SearchBarTokens.ContainerElevation's Level3 isn't used by any of them) -->
|
|
158
160
|
<div
|
|
159
161
|
v-show="!expanded"
|
|
160
|
-
class="flex h-14 items-center gap-3 rounded-full px-4 transition-[
|
|
162
|
+
class="flex h-14 items-center gap-3 rounded-full px-4 transition-[background-color] duration-150 focus-within:outline focus-within:outline-2 focus-within:outline-secondary focus-within:-outline-offset-2"
|
|
161
163
|
:class="[
|
|
162
164
|
disabled ? 'pointer-events-none opacity-[0.38]' : '',
|
|
163
165
|
appBar?.scrolled.value ? 'bg-surface-container-highest' : 'bg-surface-container-high',
|
|
164
|
-
appBar ? '' : 'shadow-elevation-3',
|
|
165
166
|
]"
|
|
166
167
|
>
|
|
167
168
|
<span class="inline-flex shrink-0 items-center" :class="iconPosition === 'end' ? 'order-last' : ''">
|
|
@@ -202,7 +203,7 @@ onUnmounted(() => { document.body.style.overflow = '' })
|
|
|
202
203
|
ref="panelEl"
|
|
203
204
|
class="fixed z-500 flex flex-col overflow-hidden"
|
|
204
205
|
:class="[
|
|
205
|
-
fullscreen ? 'inset-0' : 'max-h-[70vh] rounded-[28px]
|
|
206
|
+
fullscreen ? 'inset-0' : 'max-h-[70vh] rounded-[28px]',
|
|
206
207
|
contained ? 'bg-surface-container-low' : 'bg-surface-container-high',
|
|
207
208
|
]"
|
|
208
209
|
:style="variant === 'docked' ? panelPos : undefined"
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @slot default The current view; give it a `key` that changes with it
|
|
10
10
|
*/
|
|
11
|
+
import { sampleSpring, playSamples } from '../composables/_springAnimation'
|
|
12
|
+
|
|
11
13
|
const props = withDefaults(defineProps<{
|
|
12
14
|
/** `shared-axis` (horizontal, for forward/back navigation) or `fade-through` (unrelated destinations) */
|
|
13
15
|
type?: 'shared-axis' | 'fade-through'
|
|
@@ -18,8 +20,7 @@ const props = withDefaults(defineProps<{
|
|
|
18
20
|
}>(), { type: 'shared-axis', reverse: false, appear: false })
|
|
19
21
|
|
|
20
22
|
// slideIn/OutHorizontally and scaleIn/Out default to spring(stiffness = StiffnessMediumLow), no bounce
|
|
21
|
-
const
|
|
22
|
-
const DAMPING = 2 * Math.sqrt(STIFFNESS)
|
|
23
|
+
const SPRING = { dampingRatio: 1, stiffness: 400 }
|
|
23
24
|
// HorizontalSharedAxis
|
|
24
25
|
const SLIDE_FRACTION = 0.2
|
|
25
26
|
const AXIS_FADE = { duration: 90, delay: 50 }
|
|
@@ -30,54 +31,50 @@ const INITIAL_SCALE = 0.8
|
|
|
30
31
|
const TARGET_SCALE = 1.05
|
|
31
32
|
|
|
32
33
|
interface Motion {
|
|
33
|
-
/** Spring from `from` to `to` (an x offset
|
|
34
|
+
/** Spring from `from` to `to` (an x offset as a fraction of the view's width, or a scale) */
|
|
34
35
|
from: number
|
|
35
36
|
to: number
|
|
36
|
-
|
|
37
|
+
transform: (value: number) => string
|
|
37
38
|
/** Linear opacity tween */
|
|
38
39
|
fade: { from: number; to: number; duration: number; delay: number }
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
const running = new WeakMap<Element, () => void>()
|
|
42
43
|
|
|
44
|
+
// Played as Web Animations (see _springAnimation.ts), which the compositor runs, so the entering view mounting
|
|
45
|
+
// (the busiest moment of a navigation) doesn't jank the transition
|
|
43
46
|
function run(el: HTMLElement, m: Motion, done: () => void) {
|
|
44
47
|
running.get(el)?.()
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const samples = sampleSpring(m.from, m.to, 0, SPRING, 0.001)
|
|
49
|
+
const move = playSamples(el, samples, (v) => ({ transform: m.transform(v) }))
|
|
50
|
+
const fade = typeof el.animate === 'function'
|
|
51
|
+
? el.animate([{ opacity: String(m.fade.from) }, { opacity: String(m.fade.to) }], {
|
|
52
|
+
duration: m.fade.duration,
|
|
53
|
+
delay: m.fade.delay,
|
|
54
|
+
easing: 'linear',
|
|
55
|
+
// Holds the starting opacity through the delay
|
|
56
|
+
fill: 'backwards',
|
|
57
|
+
})
|
|
58
|
+
: null
|
|
59
|
+
fade?.finished.catch(() => {})
|
|
60
|
+
// The end state goes inline underneath
|
|
61
|
+
el.style.transform = m.transform(m.to)
|
|
62
|
+
el.style.opacity = String(m.fade.to)
|
|
63
|
+
let ended = false
|
|
51
64
|
const finish = () => {
|
|
52
|
-
|
|
65
|
+
if (ended) return
|
|
66
|
+
ended = true
|
|
67
|
+
move?.cancel()
|
|
68
|
+
fade?.cancel()
|
|
53
69
|
running.delete(el)
|
|
54
70
|
done()
|
|
55
71
|
}
|
|
56
72
|
running.set(el, finish)
|
|
57
|
-
|
|
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)
|
|
73
|
+
Promise.all([move?.finished, fade?.finished]).then(finish, () => {})
|
|
77
74
|
}
|
|
78
75
|
|
|
79
|
-
const translate = (x: number
|
|
80
|
-
const scale = (
|
|
76
|
+
const translate = (x: number) => `translateX(${x * 100}%)`
|
|
77
|
+
const scale = (v: number) => `scale(${v})`
|
|
81
78
|
|
|
82
79
|
function clear(el: HTMLElement) {
|
|
83
80
|
el.style.transform = ''
|
|
@@ -89,16 +86,15 @@ function onEnter(element: Element, done: () => void) {
|
|
|
89
86
|
const el = element as HTMLElement
|
|
90
87
|
const finish = () => { clear(el); done() }
|
|
91
88
|
if (props.type === 'fade-through') {
|
|
92
|
-
run(el, { from: INITIAL_SCALE, to: 1,
|
|
89
|
+
run(el, { from: INITIAL_SCALE, to: 1, transform: scale, fade: { from: 0, to: 1, duration: FADE_IN, delay: FADE_OUT } }, finish)
|
|
93
90
|
return
|
|
94
91
|
}
|
|
95
|
-
const w = el.offsetWidth
|
|
96
92
|
// Backward: the entering view sits under the leaving one (targetContentZIndex = -10)
|
|
97
93
|
if (props.reverse) el.style.zIndex = '-1'
|
|
98
94
|
run(el, {
|
|
99
|
-
from: (props.reverse ? -1 : 1) *
|
|
95
|
+
from: (props.reverse ? -1 : 1) * SLIDE_FRACTION,
|
|
100
96
|
to: 0,
|
|
101
|
-
|
|
97
|
+
transform: translate,
|
|
102
98
|
fade: { from: 0, to: 1, ...AXIS_FADE },
|
|
103
99
|
}, finish)
|
|
104
100
|
}
|
|
@@ -107,14 +103,13 @@ function onLeave(element: Element, done: () => void) {
|
|
|
107
103
|
const el = element as HTMLElement
|
|
108
104
|
const finish = () => { clear(el); done() }
|
|
109
105
|
if (props.type === 'fade-through') {
|
|
110
|
-
run(el, { from: 1, to: TARGET_SCALE,
|
|
106
|
+
run(el, { from: 1, to: TARGET_SCALE, transform: scale, fade: { from: 1, to: 0, duration: FADE_OUT, delay: 0 } }, finish)
|
|
111
107
|
return
|
|
112
108
|
}
|
|
113
|
-
const w = el.offsetWidth
|
|
114
109
|
run(el, {
|
|
115
110
|
from: 0,
|
|
116
|
-
to: (props.reverse ? 1 : -1) *
|
|
117
|
-
|
|
111
|
+
to: (props.reverse ? 1 : -1) * SLIDE_FRACTION,
|
|
112
|
+
transform: translate,
|
|
118
113
|
fade: { from: 1, to: 0, ...AXIS_FADE },
|
|
119
114
|
}, finish)
|
|
120
115
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { SpringSpec } from './motion'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Internal (not exported from index.ts): springs played as Web Animations.
|
|
5
|
+
*
|
|
6
|
+
* The library's springs normally step in a rAF loop (see CLAUDE.md). That loop runs on the main thread, and an
|
|
7
|
+
* enter/exit animation starts exactly when the main thread is busiest: mounting the panel's content, re-laying
|
|
8
|
+
* out the page for a scroll lock. On phones that janked visibly (MNavigationDrawer's open, where the old CSS
|
|
9
|
+
* transition hadn't, since the compositor runs those). So enter/exit/settle springs are simulated up front, one
|
|
10
|
+
* sample per 60Hz frame, and played as a linear Web Animation of transform/opacity, which the compositor runs
|
|
11
|
+
* too. Same curve, bounce and release velocity as the loop; only a finger-driven drag writes styles directly.
|
|
12
|
+
*
|
|
13
|
+
* Pattern: write the end state inline, then play the samples over it (an animation beats inline styles), so
|
|
14
|
+
* nothing jumps when it ends; to interrupt, read `sampleAt(samples, anim.currentTime)`, write that inline and
|
|
15
|
+
* cancel.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export const SAMPLE_MS = 1000 / 60
|
|
19
|
+
// Stiff springs (FastEffects, 3800) diverge at a 16ms Euler step: integrate in 4ms substeps
|
|
20
|
+
const SUBSTEP = 0.004
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The spring from `from` to `to`, starting at `velocity` (units/s), one sample per 60Hz frame until it's within
|
|
24
|
+
* `epsilon` of `to` (and slower than `epsilon * 10` per second). The last sample is exactly `to`.
|
|
25
|
+
*/
|
|
26
|
+
export function sampleSpring(from: number, to: number, velocity: number, spring: SpringSpec, epsilon: number): number[] {
|
|
27
|
+
const out = [from]
|
|
28
|
+
let pos = from
|
|
29
|
+
let vel = velocity
|
|
30
|
+
const damping = 2 * spring.dampingRatio * Math.sqrt(spring.stiffness)
|
|
31
|
+
for (let i = 0; i < 600; i++) {
|
|
32
|
+
for (let left = SAMPLE_MS / 1000; left > 1e-9; left -= SUBSTEP) {
|
|
33
|
+
const h = Math.min(SUBSTEP, left)
|
|
34
|
+
vel += (-spring.stiffness * (pos - to) - damping * vel) * h
|
|
35
|
+
pos += vel * h
|
|
36
|
+
}
|
|
37
|
+
out.push(pos)
|
|
38
|
+
if (Math.abs(pos - to) < epsilon && Math.abs(vel) < epsilon * 10) break
|
|
39
|
+
}
|
|
40
|
+
out[out.length - 1] = to
|
|
41
|
+
return out
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The value `samples` has `time` ms into playback (an Animation's currentTime), linear between frames */
|
|
45
|
+
export function sampleAt(samples: number[], time: CSSNumberish | null | undefined): number {
|
|
46
|
+
const t = Number(time ?? 0) / SAMPLE_MS
|
|
47
|
+
const i = Math.floor(t)
|
|
48
|
+
if (i >= samples.length - 1) return samples[samples.length - 1]!
|
|
49
|
+
if (i < 0) return samples[0]!
|
|
50
|
+
return samples[i]! + (samples[i + 1]! - samples[i]!) * (t - i)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** How long `samples` play, in ms */
|
|
54
|
+
export const samplesDuration = (samples: number[]) => (samples.length - 1) * SAMPLE_MS
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Plays `samples` on `el` as a linear Web Animation, each mapped to a keyframe. Null when there's nothing to
|
|
58
|
+
* play or no Web Animations API (the inline end state then simply stands).
|
|
59
|
+
*/
|
|
60
|
+
export function playSamples(
|
|
61
|
+
el: Element | null | undefined,
|
|
62
|
+
samples: number[],
|
|
63
|
+
keyframe: (value: number) => Keyframe,
|
|
64
|
+
delay = 0,
|
|
65
|
+
): Animation | null {
|
|
66
|
+
if (!el || typeof (el as HTMLElement).animate !== 'function' || samples.length < 2) return null
|
|
67
|
+
const anim = (el as HTMLElement).animate(samples.map(keyframe), { duration: samplesDuration(samples), delay, easing: 'linear' })
|
|
68
|
+
// cancel() rejects `finished`; nothing has to wait on it
|
|
69
|
+
anim.finished.catch(() => {})
|
|
70
|
+
return anim
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const prefersReducedMotion = () =>
|
|
74
|
+
typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|