Skip to content

Commit

Permalink
callback type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nandorojo committed Mar 27, 2023
1 parent 9836d44 commit 93c9eca
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
42 changes: 42 additions & 0 deletions examples/with-expo/src/Moti.Callbacks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useReducer } from 'react'
import { StyleSheet, Pressable } from 'react-native'
import { MotiView } from 'moti'

export default function HelloWorld() {
const [visible, toggle] = useReducer((s) => !s, true)

return (
<Pressable onPress={toggle} style={styles.container}>
<MotiView
animate={{
opacity: {
value: visible ? 1 : 0,
onDidAnimate(finished, maybeValue, { attemptedValue }) {
console.log('[onDidAnimate] finished:', finished)
console.log('[onDidAnimate] maybeValue:', maybeValue)
console.log('[onDidAnimate] attemptedValue:', attemptedValue)
},
},
}}
/>
</Pressable>
)
}

const styles = StyleSheet.create({
shape: {
justifyContent: 'center',
height: 250,
width: 250,
borderRadius: 25,
marginRight: 10,
backgroundColor: 'white',
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'row',
backgroundColor: '#9c1aff',
},
})
1 change: 1 addition & 0 deletions examples/with-expo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"moti": ["../../packages/moti/src"],
"moti$": ["../../packages/moti/src"],
"moti/svg": ["../../packages/moti/src/svg"]
},
Expand Down
16 changes: 9 additions & 7 deletions packages/moti/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export type StyleValueWithSequenceArraysWithoutTransform<
[key in KeyWithoutTransform]:
| T[KeyWithoutTransform] // either the value
// or an array of values for a sequence
| SequenceItem<T[KeyWithoutTransform]>[]
| SequenceItem<T[ExcludeArrayType<ExcludeObject<KeyWithoutTransform>>]>[]
}

export type StyleValueWithSequenceArraysWithTransform = {
Expand Down Expand Up @@ -252,11 +252,14 @@ export type InlineOnDidAnimate<Value> = (
}
) => void

type StyleValueWithCallbacks<Animate = FallbackAnimateProp> = {
type ExcludeArrayType<T> = T extends (infer U)[] ? never : T
type ExcludeObject<T> = T extends object ? never : T

type StyleValueWithCallbacks<Animate> = {
[Key in keyof Animate]?:
| Animate[Key]
| {
value: Animate[Key]
value: ExcludeObject<ExcludeArrayType<Animate[Key]>>
onDidAnimate: InlineOnDidAnimate<Animate[Key]>
}
}
Expand All @@ -267,12 +270,11 @@ export interface MotiProps<
// 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 = Omit<AnimateType, 'transform'> & Partial<Transforms>,
AnimateWithTransforms = StyleValueWithReplacedTransforms<AnimateType>,
// allow the style values to be callbacks with configs
AnimateWithCallbacks = StyleValueWithCallbacks<AnimateWithTransforms>,
// allow the style values to be arrays for sequences, where values are primitives or objects with configs
Animate = StyleValueWithSequenceArrays<AnimateWithCallbacks>
AnimateWithSequences = StyleValueWithSequenceArrays<AnimateWithTransforms>,
Animate = StyleValueWithCallbacks<AnimateWithSequences>
> {
// we want the "value" returned to not include the style arrays here, so we use AnimateWithTransitions
/**
Expand All @@ -293,7 +295,7 @@ export interface MotiProps<
*
* @worklet
*/
animate?: OrDerivedValue<Animate> | (() => Animate)
animate?: OrDerivedValue<Animate>
/**
* (Optional) specify styles which the component should animate from.
*
Expand Down

0 comments on commit 93c9eca

Please sign in to comment.