Skip to content

Commit

Permalink
Merge pull request #233 from nandorojo/transition
Browse files Browse the repository at this point in the history
Configure `transition` per-variant
  • Loading branch information
nandorojo authored Nov 14, 2022
2 parents 6705aa9 + dc6fde2 commit af8316f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 43 deletions.
31 changes: 0 additions & 31 deletions .github/workflows/publish.yml

This file was deleted.

2 changes: 2 additions & 0 deletions examples/with-expo/src/Easing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Easing } from 'react-native'
import * as Reanimated from 'react-native-reanimated'
33 changes: 33 additions & 0 deletions examples/with-expo/src/Moti.NewTransition.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useAnimationState, useDynamicAnimation } from 'moti'
import React from 'react'

export function App() {
const state = useAnimationState({
from: {
transition: {
type: 'timing',
},
},
})
const state2 = useDynamicAnimation(() => ({
scale: 0.5,
transition: {
type: 'timing',
},
}))

const toggleTransition = () => {
state.transitionTo('from')
}

const animateTo = () => {
state2.animateTo({
opacity: 1,
transition: {
type: 'timing',
},
})
}

return <></>
}
2 changes: 1 addition & 1 deletion packages/moti/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "moti",
"private": false,
"version": "0.0.19",
"version": "0.20.0",
"keywords": [
"react-native",
"ios",
Expand Down
16 changes: 11 additions & 5 deletions packages/moti/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,15 @@ export type Variants<
// in component usage, it will extract these from the style prop ideally
AnimateType = ImageStyle & TextStyle & ViewStyle,
// edit the style props to remove transform array, flattening it
AnimateWithTransitions = StyleValueWithReplacedTransforms<AnimateType>,
AnimateWithTransformsAndTransition = StyleValueWithReplacedTransforms<AnimateType> &
WithTransition,
// allow the style values to be arrays for sequences, where values are primitives or objects with configs
Animate = StyleValueWithSequenceArrays<AnimateWithTransitions>
Animate = StyleValueWithSequenceArrays<AnimateWithTransformsAndTransition>
> = {
[key in keyof V]?: Animate
} & {
to?: Animate
from?: AnimateWithTransitions
from?: AnimateWithTransformsAndTransition
}

export type UseAnimationState<V> = {
Expand Down Expand Up @@ -409,6 +410,10 @@ export type UseAnimationStateConfig<
to?: ToKey
}

export type WithTransition = {
transition?: MotiTransition
}

/**
* Used for `useDynamicAnimation`
*/
Expand All @@ -419,9 +424,10 @@ export type DynamicStyleProp<
AnimateType = ImageStyle & ViewStyle & TextStyle,
// edit the style props to remove transform array, flattening it
// AnimateWithTransitions = Omit<AnimateType, 'transform'> & Partial<Transforms>,
AnimateWithTransitions = StyleValueWithReplacedTransforms<AnimateType>
AnimateWithTransforms = StyleValueWithReplacedTransforms<AnimateType>
// allow the style values to be arrays for sequences, where values are primitives or objects with configs
> = NonNullable<StyleValueWithSequenceArrays<AnimateWithTransitions>>
> = NonNullable<StyleValueWithSequenceArrays<AnimateWithTransforms>> &
WithTransition

export type UseDynamicAnimationState = {
/**
Expand Down
33 changes: 27 additions & 6 deletions packages/moti/src/core/use-motify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import type {
WithSpringConfig,
WithTimingConfig,
} from 'react-native-reanimated'

import { PackageName } from './constants/package-name'
import type {
MotiProps,
MotiTransition,
SequenceItem,
Transforms,
TransitionConfig,
WithTransition,
} from './types'

const debug = (...args: any[]) => {
Expand Down Expand Up @@ -199,7 +201,6 @@ function animationConfig<Animate>(
'velocityFactor',
]
for (const configKey of configKeys) {
// is this necessary ^ don't think so...?
const styleSpecificConfig = transition?.[key]?.[configKey]
const transitionConfigForKey = transition?.[configKey]

Expand All @@ -224,7 +225,7 @@ const getSequenceArray = (
sequenceKey: string,
sequenceArray: SequenceItem<any>[],
delayMs: number | undefined,
config: {},
config: object,
animation: (...props: any) => any,
callback: (completed: boolean, value?: any) => void
) => {
Expand Down Expand Up @@ -321,7 +322,7 @@ export function useMotify<Animate>({
// initializing here fixes reanimated object.__defineProperty bug(?)
transform: [] as TransformsStyle['transform'],
}
const variantStyle: Animate = state?.__state?.value || {}
const variantStyle: Animate & WithTransition = state?.__state?.value || {}

let animateStyle: Animate

Expand Down Expand Up @@ -368,7 +369,7 @@ export function useMotify<Animate>({
for (const key in exitStyle || {}) {
const disabledExitStyles = {
position: true,
zIndex: true
zIndex: true,
}
if (!disabledExitStyles[key]) {
exitingStyleProps[key] = true
Expand All @@ -382,6 +383,12 @@ export function useMotify<Animate>({
} else {
transition = transitionProp
}

// let the state prop drive transitions too
if (variantStyle.transition) {
transition = Object.assign({}, transition, variantStyle.transition)
}

if (isExiting && exitTransitionProp) {
let exitTransition: MotiTransition<Animate> | undefined
if (exitTransitionProp && 'value' in exitTransitionProp) {
Expand Down Expand Up @@ -457,7 +464,14 @@ export function useMotify<Animate>({

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

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

const sequence = getSequenceArray(key, value, delayMs, config, animation, callback)
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

1 comment on commit af8316f

@vercel
Copy link

@vercel vercel bot commented on af8316f Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.