|
|
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 { |
|
|
|
|
|
applyAnimatedValues: (node: any, props: Lookup) => boolean | void |
|
|
|
|
|
createAnimatedStyle: (style: Lookup) => Animated |
|
|
|
|
|
getComponentProps: (props: Lookup) => typeof props |
|
|
} |
|
|
|
|
|
|
|
|
type WithAnimated = { |
|
|
(Component: AnimatableComponent): any |
|
|
[key: string]: any |
|
|
} |
|
|
|
|
|
|
|
|
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 |
|
|
|