Skip to content

Commit 4544b0e

Browse files
authored
types: reorganize animation types (#15)
1 parent fb1d708 commit 4544b0e

File tree

19 files changed

+144
-111
lines changed

19 files changed

+144
-111
lines changed

packages/effekt/src/animation/animation.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type {
1010
AnimationDriver,
1111
AnimationPropertyNames,
1212
AnimationEventNames,
13+
AnimationEffect,
14+
AnimationPlayState,
1315
GeneratedKeyframe,
16+
WebAnimation,
1417
} from './types'
1518

1619
export function createAnimation(
@@ -23,11 +26,11 @@ export function createAnimation(
2326
driver,
2427
} = options
2528

26-
const animations: globalThis.Animation[] = []
29+
const animations: WebAnimation[] = []
2730
let isCompleted: boolean = false
2831
let isDriver: boolean = driver ? true : false
2932

30-
let resolve: (value: globalThis.Animation[]) => void
33+
let resolve: (value: WebAnimation[]) => void
3134
let reject: (value: any) => void
3235

3336
const els = getElements(targets)
@@ -73,36 +76,31 @@ export function createAnimation(
7376

7477
let isReady: boolean = els.length > 0
7578

76-
const each = (callback: (a: globalThis.Animation) => void): void => {
79+
const each = (callback: (a: WebAnimation) => void): void => {
7780
for (let i = 0, l = animations.length; i < l; i++) callback(animations[i])
7881
}
7982

80-
const set = <
81-
T extends globalThis.Animation,
82-
K extends AnimationPropertyNames,
83-
>(
83+
const set = <T extends WebAnimation, K extends AnimationPropertyNames>(
8484
name: AnimationPropertyNames,
8585
value: T[K],
8686
): void => each((k) => (k[name] = value as any))
8787

8888
const run = (name: AnimationEventNames): void => each((a) => a[name]())
8989

90-
const call = (
91-
callback?: (animations: globalThis.Animation[]) => void,
92-
): void => {
90+
const call = (callback?: (animations: WebAnimation[]) => void): void => {
9391
if (isReady) callback?.(animations)
9492
}
9593

96-
const numberish = (v: globalThis.CSSNumberish): number =>
94+
const numberish = (v: globalThis.CSSNumberish | number): number =>
9795
isNumber(v) ? v : (v as globalThis.CSSUnitValue).value
9896

99-
const getAnimation = (): globalThis.Animation | null => {
97+
const getAnimation = (): WebAnimation | null => {
10098
if (isReady) {
10199
if (isDriver) return animations[0]
102100
return animations.reduce((prev, curr) => {
103-
const pT = prev.effect?.getComputedTiming().endTime as number
104-
const cT = curr.effect?.getComputedTiming().endTime as number
105-
return numberish(pT) > numberish(cT) ? prev : curr
101+
const pT = prev.effect?.getComputedTiming().endTime
102+
const cT = curr.effect?.getComputedTiming().endTime
103+
return numberish(pT!) > numberish(cT!) ? prev : curr
106104
})
107105
}
108106
return null
@@ -172,7 +170,7 @@ export function createAnimation(
172170
set playRate(r) {
173171
set('playbackRate', r)
174172
},
175-
get effect(): globalThis.AnimationEffect | null {
173+
get effect(): AnimationEffect | null {
176174
return instance.value?.effect || null
177175
},
178176
set effect(e) {
@@ -185,7 +183,7 @@ export function createAnimation(
185183
isDriver ||= true
186184
set('timeline', d)
187185
},
188-
get playState(): Readonly<globalThis.AnimationPlayState> {
186+
get playState(): Readonly<AnimationPlayState> {
189187
return instance.value?.playState || 'idle'
190188
},
191189
get progress(): number {

packages/effekt/src/animation/generate-keyframes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@/shared'
1010
import type {
1111
AnimationOptions,
12-
AnimationKeyframes,
12+
AnimationOptionsKeyframes,
1313
GeneratedKeyframe,
1414
} from './types'
1515

@@ -25,7 +25,7 @@ const rgxIsTransform = composeRegex(
2525
)
2626

2727
const parseObjectValue = (
28-
value: AnimationKeyframes[keyof AnimationKeyframes],
28+
value: AnimationOptionsKeyframes[keyof AnimationOptionsKeyframes],
2929
) => (isObject(value) && !isArray(value) && value) || { value }
3030

3131
const setUnit = (key: string, value: number | string, prop: string): string => {
@@ -77,7 +77,7 @@ export function generateKeyframes(
7777
...props
7878
} = options
7979

80-
const keys = Object.keys(props) as (keyof AnimationKeyframes)[]
80+
const keys = Object.keys(props) as (keyof AnimationOptionsKeyframes)[]
8181
const keyframes: GeneratedKeyframe[] = []
8282
const transforms: GeneratedKeyframe[] = []
8383
const effect = {
@@ -131,7 +131,7 @@ export function generateKeyframes(
131131
}
132132
}
133133

134-
transforms.sort((a, b) => (a.duration as number) - (b.duration as number))
134+
transforms.sort((a, b) => a.duration! - b.duration!)
135135

136136
return [...transforms, ...keyframes]
137137
}

packages/effekt/src/animation/types/animation.ts renamed to packages/effekt/src/animation/types/animation/animation.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import type { AnimationPromise } from './options'
2-
import type { AnimationDriver } from './effect'
1+
import type {
2+
AnimationPromise,
3+
AnimationEffect,
4+
AnimationDriver,
5+
AnimationPlayState,
6+
} from './shared'
37

48
export interface Animation {
59
play(): void
@@ -15,11 +19,11 @@ export interface Animation {
1519
set time(t: number)
1620
get playRate(): number
1721
set playRate(r: number)
18-
get effect(): globalThis.AnimationEffect | null
19-
set effect(e: globalThis.AnimationEffect | null)
22+
get effect(): AnimationEffect
23+
set effect(e: AnimationEffect)
2024
get driver(): AnimationDriver
2125
set driver(d: AnimationDriver)
22-
get playState(): Readonly<globalThis.AnimationPlayState>
26+
get playState(): Readonly<AnimationPlayState>
2327
get progress(): number
2428
set progress(t: number)
2529
get isCompleted(): Readonly<boolean>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './shared'
2+
export * from './animation'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export type AnimationTargets =
2+
| string
3+
| Element
4+
| null
5+
| (Element | null)[]
6+
| NodeListOf<Element>
7+
8+
export type AnimationPromise = Promise<WebAnimation[]>
9+
10+
export type AnimationDriver = globalThis.AnimationTimeline | null
11+
12+
export type AnimationEffect = globalThis.AnimationEffect | null
13+
14+
export type AnimationPlayState = globalThis.AnimationPlayState
15+
16+
export type AnimationPropertyNames =
17+
| 'startTime'
18+
| 'currentTime'
19+
| 'playbackRate'
20+
| 'effect'
21+
| 'timeline'
22+
23+
export type AnimationEventNames =
24+
| 'play'
25+
| 'pause'
26+
| 'reverse'
27+
| 'cancel'
28+
| 'finish'
29+
30+
export type WebAnimation = globalThis.Animation
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
export * from './targets'
2-
export * from './effect'
3-
export * from './keyframes'
4-
export * from './events'
51
export * from './options'
62
export * from './animation'

packages/effekt/src/animation/types/keyframes/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/effekt/src/animation/types/keyframes/options.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import type { AnimationEffect } from './effect'
2-
import type { AnimationKeyframes } from './keyframes'
3-
import type { AnimationEvents } from './events'
4-
5-
export interface AnimationOptions
6-
extends AnimationKeyframes,
7-
AnimationEvents,
8-
AnimationEffect {
1+
export interface AnimationOptionsCustom {
92
/**
103
* Specifies whether the animation will play automatically when `animate()` is called.
114
*
@@ -34,19 +27,3 @@ export interface AnimationOptions
3427
*/
3528
offset?: number | (number | null)[]
3629
}
37-
38-
export type AnimationPropertyNames =
39-
| 'startTime'
40-
| 'currentTime'
41-
| 'playbackRate'
42-
| 'effect'
43-
| 'timeline'
44-
45-
export type AnimationEventNames =
46-
| 'play'
47-
| 'pause'
48-
| 'reverse'
49-
| 'cancel'
50-
| 'finish'
51-
52-
export type AnimationPromise = Promise<globalThis.Animation[]>

packages/effekt/src/animation/types/effect.ts renamed to packages/effekt/src/animation/types/options/effect.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import type { DelayFunction, EasingFunction } from '@/shared/types'
2+
import type {
3+
PlaybackDirection,
4+
FillMode,
5+
CompositeOperation,
6+
CompositeOperationOrAuto,
7+
RepeatCompositeOperation,
8+
} from './shared'
9+
import type { AnimationDriver } from '../animation'
210

3-
export interface AnimationEffect {
11+
export interface AnimationOptionsEffect {
412
/**
513
* Specifies a unique property that references the animation.
614
*
@@ -17,7 +25,7 @@ export interface AnimationEffect {
1725
*
1826
* @default undefined
1927
*/
20-
direction?: globalThis.PlaybackDirection
28+
direction?: PlaybackDirection
2129
/**
2230
* Specifies the playback rate of the animation.
2331
*
@@ -71,7 +79,7 @@ export interface AnimationEffect {
7179
*
7280
* @default 'both'
7381
*/
74-
fillMode?: globalThis.FillMode
82+
fillMode?: FillMode
7583
/**
7684
* Specifies the mathematical function used in the interpolation between the `start` and `end` keyframes.
7785
*
@@ -83,15 +91,13 @@ export interface AnimationEffect {
8391
*
8492
* @default undefined
8593
*/
86-
composite?:
87-
| globalThis.CompositeOperation
88-
| globalThis.CompositeOperationOrAuto[]
94+
composite?: CompositeOperation | CompositeOperationOrAuto[]
8995
/**
9096
* Determines how values build from iteration to iteration in this animation
9197
*
9298
* @default undefined
9399
*/
94-
repeatComposite?: globalThis.IterationCompositeOperation
100+
repeatComposite?: RepeatCompositeOperation
95101
/**
96102
* Specifies a pseudo-element selector, such as `::before`.
97103
*
@@ -119,5 +125,3 @@ export interface AnimationEffect {
119125
*/
120126
end?: string
121127
}
122-
123-
export type AnimationDriver = globalThis.AnimationTimeline | null

0 commit comments

Comments
 (0)