File size: 2,161 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import { Lookup } from '@react-spring/types'
import { is, eachProp } from '@react-spring/shared'
import { AnimatableComponent, withAnimated } from './withAnimated'
import { Animated } from './Animated'
import { AnimatedObject } from './AnimatedObject'
export interface HostConfig {
/** Provide custom logic for native updates */
applyAnimatedValues: (node: any, props: Lookup) => boolean | void
/** Wrap the `style` prop with an animated node */
createAnimatedStyle: (style: Lookup) => Animated
/** Intercept props before they're passed to an animated component */
getComponentProps: (props: Lookup) => typeof props
}
// A stub type that gets replaced by @react-spring/web and others.
type WithAnimated = {
(Component: AnimatableComponent): any
[key: string]: any
}
// For storing the animated version on the original component
const cacheKey = Symbol.for('AnimatedComponent')
export const createHost = (
components: AnimatableComponent[] | { [key: string]: AnimatableComponent },
{
applyAnimatedValues = () => false,
createAnimatedStyle = style => new AnimatedObject(style),
getComponentProps = props => props,
}: Partial<HostConfig> = {}
) => {
const hostConfig: HostConfig = {
applyAnimatedValues,
createAnimatedStyle,
getComponentProps,
}
const animated: WithAnimated = (Component: any) => {
const displayName = getDisplayName(Component) || 'Anonymous'
if (is.str(Component)) {
Component =
animated[Component] ||
(animated[Component] = withAnimated(Component, hostConfig))
} else {
Component =
Component[cacheKey] ||
(Component[cacheKey] = withAnimated(Component, hostConfig))
}
Component.displayName = `Animated(${displayName})`
return Component
}
eachProp(components, (Component, key) => {
if (is.arr(components)) {
key = getDisplayName(Component)!
}
animated[key] = animated(Component)
})
return {
animated,
}
}
const getDisplayName = (arg: AnimatableComponent) =>
is.str(arg)
? arg
: arg && is.str(arg.displayName)
? arg.displayName
: (is.fun(arg) && arg.name) || null
|