import React, { forwardRef } from 'react'
import Animated from 'react-native-reanimated'
import { ExcludeFunctionKeys, MotiProps } from '../core/types'
import { useMotify } from '../core/use-motify'
type AdditionalProps = {
children?: React.ReactNode
/**
* Animated props are not allowed with a Moti SVG component, since they will be overridden.
*
* Please use the `animate` prop instead. You can pass a derived value if needed:
*
* ```tsx
* const MotiRect = motifySvg(Rect)()
*
* export const Example = () => {
* const animate = useDerivedValue(() => {
* return {
* width: 100,
* height: 100,
* }
* })
* return
* }
* ```
*/
animatedProps?: never
}
export function motifySvg<
C extends React.ComponentClass,
Props = React.ComponentPropsWithoutRef,
Animate = ExcludeFunctionKeys>
>(ComponentWithoutAnimation: C) {
const withAnimations = () => {
const AnimatedComponent = Animated.createAnimatedComponent(
ComponentWithoutAnimation
)
const Motified = forwardRef<
React.RefAttributes>,
Props & MotiProps & AdditionalProps
>(function Moti(props, ref) {
const animated = useMotify(props)
if (props.animatedProps) {
console.warn(
`Moti: You passed animatedProps to a Moti SVG component. This will do nothing. You should use the animate prop directly. This will have no effect.`
)
}
return (
)
})
Motified.displayName = `MotiSvg.${
ComponentWithoutAnimation.displayName ||
ComponentWithoutAnimation.name ||
'NoName'
}`
return Motified
}
return withAnimations
}