@octane-xplat/motion 0.7.3 → 0.10.0
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/README.md +31 -2
- package/UPSTREAM.md +295 -27
- package/dist/native/index.js +980 -112
- package/dist/native/octane-client-build.json +1 -1
- package/dist/web/index.js +1104 -166
- package/dist/web/octane-client-build.json +1 -1
- package/package.json +25 -16
- package/src/clock.ts +13 -1
- package/src/components.mobile.test.tsrx +58 -1
- package/src/components.tsrx +185 -39
- package/src/components.web.test.tsrx +236 -3
- package/src/controller.ts +122 -10
- package/src/delegation.test.ts +269 -0
- package/src/delegation.ts +29 -0
- package/src/delegation.web.test.ts +270 -0
- package/src/drag.mobile.test.ts +81 -0
- package/src/drag.test.ts +167 -0
- package/src/drag.ts +157 -0
- package/src/driver.ts +249 -0
- package/src/driver.web.ts +180 -0
- package/src/engine.test.ts +99 -0
- package/src/engine.ts +140 -18
- package/src/hooks.tsrx +53 -2
- package/src/host-types.ts +44 -1
- package/src/host.ts +39 -2
- package/src/host.web.ts +51 -10
- package/src/index.ts +18 -4
- package/src/index.web.ts +18 -4
- package/src/pan.ts +92 -0
- package/src/pan.web.ts +116 -0
- package/src/presence-input.ts +11 -4
- package/src/presence-input.web.ts +13 -4
- package/src/presence-state.ts +27 -7
- package/src/presence.tsrx +6 -7
- package/src/reduced-motion.ts +23 -4
- package/src/types.ts +100 -11
- package/src/value.ts +18 -8
- package/src/variants.test.ts +197 -0
- package/src/variants.ts +226 -0
- package/types/engine.d.ts +8 -2
- package/types/index.d.ts +34 -6
- package/types/types.d.ts +94 -11
- package/types/value.d.ts +7 -4
package/src/types.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { View,
|
|
2
|
-
import type { MotionValue } from './value'
|
|
1
|
+
import type { View, HStack, Pressable } from '@octane-xplat/ui'
|
|
2
|
+
import type { MotionValue } from './value.js'
|
|
3
3
|
|
|
4
4
|
type ViewProps = Parameters<typeof View>[0]
|
|
5
|
-
type
|
|
5
|
+
type HStackProps = Parameters<typeof HStack>[0]
|
|
6
6
|
type PressableProps = Parameters<typeof Pressable>[0]
|
|
7
7
|
|
|
8
8
|
/** Supported channels. Translation is CSS pixels/DIP; rotation is degrees. */
|
|
@@ -10,13 +10,43 @@ export type MotionKey = 'opacity' | 'x' | 'y' | 'scale' | 'scaleX' | 'scaleY' |
|
|
|
10
10
|
/** Absolute numeric destinations; omitted channels retain their current values. */
|
|
11
11
|
export type Target = Partial<Record<MotionKey, number>>
|
|
12
12
|
/** Tween easing, including a CSS-compatible cubic Bezier tuple. */
|
|
13
|
-
export type Ease =
|
|
13
|
+
export type Ease =
|
|
14
|
+
| 'linear'
|
|
15
|
+
| 'easeIn'
|
|
16
|
+
| 'easeOut'
|
|
17
|
+
| 'easeInOut'
|
|
18
|
+
| 'circIn'
|
|
19
|
+
| 'circOut'
|
|
20
|
+
| 'circInOut'
|
|
21
|
+
| 'backIn'
|
|
22
|
+
| 'backOut'
|
|
23
|
+
| 'backInOut'
|
|
24
|
+
| 'anticipate'
|
|
25
|
+
| [number, number, number, number]
|
|
26
|
+
|
|
27
|
+
/** How repeated legs play: restart, alternate direction, or alternate
|
|
28
|
+
* direction with inverted easing (identical to reverse for two keyframes). */
|
|
29
|
+
export type RepeatType = 'loop' | 'reverse' | 'mirror'
|
|
30
|
+
/** Numeric tree timing in seconds, applied to inherited animate labels. */
|
|
31
|
+
export interface ChildOrchestration {
|
|
32
|
+
staggerChildren?: number
|
|
33
|
+
delayChildren?: number
|
|
34
|
+
when?: 'beforeChildren' | 'afterChildren'
|
|
35
|
+
}
|
|
36
|
+
|
|
14
37
|
/** Timing is seconds. Spring velocity is units per second. */
|
|
15
|
-
export interface Transition {
|
|
38
|
+
export interface Transition extends ChildOrchestration {
|
|
16
39
|
type?: 'tween' | 'spring'
|
|
40
|
+
/** Tween duration; on springs this is the visual duration (combine with bounce). */
|
|
17
41
|
duration?: number
|
|
18
42
|
delay?: number
|
|
19
43
|
ease?: Ease
|
|
44
|
+
/** Additional iterations after the first leg; Infinity loops forever. */
|
|
45
|
+
repeat?: number
|
|
46
|
+
repeatType?: RepeatType
|
|
47
|
+
repeatDelay?: number
|
|
48
|
+
/** Spring bounciness 0–1; pairs with duration instead of stiffness/damping. */
|
|
49
|
+
bounce?: number
|
|
20
50
|
stiffness?: number
|
|
21
51
|
damping?: number
|
|
22
52
|
mass?: number
|
|
@@ -25,28 +55,87 @@ export interface Transition {
|
|
|
25
55
|
restDelta?: number
|
|
26
56
|
}
|
|
27
57
|
|
|
58
|
+
/** Per-channel transition overrides, upstream form: `{x: {…}, default: {…}}`. */
|
|
59
|
+
export type TransitionOrchestration = ChildOrchestration & {
|
|
60
|
+
default?: Transition
|
|
61
|
+
} & Partial<Record<MotionKey, Transition>>
|
|
62
|
+
|
|
63
|
+
/** A flat transition or per-channel overrides. */
|
|
64
|
+
export type TransitionInput = Transition | TransitionOrchestration
|
|
65
|
+
|
|
66
|
+
/** Labels resolve against this host's variants, in array order. */
|
|
67
|
+
export type VariantLabels = string | string[]
|
|
68
|
+
/** An inline numeric target or labels resolved by the host. */
|
|
69
|
+
export type AnimationDefinition = Target | VariantLabels
|
|
70
|
+
/** A numeric variant destination with an optional run transition override. */
|
|
71
|
+
export type VariantTarget = Target & { transition?: TransitionInput }
|
|
72
|
+
/** Host-local definitions; pure resolvers receive this host's custom value. */
|
|
73
|
+
export type Variants = Record<string, VariantTarget | ((custom: any) => VariantTarget)>
|
|
74
|
+
|
|
28
75
|
/** Static platform styles plus numeric motion-value bindings. */
|
|
29
76
|
export type MotionStyle = Record<string, unknown> & Partial<Record<MotionKey, number | MotionValue>>
|
|
77
|
+
/** Translation limits in CSS pixels/DIP. Omitted edges are unbounded. */
|
|
78
|
+
export interface DragConstraints {
|
|
79
|
+
left?: number
|
|
80
|
+
right?: number
|
|
81
|
+
top?: number
|
|
82
|
+
bottom?: number
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface DragInfo {
|
|
86
|
+
point: { x: number; y: number }
|
|
87
|
+
delta: { x: number; y: number }
|
|
88
|
+
offset: { x: number; y: number }
|
|
89
|
+
/** CSS pixels/DIP per second. */
|
|
90
|
+
velocity: { x: number; y: number }
|
|
91
|
+
cancelled: boolean
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type DragCallback = (event: any, info: DragInfo) => void
|
|
95
|
+
|
|
30
96
|
/** Supported declarative motion controls. */
|
|
31
97
|
export interface MotionProps {
|
|
32
|
-
|
|
98
|
+
drag?: boolean | 'x' | 'y'
|
|
99
|
+
/** Numeric translation bounds; measured ref constraints are deferred. */
|
|
100
|
+
dragConstraints?: DragConstraints
|
|
101
|
+
/** Project release velocity into a spring destination (default true). */
|
|
102
|
+
dragMomentum?: boolean
|
|
103
|
+
/** Resistance beyond bounds, 0–1 (default 0.35); false = 0, true = 0.35. */
|
|
104
|
+
dragElastic?: boolean | number
|
|
105
|
+
onDragStart?: DragCallback
|
|
106
|
+
onDrag?: DragCallback
|
|
107
|
+
onDragEnd?: DragCallback
|
|
108
|
+
/** Named numeric targets; array labels merge left to right. */
|
|
109
|
+
variants?: Variants
|
|
110
|
+
/** Passed to this host's variant resolvers; not inherited. */
|
|
111
|
+
custom?: any
|
|
112
|
+
initial?: AnimationDefinition | false
|
|
33
113
|
/** Destination while the nearest Presence boundary retains this host for exit. */
|
|
34
|
-
exit?:
|
|
35
|
-
animate?:
|
|
36
|
-
transition?:
|
|
114
|
+
exit?: AnimationDefinition
|
|
115
|
+
animate?: AnimationDefinition
|
|
116
|
+
transition?: TransitionInput
|
|
37
117
|
style?: MotionStyle
|
|
118
|
+
/** Target while the pointer is down (web: pointerdown; native: touch down). */
|
|
119
|
+
whileTap?: AnimationDefinition
|
|
120
|
+
whileTapTransition?: Transition
|
|
121
|
+
/** Target while the host holds focus. */
|
|
122
|
+
whileFocus?: AnimationDefinition
|
|
123
|
+
whileFocusTransition?: Transition
|
|
124
|
+
onAnimationStart?: (definition: AnimationDefinition) => void
|
|
38
125
|
onAnimationComplete?: () => void
|
|
126
|
+
/** Per-frame snapshot while any channel animates. */
|
|
127
|
+
onUpdate?: (latest: Target) => void
|
|
39
128
|
}
|
|
40
129
|
|
|
41
130
|
/** View layout and accessibility props with motion controls. */
|
|
42
131
|
export type MotionViewProps = Omit<ViewProps, 'style'> & MotionProps
|
|
43
132
|
/** Row layout and accessibility props with motion controls. */
|
|
44
|
-
export type MotionRowProps = Omit<
|
|
133
|
+
export type MotionRowProps = Omit<HStackProps, 'style'> & MotionProps
|
|
45
134
|
/** Pressable interaction props with motion controls. */
|
|
46
135
|
export type MotionPressableProps = Omit<PressableProps, 'style'> & MotionProps
|
|
47
136
|
/** Inherited defaults; `user` observes the live system preference. */
|
|
48
137
|
export interface MotionConfigProps {
|
|
49
|
-
transition?:
|
|
138
|
+
transition?: TransitionInput
|
|
50
139
|
reducedMotion?: 'always' | 'never' | 'user'
|
|
51
140
|
children?: any
|
|
52
141
|
}
|
package/src/value.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Clock } from './clock-types'
|
|
2
|
-
import type { Transition } from './types'
|
|
3
|
-
import { runAnimation, validateTransition, type AnimationControls } from './engine'
|
|
1
|
+
import type { Clock } from './clock-types.js'
|
|
2
|
+
import type { Transition } from './types.js'
|
|
3
|
+
import { runAnimation, validateTransition, type AnimationControls } from './engine.js'
|
|
4
4
|
|
|
5
5
|
/** Events supported by numeric motion values. */
|
|
6
6
|
export interface MotionValueEvents {
|
|
@@ -88,6 +88,15 @@ export class MotionValue {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
private write(value: number) {
|
|
91
|
+
this.track(value)
|
|
92
|
+
if (!this.disposed && value !== this.previous) {
|
|
93
|
+
this.emit('change', value)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Update position bookkeeping without emitting — platform-delegated runs
|
|
97
|
+
* track presentation state so interruption reads fresh value/velocity. */
|
|
98
|
+
track(value: number) {
|
|
99
|
+
this.assert(value)
|
|
91
100
|
if (this.disposed) {
|
|
92
101
|
return
|
|
93
102
|
}
|
|
@@ -101,12 +110,13 @@ export class MotionValue {
|
|
|
101
110
|
this.previous = this.current
|
|
102
111
|
this.current = value
|
|
103
112
|
this.updated = now
|
|
104
|
-
if (value !== this.previous) {
|
|
105
|
-
this.emit('change', value)
|
|
106
|
-
}
|
|
107
113
|
}
|
|
108
114
|
/** Animate to a destination. New playback replaces old playback. */
|
|
109
|
-
animate(
|
|
115
|
+
animate(
|
|
116
|
+
target: number,
|
|
117
|
+
transition: Transition = {},
|
|
118
|
+
clampSample?: (value: number) => number,
|
|
119
|
+
): AnimationControls {
|
|
110
120
|
this.assert(target)
|
|
111
121
|
validateTransition(transition)
|
|
112
122
|
if (this.disposed) {
|
|
@@ -122,7 +132,7 @@ export class MotionValue {
|
|
|
122
132
|
this.current,
|
|
123
133
|
target,
|
|
124
134
|
{ ...transition, velocity },
|
|
125
|
-
(value) => this.write(value),
|
|
135
|
+
(value) => this.write(clampSample ? clampSample(value) : value),
|
|
126
136
|
)
|
|
127
137
|
|
|
128
138
|
this.controls = controls
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { resolveVariant, playbackTransition, VariantNode } from './variants'
|
|
3
|
+
import type { AnimationResult } from './engine'
|
|
4
|
+
import type { TransitionInput } from './types'
|
|
5
|
+
|
|
6
|
+
function node(timing: TransitionInput = {}) {
|
|
7
|
+
const value = new VariantNode()
|
|
8
|
+
value.resolve = () => ({ target: { x: 10 }, transition: timing })
|
|
9
|
+
value.play = vi.fn(async () => 'finished' as AnimationResult)
|
|
10
|
+
value.stopOwn = vi.fn()
|
|
11
|
+
return value
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function pending() {
|
|
15
|
+
let resolve!: (result: AnimationResult) => void
|
|
16
|
+
const promise = new Promise<AnimationResult>((done) => {
|
|
17
|
+
resolve = done
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
return { promise, resolve }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('variant resolution', () => {
|
|
24
|
+
it('merges arrays left to right and keeps the last defined transition', () => {
|
|
25
|
+
expect(
|
|
26
|
+
resolveVariant(['missing', 'a', 'b', 'c'], {
|
|
27
|
+
a: { x: 10, opacity: 0, transition: { duration: 1 } },
|
|
28
|
+
b: { x: 20, transition: { duration: 2 } },
|
|
29
|
+
c: { y: 30 },
|
|
30
|
+
}),
|
|
31
|
+
).toEqual({ target: { x: 20, opacity: 0, y: 30 }, transition: { duration: 2 } })
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('passes custom to resolvers and validates the returned numeric target', () => {
|
|
35
|
+
expect(resolveVariant('a', { a: (custom) => ({ x: custom }) }, 7).target).toEqual({ x: 7 })
|
|
36
|
+
expect(() => resolveVariant('a', { a: () => ({ x: '10px' as any }) })).toThrow('finite numeric')
|
|
37
|
+
expect(() => resolveVariant('a', { a: { transition: { staggerChildren: -1 } } })).toThrow(
|
|
38
|
+
'staggerChildren',
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
expect(resolveVariant('toString', {}).target).toEqual({})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('strips parent timing and adds delay to each per-key override', () => {
|
|
45
|
+
expect(
|
|
46
|
+
playbackTransition(
|
|
47
|
+
{
|
|
48
|
+
default: { duration: 1, delay: 0.1 },
|
|
49
|
+
x: { delay: 0.2 },
|
|
50
|
+
delayChildren: 1,
|
|
51
|
+
staggerChildren: 2,
|
|
52
|
+
when: 'afterChildren',
|
|
53
|
+
},
|
|
54
|
+
0.3,
|
|
55
|
+
),
|
|
56
|
+
).toEqual({ default: { duration: 1, delay: 0.4 }, x: { delay: 0.5 } })
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe('variant orchestration', () => {
|
|
61
|
+
it('starts parent and children concurrently with stable stagger offsets', async () => {
|
|
62
|
+
const parent = node({ delayChildren: 0.1, staggerChildren: 0.2 })
|
|
63
|
+
const a = node(),
|
|
64
|
+
b = node()
|
|
65
|
+
|
|
66
|
+
parent.register(a)
|
|
67
|
+
parent.register(b)
|
|
68
|
+
const job = pending()
|
|
69
|
+
parent.play = vi.fn(() => job.promise)
|
|
70
|
+
const result = parent.run('show')
|
|
71
|
+
expect(a.play).toHaveBeenCalledWith('show', { x: 10 }, { delay: 0.1 })
|
|
72
|
+
expect(b.play).toHaveBeenCalledWith('show', { x: 10 }, { delay: 0.30000000000000004 })
|
|
73
|
+
job.resolve('finished')
|
|
74
|
+
expect(await result).toBe('finished')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('waits for actual parent completion before children', async () => {
|
|
78
|
+
const parent = node({ when: 'beforeChildren' }),
|
|
79
|
+
child = node()
|
|
80
|
+
|
|
81
|
+
parent.register(child)
|
|
82
|
+
const job = pending()
|
|
83
|
+
parent.play = vi.fn(() => job.promise)
|
|
84
|
+
const result = parent.run('show')
|
|
85
|
+
expect(child.play).not.toHaveBeenCalled()
|
|
86
|
+
job.resolve('finished')
|
|
87
|
+
expect(await result).toBe('finished')
|
|
88
|
+
expect(child.play).toHaveBeenCalledOnce()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('waits for children and cancels queued parent work on disposal', async () => {
|
|
92
|
+
const parent = node({ when: 'afterChildren' }),
|
|
93
|
+
child = node()
|
|
94
|
+
|
|
95
|
+
parent.register(child)
|
|
96
|
+
const job = pending()
|
|
97
|
+
child.play = vi.fn(() => job.promise)
|
|
98
|
+
const result = parent.run('show')
|
|
99
|
+
expect(parent.play).not.toHaveBeenCalled()
|
|
100
|
+
parent.stop()
|
|
101
|
+
job.resolve('finished')
|
|
102
|
+
expect(await result).toBe('cancelled')
|
|
103
|
+
expect(parent.play).not.toHaveBeenCalled()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('unregisters children and propagates cancellation through nested runs', async () => {
|
|
107
|
+
const parent = node(),
|
|
108
|
+
child = node(),
|
|
109
|
+
grandchild = node()
|
|
110
|
+
|
|
111
|
+
child.register(grandchild)
|
|
112
|
+
const off = parent.register(child)
|
|
113
|
+
await parent.run('show')
|
|
114
|
+
expect(grandchild.play).toHaveBeenCalledOnce()
|
|
115
|
+
off()
|
|
116
|
+
await parent.run('hide')
|
|
117
|
+
expect(child.play).toHaveBeenCalledOnce()
|
|
118
|
+
expect(child.stopOwn).toHaveBeenCalled()
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('starts the parent only after every child finishes successfully', async () => {
|
|
123
|
+
const parent = node({ when: 'afterChildren' })
|
|
124
|
+
const a = node(),
|
|
125
|
+
b = node()
|
|
126
|
+
|
|
127
|
+
parent.register(a)
|
|
128
|
+
parent.register(b)
|
|
129
|
+
const first = pending(),
|
|
130
|
+
second = pending()
|
|
131
|
+
|
|
132
|
+
a.play = vi.fn(() => first.promise)
|
|
133
|
+
b.play = vi.fn(() => second.promise)
|
|
134
|
+
const result = parent.run('show')
|
|
135
|
+
first.resolve('finished')
|
|
136
|
+
await Promise.resolve()
|
|
137
|
+
expect(parent.play).not.toHaveBeenCalled()
|
|
138
|
+
second.resolve('finished')
|
|
139
|
+
expect(await result).toBe('finished')
|
|
140
|
+
expect(parent.play).toHaveBeenCalledOnce()
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('does not launch an obsolete child phase after replacement', async () => {
|
|
144
|
+
const parent = node({ when: 'beforeChildren' }),
|
|
145
|
+
child = node()
|
|
146
|
+
|
|
147
|
+
parent.register(child)
|
|
148
|
+
const old = pending()
|
|
149
|
+
parent.play = vi.fn(() => old.promise)
|
|
150
|
+
const obsolete = parent.run('show')
|
|
151
|
+
parent.play = vi.fn(async () => 'finished')
|
|
152
|
+
await parent.run('hide')
|
|
153
|
+
old.resolve('finished')
|
|
154
|
+
expect(await obsolete).toBe('cancelled')
|
|
155
|
+
expect(child.play).toHaveBeenCalledTimes(1)
|
|
156
|
+
expect(child.play).toHaveBeenCalledWith('hide', { x: 10 }, { delay: 0 })
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('animates late children after the current parent run finishes', async () => {
|
|
160
|
+
const parent = node({ when: 'beforeChildren', delayChildren: 0.1 }),
|
|
161
|
+
child = node()
|
|
162
|
+
|
|
163
|
+
await parent.run('show')
|
|
164
|
+
parent.register(child)
|
|
165
|
+
await Promise.resolve()
|
|
166
|
+
expect(child.play).toHaveBeenCalledWith('show', { x: 10 }, { delay: 0.1 })
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('keeps independently started exits alive while invalidating queued tree work', async () => {
|
|
170
|
+
const parent = node({ when: 'beforeChildren' }),
|
|
171
|
+
child = node()
|
|
172
|
+
|
|
173
|
+
parent.register(child)
|
|
174
|
+
const job = pending()
|
|
175
|
+
parent.play = vi.fn(() => job.promise)
|
|
176
|
+
const run = parent.run('show')
|
|
177
|
+
const stops = (child.stopOwn as ReturnType<typeof vi.fn>).mock.calls.length
|
|
178
|
+
parent.cancelPending()
|
|
179
|
+
job.resolve('finished')
|
|
180
|
+
expect(await run).toBe('cancelled')
|
|
181
|
+
expect(child.stopOwn).toHaveBeenCalledTimes(stops)
|
|
182
|
+
expect(child.play).not.toHaveBeenCalled()
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
it('does not count a late child twice while waiting for the parent phase', async () => {
|
|
186
|
+
const parent = node({ when: 'beforeChildren' }),
|
|
187
|
+
child = node()
|
|
188
|
+
|
|
189
|
+
const job = pending()
|
|
190
|
+
parent.play = vi.fn(() => job.promise)
|
|
191
|
+
const run = parent.run('show')
|
|
192
|
+
parent.register(child)
|
|
193
|
+
job.resolve('finished')
|
|
194
|
+
await run
|
|
195
|
+
await Promise.resolve()
|
|
196
|
+
expect(child.play).toHaveBeenCalledOnce()
|
|
197
|
+
})
|
package/src/variants.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { validateTarget } from './controller'
|
|
2
|
+
import { isOrchestrated, validateTransitionInput } from './engine'
|
|
3
|
+
import type { AnimationResult } from './engine'
|
|
4
|
+
import type {
|
|
5
|
+
AnimationDefinition,
|
|
6
|
+
MotionKey,
|
|
7
|
+
Target,
|
|
8
|
+
TransitionInput,
|
|
9
|
+
Variants,
|
|
10
|
+
VariantLabels,
|
|
11
|
+
} from './types'
|
|
12
|
+
|
|
13
|
+
export function isLabels(value: unknown): value is VariantLabels {
|
|
14
|
+
return (
|
|
15
|
+
typeof value === 'string' ||
|
|
16
|
+
(Array.isArray(value) && value.every((label) => typeof label === 'string'))
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resolveVariant(
|
|
21
|
+
definition: AnimationDefinition | false | undefined,
|
|
22
|
+
variants?: Variants,
|
|
23
|
+
custom?: any,
|
|
24
|
+
) {
|
|
25
|
+
const target: Target = {}
|
|
26
|
+
let transition: TransitionInput | undefined
|
|
27
|
+
if (definition === undefined || definition === false) {
|
|
28
|
+
return { target, transition }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const entries = isLabels(definition)
|
|
32
|
+
? (typeof definition === 'string' ? [definition] : definition).map((label) =>
|
|
33
|
+
Object.hasOwn(variants ?? {}, label) ? variants![label] : undefined,
|
|
34
|
+
)
|
|
35
|
+
: [definition]
|
|
36
|
+
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
const resolved = typeof entry === 'function' ? entry(custom) : entry
|
|
39
|
+
if (!resolved) {
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const { transition: override, ...values } = resolved as Target & {
|
|
44
|
+
transition?: TransitionInput
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
validateTarget(values)
|
|
48
|
+
Object.assign(target, values)
|
|
49
|
+
if (override !== undefined) {
|
|
50
|
+
validateTransitionInput(override)
|
|
51
|
+
transition = override
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { target, transition }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Keep tree timing out of Controller and add inherited delay to every channel. */
|
|
59
|
+
export function playbackTransition(input: TransitionInput, delay = 0): TransitionInput {
|
|
60
|
+
if (isOrchestrated(input)) {
|
|
61
|
+
const {
|
|
62
|
+
staggerChildren: _staggerChildren,
|
|
63
|
+
delayChildren: _delayChildren,
|
|
64
|
+
when: _when,
|
|
65
|
+
...timing
|
|
66
|
+
} = input
|
|
67
|
+
|
|
68
|
+
const result: TransitionInput = {
|
|
69
|
+
default: { ...timing.default, delay: (timing.default?.delay ?? 0) + delay },
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (const [key, value] of Object.entries(timing)) {
|
|
73
|
+
if (key !== 'default' && value) {
|
|
74
|
+
result[key as MotionKey] = {
|
|
75
|
+
...value,
|
|
76
|
+
delay: (value.delay ?? timing.default?.delay ?? 0) + delay,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return result
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const {
|
|
85
|
+
staggerChildren: _staggerChildren,
|
|
86
|
+
delayChildren: _delayChildren,
|
|
87
|
+
when: _when,
|
|
88
|
+
...timing
|
|
89
|
+
} = input
|
|
90
|
+
|
|
91
|
+
return { ...timing, delay: (timing.delay ?? 0) + delay }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Root-local tree of inherited runs. Generations prevent queued work after replacement/disposal. */
|
|
95
|
+
export class VariantNode {
|
|
96
|
+
readonly children = new Set<VariantNode>()
|
|
97
|
+
private generation = 0
|
|
98
|
+
private active?: {
|
|
99
|
+
definition: AnimationDefinition
|
|
100
|
+
finished: Promise<AnimationResult>
|
|
101
|
+
generation: number
|
|
102
|
+
}
|
|
103
|
+
initial?: VariantLabels | false
|
|
104
|
+
animate?: VariantLabels
|
|
105
|
+
resolve!: (definition: AnimationDefinition) => { target: Target; transition: TransitionInput }
|
|
106
|
+
play!: (
|
|
107
|
+
definition: AnimationDefinition,
|
|
108
|
+
target: Target,
|
|
109
|
+
transition: TransitionInput,
|
|
110
|
+
) => Promise<AnimationResult>
|
|
111
|
+
stopOwn!: () => void
|
|
112
|
+
|
|
113
|
+
register(child: VariantNode) {
|
|
114
|
+
this.children.add(child)
|
|
115
|
+
const active = this.active
|
|
116
|
+
if (active && isLabels(active.definition)) {
|
|
117
|
+
void active.finished.then((result) => {
|
|
118
|
+
if (
|
|
119
|
+
result !== 'finished' ||
|
|
120
|
+
active.generation !== this.generation ||
|
|
121
|
+
!this.children.has(child)
|
|
122
|
+
) {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const { transition } = this.resolve(active.definition)
|
|
127
|
+
void child.run(
|
|
128
|
+
active.definition,
|
|
129
|
+
(transition.delayChildren ?? 0) +
|
|
130
|
+
[...this.children].indexOf(child) * (transition.staggerChildren ?? 0),
|
|
131
|
+
)
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return () => {
|
|
136
|
+
this.children.delete(child)
|
|
137
|
+
child.stop()
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Invalidate queued phases without stopping independently registered Presence exits. */
|
|
142
|
+
cancelPending() {
|
|
143
|
+
this.generation++
|
|
144
|
+
for (const child of this.children) {
|
|
145
|
+
child.cancelPending()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
stop() {
|
|
150
|
+
this.generation++
|
|
151
|
+
this.stopOwn?.()
|
|
152
|
+
for (const child of this.children) {
|
|
153
|
+
child.stop()
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
run(definition: AnimationDefinition, delay = 0): Promise<AnimationResult> {
|
|
158
|
+
this.animate = isLabels(definition) ? definition : undefined
|
|
159
|
+
this.stop()
|
|
160
|
+
const generation = this.generation
|
|
161
|
+
const finished = this.runTree(definition, delay, generation)
|
|
162
|
+
this.active = { definition, finished, generation }
|
|
163
|
+
return finished
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private async runTree(
|
|
167
|
+
definition: AnimationDefinition,
|
|
168
|
+
delay: number,
|
|
169
|
+
generation: number,
|
|
170
|
+
): Promise<AnimationResult> {
|
|
171
|
+
const { target, transition } = this.resolve(definition)
|
|
172
|
+
const participants = [...this.children]
|
|
173
|
+
const own = () =>
|
|
174
|
+
this.play(
|
|
175
|
+
definition,
|
|
176
|
+
target,
|
|
177
|
+
playbackTransition(
|
|
178
|
+
transition,
|
|
179
|
+
transition.when === 'afterChildren' && participants.length && isLabels(definition)
|
|
180
|
+
? 0
|
|
181
|
+
: delay,
|
|
182
|
+
),
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
const children = () =>
|
|
186
|
+
Promise.all(
|
|
187
|
+
isLabels(definition)
|
|
188
|
+
? participants
|
|
189
|
+
.filter((child) => this.children.has(child))
|
|
190
|
+
.map((child, index) =>
|
|
191
|
+
child.run(
|
|
192
|
+
definition,
|
|
193
|
+
(transition.when === 'beforeChildren' ? 0 : delay) +
|
|
194
|
+
(transition.delayChildren ?? 0) +
|
|
195
|
+
index * (transition.staggerChildren ?? 0),
|
|
196
|
+
),
|
|
197
|
+
)
|
|
198
|
+
: [],
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
let results: AnimationResult[]
|
|
202
|
+
if (transition.when === 'beforeChildren') {
|
|
203
|
+
const result = await own()
|
|
204
|
+
if (generation !== this.generation || result !== 'finished') {
|
|
205
|
+
return 'cancelled'
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
results = [result, ...(await children())]
|
|
209
|
+
} else if (transition.when === 'afterChildren') {
|
|
210
|
+
results = await children()
|
|
211
|
+
if (generation !== this.generation || results.some((result) => result !== 'finished')) {
|
|
212
|
+
return 'cancelled'
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
results.push(await own())
|
|
216
|
+
} else {
|
|
217
|
+
const parent = own()
|
|
218
|
+
const descendants = children()
|
|
219
|
+
results = [await parent, ...(await descendants)]
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return generation === this.generation && results.every((result) => result === 'finished')
|
|
223
|
+
? 'finished'
|
|
224
|
+
: 'cancelled'
|
|
225
|
+
}
|
|
226
|
+
}
|
package/types/engine.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Clock } from './clock-types';
|
|
2
|
-
import type { Transition } from './types';
|
|
1
|
+
import type { Clock } from './clock-types.js';
|
|
2
|
+
import type { Transition, TransitionInput, TransitionOrchestration, MotionKey } from './types.js';
|
|
3
3
|
/** Terminal status; cancellation never masquerades as completion. */
|
|
4
4
|
export type AnimationResult = 'finished' | 'cancelled' | 'replaced';
|
|
5
5
|
/** A cancellable animation whose finished promise always settles. */
|
|
@@ -7,5 +7,11 @@ export interface AnimationControls {
|
|
|
7
7
|
finished: Promise<AnimationResult>;
|
|
8
8
|
stop(reason?: Exclude<AnimationResult, 'finished'>): void;
|
|
9
9
|
}
|
|
10
|
+
export declare function isBezierEase(ease: Transition['ease']): boolean;
|
|
11
|
+
/** CSS cubic-bezier control points for a delegatable ease; tuples pass through. */
|
|
12
|
+
export declare function bezierPoints(ease: Transition['ease']): [number, number, number, number];
|
|
13
|
+
export declare function isOrchestrated(t: TransitionInput | undefined): t is TransitionOrchestration;
|
|
14
|
+
export declare function resolveTransition(t: TransitionInput, key: MotionKey): Transition;
|
|
10
15
|
export declare function validateTransition(t: Transition): void;
|
|
16
|
+
export declare function validateTransitionInput(t: TransitionInput): void;
|
|
11
17
|
export declare function runAnimation(clock: Clock, from: number, to: number, transition: Transition, update: (value: number) => void, complete?: () => void): AnimationControls;
|