@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.
@@ -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
- * Library convention (see CLAUDE.md): springs stepped in one rAF loop, state in plain
12
- * variables, the DOM written once per frame. The panel's transform must never also be bound
13
- * reactively in the template, which is what silently killed the old CSS enter animation.
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
- interface Spring { damping: number; stiffness: number }
20
- const DEFAULT_SPATIAL: Spring = { damping: 0.9, stiffness: 700 }
21
- const FAST_EFFECTS: Spring = { damping: 1, stiffness: 3800 }
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
- const offset = { pos: 0, vel: 0 }
44
- const alpha = { pos: 0, vel: 0 }
45
- let offsetTarget = 0
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 offsetSpring = DEFAULT_SPATIAL
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 size = () => (panel ? (axis === 'y' ? panel.offsetHeight : panel.offsetWidth) : 0)
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 = axis === 'y' ? `translateY(${offset.pos}px)` : `translateX(${offset.pos}px)`
63
- if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha.pos)))
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 finish() {
67
- raf = 0
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
- function frame(now: number) {
74
- const dt = Math.min((now - last) / 1000, 1 / 30)
75
- last = now
76
- if (!dragging) step(offset, offsetTarget, offsetSpring, dt)
77
- step(alpha, alphaTarget, DEFAULT_EFFECTS, dt)
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
- raf = requestAnimationFrame(frame)
91
- }
92
-
93
- function run() {
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 finish()
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
- if (raf) return
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
- if (raf) cancelAnimationFrame(raf)
116
- raf = 0
132
+ offsetAnim?.cancel()
133
+ alphaAnim?.cancel()
134
+ offsetAnim = alphaAnim = null
117
135
  bind(el)
118
- offset.pos = size()
119
- offset.vel = 0
120
- alpha.pos = 0
121
- alpha.vel = 0
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
- run()
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
- offsetTarget = size()
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.pos
147
- offset.vel = 0
148
- lastMove = { at: performance.now(), pos: offset.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
- offset.pos = Math.max(0, dragOrigin + (axis === 'y' ? e.clientY : e.clientX) - dragStart)
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) offset.vel = (offset.pos - lastMove.pos) / dt
158
- lastMove = { at: now, pos: offset.pos }
159
- write()
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) offset.vel = 0
167
- if (offset.pos > DISMISS_DISTANCE || offset.vel > DISMISS_VELOCITY) {
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
- offsetTarget = 0
171
- offsetSpring = DEFAULT_SPATIAL
172
- run()
184
+ animate(0, DEFAULT_SPATIAL, alphaTarget)
173
185
  }
174
186
  }
175
187
 
176
188
  onBeforeUnmount(() => {
177
- if (raf) cancelAnimationFrame(raf)
189
+ offsetAnim?.cancel()
190
+ alphaAnim?.cancel()
178
191
  })
179
192
 
180
193
  return { onEnter, onLeave, onDragStart, onDragMove, onDragEnd }