Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sequences work again on reanimated 2.3+ #200

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 57 additions & 51 deletions packages/core/src/use-map-animate-to-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,61 @@ function animationConfig<Animate>(
}
}

const getSequenceArray = (
sequenceKey: string,
sequenceArray: SequenceItem<any>[],
delayMs: number | undefined,
config: {},
animation: (...props: any) => any,
callback: (completed: boolean, value?: any) => void
) => {
'worklet'

const sequence: any[] = []

for (const step of sequenceArray) {
const shouldPush =
typeof step === 'object'
? step && step?.value != null && step?.value !== false
: step != null && step !== false
if (shouldPush) {
let stepDelay = delayMs
let stepValue = step
let stepConfig = Object.assign({}, config)
let stepAnimation = animation
if (typeof step === 'object') {
// not allowed in Reanimated: { delay, value, ...transition } = step
const stepTransition = Object.assign({}, step)

delete stepTransition.delay
delete stepTransition.value

const { config: inlineStepConfig, animation } = animationConfig(
sequenceKey,
stepTransition
)

stepConfig = Object.assign({}, stepConfig, inlineStepConfig)
stepAnimation = animation

if (step.delay != null) {
stepDelay = step.delay
}
stepValue = step.value
}

const sequenceValue = stepAnimation(stepValue, stepConfig, callback)
if (stepDelay != null) {
sequence.push(withDelay(stepDelay, sequenceValue))
} else {
sequence.push(sequenceValue)
}
}
}

return sequence
}

export function useMotify<Animate>({
animate: animateProp,
from: fromProp = false,
Expand Down Expand Up @@ -385,55 +440,6 @@ export function useMotify<Animate>({
continue
}

const getSequenceArray = (
sequenceKey: string,
sequenceArray: SequenceItem<any>[]
) => {
const sequence: any[] = []

for (const step of sequenceArray) {
const shouldPush =
typeof step === 'object'
? step && step?.value != null && step?.value !== false
: step != null && step !== false
if (shouldPush) {
let stepDelay = delayMs
let stepValue = step
let stepConfig = Object.assign({}, config)
let stepAnimation = animation
if (typeof step === 'object') {
// not allowed in Reanimated: { delay, value, ...transition } = step
const stepTransition = Object.assign({}, step)

delete stepTransition.delay
delete stepTransition.value

const { config: inlineStepConfig, animation } = animationConfig(
sequenceKey,
stepTransition
)

stepConfig = Object.assign({}, stepConfig, inlineStepConfig)
stepAnimation = animation

if (step.delay != null) {
stepDelay = step.delay
}
stepValue = step.value
}

const sequenceValue = stepAnimation(stepValue, stepConfig, callback)
if (stepDelay != null) {
sequence.push(withDelay(stepDelay, sequenceValue))
} else {
sequence.push(sequenceValue)
}
}
}

return sequence
}

if (key === 'transform') {
if (!Array.isArray(value)) {
console.error(
Expand All @@ -448,7 +454,7 @@ export function useMotify<Animate>({

if (Array.isArray(transformValue)) {
// we have a sequence in this transform...
const sequence = getSequenceArray(transformKey, transformValue)
const sequence = getSequenceArray(transformKey, transformValue, delayMs, config, animation, callback)

if (sequence.length) {
let finalValue = withSequence(sequence[0], ...sequence.slice(1))
Expand Down Expand Up @@ -485,7 +491,7 @@ export function useMotify<Animate>({
} else if (Array.isArray(value)) {
// we have a sequence

const sequence = getSequenceArray(key, value)
const sequence = getSequenceArray(key, value, delayMs, config, animation, callback)
let finalValue = withSequence(sequence[0], ...sequence.slice(1))
if (shouldRepeat) {
finalValue = withRepeat(finalValue, repeatCount, repeatReverse)
Expand Down