From e115c28ff8e7466a2b6a055dcbf36b1245a5e852 Mon Sep 17 00:00:00 2001 From: Justin Stanley Date: Thu, 19 May 2022 15:19:05 -0500 Subject: [PATCH] fix: sequences work again on reanimated 2.3+ See: https://github.com/nandorojo/moti/issues/195 --- packages/core/src/use-map-animate-to-style.ts | 108 +++++++++--------- 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/packages/core/src/use-map-animate-to-style.ts b/packages/core/src/use-map-animate-to-style.ts index dce4aa1..9e9d722 100644 --- a/packages/core/src/use-map-animate-to-style.ts +++ b/packages/core/src/use-map-animate-to-style.ts @@ -220,6 +220,61 @@ function animationConfig( } } +const getSequenceArray = ( + sequenceKey: string, + sequenceArray: SequenceItem[], + 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: animateProp, from: fromProp = false, @@ -385,55 +440,6 @@ export function useMotify({ continue } - const getSequenceArray = ( - sequenceKey: string, - sequenceArray: SequenceItem[] - ) => { - 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( @@ -448,7 +454,7 @@ export function useMotify({ 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)) @@ -485,7 +491,7 @@ export function useMotify({ } 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)