-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScript for Layout Animations (#2137)
## Description Added types for LayoutAnimations ## Checklist - [x] Updated TS types - [X] Added TS types tests - [x] Ensured that CI passes
- Loading branch information
Showing
19 changed files
with
1,030 additions
and
713 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
declare const _WORKLET: boolean; | ||
declare const _stopObservingProgress: (tag: number, flag: boolean) => void; | ||
declare const _startObservingProgress: (tag: number, flag: boolean) => void; | ||
declare namespace NodeJS { | ||
interface Global { | ||
LayoutAnimationRepository: { | ||
configs: Record<string, unknown>; | ||
registerConfig(tag: number, config: Record<string, unknown>): void; | ||
removeConfig(tag: number): void; | ||
startAnimationForTag( | ||
tag: number, | ||
type: unknown, | ||
yogaValues: unknown | ||
): void; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
src/reanimated2/layoutReanimation/animationBuilder/BaseAnimationBuilder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
import { withDelay, withTiming, withSpring } from '../../animations'; | ||
import { | ||
EntryExitAnimationFunction, | ||
AnimationFunction, | ||
BaseBuilderAnimationConfig, | ||
LayoutAnimationAndConfig, | ||
EntryExitAnimationBuild, | ||
} from './commonTypes'; | ||
import { EasingFn } from '../../Easing'; | ||
|
||
export class BaseAnimationBuilder { | ||
durationV?: number; | ||
easingV?: EasingFn; | ||
delayV?: number; | ||
rotateV?: string; | ||
type?: AnimationFunction; | ||
dampingV?: number; | ||
massV?: number; | ||
stiffnessV?: number; | ||
overshootClampingV?: number; | ||
restDisplacementThresholdV?: number; | ||
restSpeedThresholdV?: number; | ||
|
||
static createInstance: () => BaseAnimationBuilder; | ||
build: EntryExitAnimationBuild; | ||
|
||
static duration(durationMs: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.duration(durationMs); | ||
} | ||
|
||
duration(durationMs: number): BaseAnimationBuilder { | ||
this.durationV = durationMs; | ||
return this; | ||
} | ||
|
||
static easing(easingFunction: EasingFn): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.easing(easingFunction); | ||
} | ||
|
||
easing(easingFunction: EasingFn): BaseAnimationBuilder { | ||
this.easingV = easingFunction; | ||
return this; | ||
} | ||
|
||
static delay(delayMs: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.delay(delayMs); | ||
} | ||
|
||
delay(delayMs: number): BaseAnimationBuilder { | ||
this.delayV = delayMs; | ||
return this; | ||
} | ||
|
||
static rotate(degree: string): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.rotate(degree); | ||
} | ||
|
||
rotate(degree: string): BaseAnimationBuilder { | ||
this.rotateV = degree; | ||
return this; | ||
} | ||
|
||
static springify(): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.springify(); | ||
} | ||
|
||
springify(): BaseAnimationBuilder { | ||
this.type = withSpring as AnimationFunction; | ||
return this; | ||
} | ||
|
||
static damping(damping: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.damping(damping); | ||
} | ||
|
||
damping(damping: number): BaseAnimationBuilder { | ||
this.dampingV = damping; | ||
return this; | ||
} | ||
|
||
static mass(mass: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.mass(mass); | ||
} | ||
|
||
mass(mass: number): BaseAnimationBuilder { | ||
this.massV = mass; | ||
return this; | ||
} | ||
|
||
static stiffness(stiffness: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.stiffness(stiffness); | ||
} | ||
|
||
stiffness(stiffness: number): BaseAnimationBuilder { | ||
this.stiffnessV = stiffness; | ||
return this; | ||
} | ||
|
||
static overshootClamping(overshootClamping: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.overshootClamping(overshootClamping); | ||
} | ||
|
||
overshootClamping(overshootClamping: number): BaseAnimationBuilder { | ||
this.overshootClampingV = overshootClamping; | ||
return this; | ||
} | ||
|
||
static restDisplacementThreshold( | ||
restDisplacementThreshold: number | ||
): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.restDisplacementThreshold(restDisplacementThreshold); | ||
} | ||
|
||
restDisplacementThreshold( | ||
restDisplacementThreshold: number | ||
): BaseAnimationBuilder { | ||
this.restDisplacementThresholdV = restDisplacementThreshold; | ||
return this; | ||
} | ||
|
||
static restSpeedThreshold(restSpeedThreshold: number): BaseAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.restSpeedThreshold(restSpeedThreshold); | ||
} | ||
|
||
restSpeedThreshold(restSpeedThreshold: number): BaseAnimationBuilder { | ||
this.restSpeedThresholdV = restSpeedThreshold; | ||
return this; | ||
} | ||
|
||
static build(): EntryExitAnimationFunction { | ||
const instance = this.createInstance(); | ||
return instance.build(); | ||
} | ||
|
||
getDelayFunction(): AnimationFunction { | ||
const delay = this.delayV; | ||
return delay | ||
? withDelay | ||
: (_, animation) => { | ||
'worklet'; | ||
return animation; | ||
}; | ||
} | ||
|
||
getAnimationAndConfig(): LayoutAnimationAndConfig { | ||
const duration = this.durationV; | ||
const easing = this.easingV; | ||
const rotate = this.rotateV; | ||
const type = this.type ? this.type : (withTiming as AnimationFunction); | ||
const damping = this.dampingV; | ||
const mass = this.massV; | ||
const stiffness = this.stiffnessV; | ||
const overshootClamping = this.overshootClampingV; | ||
const restDisplacementThreshold = this.restDisplacementThresholdV; | ||
const restSpeedThreshold = this.restSpeedThresholdV; | ||
|
||
const animation = type; | ||
|
||
const config: BaseBuilderAnimationConfig = {}; | ||
|
||
if (type === withTiming) { | ||
if (easing) { | ||
config.easing = easing; | ||
} | ||
if (duration) { | ||
config.duration = duration; | ||
} | ||
if (rotate) { | ||
config.rotate = rotate; | ||
} | ||
} else { | ||
if (damping) { | ||
config.damping = damping; | ||
} | ||
if (mass) { | ||
config.mass = mass; | ||
} | ||
if (stiffness) { | ||
config.stiffness = stiffness; | ||
} | ||
if (overshootClamping) { | ||
config.overshootClamping = overshootClamping; | ||
} | ||
if (restDisplacementThreshold) { | ||
config.restDisplacementThreshold = restDisplacementThreshold; | ||
} | ||
if (restSpeedThreshold) { | ||
config.restSpeedThreshold = restSpeedThreshold; | ||
} | ||
if (rotate) { | ||
config.rotate = rotate; | ||
} | ||
} | ||
return [animation, config]; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/reanimated2/layoutReanimation/animationBuilder/BaseBounceAnimationBuilder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { withDelay, withTiming } from '../../animations'; | ||
import { | ||
EntryExitAnimationFunction, | ||
AnimationFunction, | ||
LayoutAnimationAndConfig, | ||
BounceBuilderAnimationConfig, | ||
EntryExitAnimationBuild, | ||
} from './commonTypes'; | ||
|
||
export class BaseBounceAnimationBuilder { | ||
durationV: number; | ||
delayV: number; | ||
|
||
static createInstance: () => BaseBounceAnimationBuilder; | ||
build: EntryExitAnimationBuild; | ||
|
||
static duration(durationMs: number): BaseBounceAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.duration(durationMs); | ||
} | ||
|
||
duration(durationMs: number): BaseBounceAnimationBuilder { | ||
this.durationV = durationMs; | ||
return this; | ||
} | ||
|
||
static delay(delayMs: number): BaseBounceAnimationBuilder { | ||
const instance = this.createInstance(); | ||
return instance.delay(delayMs); | ||
} | ||
|
||
delay(delayMs: number): BaseBounceAnimationBuilder { | ||
this.delayV = delayMs; | ||
return this; | ||
} | ||
|
||
getDelayFunction(): AnimationFunction { | ||
const delay = this.delayV; | ||
return delay | ||
? withDelay | ||
: (_, animation) => { | ||
'worklet'; | ||
return animation; | ||
}; | ||
} | ||
|
||
getAnimationAndConfig(): LayoutAnimationAndConfig { | ||
const duration = this.durationV; | ||
const type = withTiming; | ||
const animation = type; | ||
|
||
const config: BounceBuilderAnimationConfig = {}; | ||
|
||
if (duration) { | ||
config.duration = duration; | ||
} | ||
|
||
return [animation, config]; | ||
} | ||
|
||
static build(): EntryExitAnimationFunction { | ||
const instance = this.createInstance(); | ||
return instance.build(); | ||
} | ||
} |
Oops, something went wrong.