@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
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { onBeforeUnmount } from 'vue'
|
|
2
|
+
import { motionSprings, type SpringSpec } from './motion'
|
|
3
|
+
import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from './_springAnimation'
|
|
2
4
|
|
|
3
5
|
/*
|
|
4
6
|
* Internal (not exported from index.ts): enter/exit/drag motion for modal sheets
|
|
@@ -8,99 +10,114 @@ import { onBeforeUnmount } from 'vue'
|
|
|
8
10
|
* (1.0 / 1600). The panel slides only, no opacity fade. A drag release springs from where
|
|
9
11
|
* the finger left it, carrying its velocity, either back open or out.
|
|
10
12
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
13
|
+
* The springs play as Web Animations (see _springAnimation.ts), on the compositor, so the
|
|
14
|
+
* enter isn't janked by the sheet's content mounting at the same moment; only the drag writes
|
|
15
|
+
* the transform directly. The offset is a fraction of the panel's size, so entering never reads
|
|
16
|
+
* layout. The panel's transform must never also be bound reactively in the template, which is
|
|
17
|
+
* what silently killed the old CSS enter animation.
|
|
14
18
|
*
|
|
15
19
|
* Wire it to a `<Transition :css="false" @enter @leave>` around a root holding
|
|
16
20
|
* `[data-sheet-panel]` and `[data-sheet-scrim]`.
|
|
17
21
|
*/
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const DEFAULT_EFFECTS: Spring = { damping: 1, stiffness: 1600 }
|
|
23
|
+
const DEFAULT_SPATIAL = motionSprings.standard.defaultSpatial
|
|
24
|
+
const FAST_EFFECTS = motionSprings.standard.fastEffects
|
|
25
|
+
const DEFAULT_EFFECTS = motionSprings.standard.defaultEffects
|
|
23
26
|
|
|
24
27
|
/** Past this many px, or this fast (px/s) toward closing, a released drag dismisses. */
|
|
25
28
|
const DISMISS_DISTANCE = 100
|
|
26
29
|
const DISMISS_VELOCITY = 1000
|
|
27
30
|
|
|
28
|
-
// Semi-implicit Euler in fixed 4ms substeps: FastEffects' stiffness (3800) is unstable at
|
|
29
|
-
// a dropped frame's 33ms step (it diverges instead of settling).
|
|
30
|
-
const SUBSTEP = 0.004
|
|
31
|
-
function step(s: { pos: number; vel: number }, target: number, spring: Spring, dt: number) {
|
|
32
|
-
for (let left = dt; left > 0; left -= SUBSTEP) {
|
|
33
|
-
const h = Math.min(SUBSTEP, left)
|
|
34
|
-
const accel = -spring.stiffness * (s.pos - target) - 2 * spring.damping * Math.sqrt(spring.stiffness) * s.vel
|
|
35
|
-
s.vel += accel * h
|
|
36
|
-
s.pos += s.vel * h
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
31
|
export function useSheetMotion(axis: 'x' | 'y', onDismiss: () => void) {
|
|
41
32
|
let panel: HTMLElement | null = null
|
|
42
33
|
let scrim: HTMLElement | null = null
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let
|
|
34
|
+
// 0 = open, 1 = off-screen (a fraction of the panel's size); alpha is the scrim's opacity
|
|
35
|
+
let offset = 0
|
|
36
|
+
let alpha = 0
|
|
46
37
|
let alphaTarget = 0
|
|
47
|
-
let
|
|
38
|
+
let velocity = 0
|
|
39
|
+
let offsetAnim: Animation | null = null
|
|
40
|
+
let alphaAnim: Animation | null = null
|
|
41
|
+
let offsetSamples: number[] = []
|
|
42
|
+
let alphaSamples: number[] = []
|
|
48
43
|
let settled: (() => void) | null = null
|
|
49
|
-
let raf = 0
|
|
50
|
-
let last = 0
|
|
51
44
|
|
|
52
45
|
let dragging = false
|
|
53
46
|
let dragStart = 0
|
|
54
47
|
let dragOrigin = 0
|
|
48
|
+
let size = 1
|
|
55
49
|
let lastMove = { at: 0, pos: 0 }
|
|
50
|
+
let dragVelocity = 0
|
|
56
51
|
|
|
57
|
-
const
|
|
58
|
-
const reducedMotion = () =>
|
|
59
|
-
typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
52
|
+
const translate = (f: number) => (axis === 'y' ? `translateY(${f * 100}%)` : `translateX(${f * 100}%)`)
|
|
60
53
|
|
|
61
54
|
function write() {
|
|
62
|
-
if (panel) panel.style.transform =
|
|
63
|
-
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha
|
|
55
|
+
if (panel) panel.style.transform = translate(offset)
|
|
56
|
+
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha)))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function stopOffset() {
|
|
60
|
+
if (!offsetAnim) return
|
|
61
|
+
offset = sampleAt(offsetSamples, offsetAnim.currentTime)
|
|
62
|
+
if (panel) panel.style.transform = translate(offset)
|
|
63
|
+
offsetAnim.cancel()
|
|
64
|
+
offsetAnim = null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function stopAlpha() {
|
|
68
|
+
if (!alphaAnim) return
|
|
69
|
+
alpha = sampleAt(alphaSamples, alphaAnim.currentTime)
|
|
70
|
+
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha)))
|
|
71
|
+
alphaAnim.cancel()
|
|
72
|
+
alphaAnim = null
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
function
|
|
67
|
-
|
|
75
|
+
function maybeSettle() {
|
|
76
|
+
if (offsetAnim || alphaAnim || dragging) return
|
|
68
77
|
const cb = settled
|
|
69
78
|
settled = null
|
|
70
79
|
cb?.()
|
|
71
80
|
}
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const offsetDone = dragging || (Math.abs(offset.pos - offsetTarget) < 0.5 && Math.abs(offset.vel) < 5)
|
|
79
|
-
const alphaDone = Math.abs(alpha.pos - alphaTarget) < 0.002 && Math.abs(alpha.vel) < 0.05
|
|
80
|
-
if (offsetDone && alphaDone) {
|
|
81
|
-
if (!dragging) offset.pos = offsetTarget
|
|
82
|
-
alpha.pos = alphaTarget
|
|
83
|
-
offset.vel = alpha.vel = 0
|
|
84
|
-
write()
|
|
85
|
-
if (!dragging) return finish()
|
|
86
|
-
raf = 0
|
|
87
|
-
return
|
|
82
|
+
// Without `done`, a pending Transition callback (an enter interrupted by a drag) is kept for this run
|
|
83
|
+
function animate(offsetTo: number, spring: SpringSpec, alphaTo: number, done?: () => void) {
|
|
84
|
+
if (done) {
|
|
85
|
+
settled?.()
|
|
86
|
+
settled = done
|
|
88
87
|
}
|
|
88
|
+
stopOffset()
|
|
89
|
+
const fromOffset = offset
|
|
90
|
+
const v = velocity
|
|
91
|
+
velocity = 0
|
|
92
|
+
// The scrim keeps its running fade when its target doesn't change (a drag released back open)
|
|
93
|
+
const restartAlpha = alphaTo !== alphaTarget || !alphaAnim
|
|
94
|
+
if (restartAlpha) stopAlpha()
|
|
95
|
+
const fromAlpha = alpha
|
|
96
|
+
alphaTarget = alphaTo
|
|
97
|
+
// End states inline underneath, so nothing jumps when the animations end
|
|
98
|
+
offset = offsetTo
|
|
99
|
+
if (restartAlpha) alpha = alphaTo
|
|
89
100
|
write()
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (reducedMotion()) {
|
|
95
|
-
offset.pos = offsetTarget
|
|
96
|
-
alpha.pos = alphaTarget
|
|
97
|
-
offset.vel = alpha.vel = 0
|
|
101
|
+
if (prefersReducedMotion()) {
|
|
102
|
+
alphaAnim?.cancel()
|
|
103
|
+
alphaAnim = null
|
|
104
|
+
alpha = alphaTo
|
|
98
105
|
write()
|
|
99
|
-
return
|
|
106
|
+
return maybeSettle()
|
|
107
|
+
}
|
|
108
|
+
if (fromOffset !== offsetTo) {
|
|
109
|
+
offsetSamples = sampleSpring(fromOffset, offsetTo, v, spring, 0.001)
|
|
110
|
+
const a = playSamples(panel, offsetSamples, (f) => ({ transform: translate(f) }))
|
|
111
|
+
offsetAnim = a
|
|
112
|
+
if (a) a.onfinish = () => { if (offsetAnim === a) { offsetAnim = null; maybeSettle() } }
|
|
113
|
+
}
|
|
114
|
+
if (restartAlpha && fromAlpha !== alphaTo) {
|
|
115
|
+
alphaSamples = sampleSpring(fromAlpha, alphaTo, 0, DEFAULT_EFFECTS, 0.002)
|
|
116
|
+
const a = playSamples(scrim, alphaSamples, (o) => ({ opacity: String(Math.min(1, Math.max(0, o))) }))
|
|
117
|
+
alphaAnim = a
|
|
118
|
+
if (a) a.onfinish = () => { if (alphaAnim === a) { alphaAnim = null; maybeSettle() } }
|
|
100
119
|
}
|
|
101
|
-
|
|
102
|
-
last = performance.now()
|
|
103
|
-
raf = requestAnimationFrame(frame)
|
|
120
|
+
maybeSettle()
|
|
104
121
|
}
|
|
105
122
|
|
|
106
123
|
function bind(el: Element) {
|
|
@@ -112,69 +129,65 @@ export function useSheetMotion(axis: 'x' | 'y', onDismiss: () => void) {
|
|
|
112
129
|
// Reopened mid-exit: let the old element go now instead of leaving it in the DOM.
|
|
113
130
|
settled?.()
|
|
114
131
|
settled = null
|
|
115
|
-
|
|
116
|
-
|
|
132
|
+
offsetAnim?.cancel()
|
|
133
|
+
alphaAnim?.cancel()
|
|
134
|
+
offsetAnim = alphaAnim = null
|
|
117
135
|
bind(el)
|
|
118
|
-
offset
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
offsetTarget = 0
|
|
123
|
-
alphaTarget = 1
|
|
124
|
-
offsetSpring = DEFAULT_SPATIAL
|
|
125
|
-
settled = done
|
|
136
|
+
offset = 1
|
|
137
|
+
alpha = 0
|
|
138
|
+
alphaTarget = 0
|
|
139
|
+
velocity = 0
|
|
126
140
|
write()
|
|
127
|
-
|
|
141
|
+
animate(0, DEFAULT_SPATIAL, 1, done)
|
|
128
142
|
}
|
|
129
143
|
|
|
130
144
|
function onLeave(el: Element, done: () => void) {
|
|
131
145
|
bind(el)
|
|
132
146
|
dragging = false
|
|
133
|
-
|
|
134
|
-
alphaTarget = 0
|
|
135
|
-
offsetSpring = FAST_EFFECTS
|
|
136
|
-
settled = done
|
|
137
|
-
run()
|
|
147
|
+
animate(1, FAST_EFFECTS, 0, done)
|
|
138
148
|
}
|
|
139
149
|
|
|
140
150
|
function onDragStart(e: PointerEvent) {
|
|
141
151
|
// A sheet mounted already open never ran onEnter: find its elements from the handle.
|
|
142
152
|
const root = (e.currentTarget as HTMLElement).closest('[data-sheet-panel]')?.parentElement
|
|
143
153
|
if (root) bind(root)
|
|
154
|
+
stopOffset()
|
|
144
155
|
dragging = true
|
|
156
|
+
size = (panel ? (axis === 'y' ? panel.offsetHeight : panel.offsetWidth) : 0) || 1
|
|
145
157
|
dragStart = axis === 'y' ? e.clientY : e.clientX
|
|
146
|
-
dragOrigin = offset
|
|
147
|
-
|
|
148
|
-
lastMove = { at: performance.now(), pos:
|
|
158
|
+
dragOrigin = offset * size
|
|
159
|
+
dragVelocity = 0
|
|
160
|
+
lastMove = { at: performance.now(), pos: dragOrigin }
|
|
149
161
|
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
|
|
150
162
|
}
|
|
151
163
|
|
|
152
164
|
function onDragMove(e: PointerEvent) {
|
|
153
165
|
if (!dragging) return
|
|
154
166
|
const now = performance.now()
|
|
155
|
-
|
|
167
|
+
const pos = Math.max(0, dragOrigin + (axis === 'y' ? e.clientY : e.clientX) - dragStart)
|
|
156
168
|
const dt = (now - lastMove.at) / 1000
|
|
157
|
-
if (dt > 0)
|
|
158
|
-
lastMove = { at: now, pos
|
|
159
|
-
|
|
169
|
+
if (dt > 0) dragVelocity = (pos - lastMove.pos) / dt
|
|
170
|
+
lastMove = { at: now, pos }
|
|
171
|
+
offset = pos / size
|
|
172
|
+
if (panel) panel.style.transform = translate(offset)
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
function onDragEnd() {
|
|
163
176
|
if (!dragging) return
|
|
164
177
|
dragging = false
|
|
165
178
|
// A pause before lifting the finger means no fling.
|
|
166
|
-
if (performance.now() - lastMove.at > 100)
|
|
167
|
-
|
|
179
|
+
if (performance.now() - lastMove.at > 100) dragVelocity = 0
|
|
180
|
+
velocity = dragVelocity / size
|
|
181
|
+
if (offset * size > DISMISS_DISTANCE || dragVelocity > DISMISS_VELOCITY) {
|
|
168
182
|
onDismiss()
|
|
169
183
|
} else {
|
|
170
|
-
|
|
171
|
-
offsetSpring = DEFAULT_SPATIAL
|
|
172
|
-
run()
|
|
184
|
+
animate(0, DEFAULT_SPATIAL, alphaTarget)
|
|
173
185
|
}
|
|
174
186
|
}
|
|
175
187
|
|
|
176
188
|
onBeforeUnmount(() => {
|
|
177
|
-
|
|
189
|
+
offsetAnim?.cancel()
|
|
190
|
+
alphaAnim?.cancel()
|
|
178
191
|
})
|
|
179
192
|
|
|
180
193
|
return { onEnter, onLeave, onDragStart, onDragMove, onDragEnd }
|