|
|
import * as React from 'react' |
|
|
import { forwardRef, useRef, Ref, useCallback, useEffect } from 'react' |
|
|
import { |
|
|
is, |
|
|
each, |
|
|
raf, |
|
|
useForceUpdate, |
|
|
useOnce, |
|
|
FluidEvent, |
|
|
FluidValue, |
|
|
addFluidObserver, |
|
|
removeFluidObserver, |
|
|
useIsomorphicLayoutEffect, |
|
|
} from '@react-spring/shared' |
|
|
import { ElementType } from '@react-spring/types' |
|
|
|
|
|
import { AnimatedObject } from './AnimatedObject' |
|
|
import { TreeContext } from './context' |
|
|
import { HostConfig } from './createHost' |
|
|
|
|
|
export type AnimatableComponent = string | Exclude<ElementType, string> |
|
|
|
|
|
export const withAnimated = (Component: any, host: HostConfig) => { |
|
|
const hasInstance: boolean = |
|
|
|
|
|
|
|
|
!is.fun(Component) || |
|
|
(Component.prototype && Component.prototype.isReactComponent) |
|
|
|
|
|
return forwardRef((givenProps: any, givenRef: Ref<any>) => { |
|
|
const instanceRef = useRef<any>(null) |
|
|
|
|
|
|
|
|
|
|
|
const ref = |
|
|
hasInstance && |
|
|
|
|
|
useCallback( |
|
|
(value: any) => { |
|
|
instanceRef.current = updateRef(givenRef, value) |
|
|
}, |
|
|
[givenRef] |
|
|
) |
|
|
|
|
|
const [props, deps] = getAnimatedState(givenProps, host) |
|
|
|
|
|
const forceUpdate = useForceUpdate() |
|
|
|
|
|
const callback = () => { |
|
|
const instance = instanceRef.current |
|
|
if (hasInstance && !instance) { |
|
|
|
|
|
|
|
|
return |
|
|
} |
|
|
|
|
|
const didUpdate = instance |
|
|
? host.applyAnimatedValues(instance, props.getValue(true)) |
|
|
: false |
|
|
|
|
|
|
|
|
if (didUpdate === false) { |
|
|
forceUpdate() |
|
|
} |
|
|
} |
|
|
|
|
|
const observer = new PropsObserver(callback, deps) |
|
|
|
|
|
const observerRef = useRef<PropsObserver>(undefined) |
|
|
useIsomorphicLayoutEffect(() => { |
|
|
observerRef.current = observer |
|
|
|
|
|
|
|
|
each(deps, dep => addFluidObserver(dep, observer)) |
|
|
|
|
|
return () => { |
|
|
|
|
|
if (observerRef.current) { |
|
|
each(observerRef.current.deps, dep => |
|
|
removeFluidObserver(dep, observerRef.current!) |
|
|
) |
|
|
raf.cancel(observerRef.current.update) |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
useEffect(callback, []) |
|
|
|
|
|
useOnce(() => () => { |
|
|
const observer = observerRef.current! |
|
|
each(observer.deps, dep => removeFluidObserver(dep, observer)) |
|
|
}) |
|
|
|
|
|
const usedProps = host.getComponentProps(props.getValue()) |
|
|
return <Component {...usedProps} ref={ref} /> |
|
|
}) |
|
|
} |
|
|
|
|
|
class PropsObserver { |
|
|
constructor( |
|
|
readonly update: () => void, |
|
|
readonly deps: Set<FluidValue> |
|
|
) {} |
|
|
eventObserved(event: FluidEvent) { |
|
|
if (event.type == 'change') { |
|
|
raf.write(this.update) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
type AnimatedState = [props: AnimatedObject, dependencies: Set<FluidValue>] |
|
|
|
|
|
function getAnimatedState(props: any, host: HostConfig): AnimatedState { |
|
|
const dependencies = new Set<FluidValue>() |
|
|
TreeContext.dependencies = dependencies |
|
|
|
|
|
|
|
|
if (props.style) |
|
|
props = { |
|
|
...props, |
|
|
style: host.createAnimatedStyle(props.style), |
|
|
} |
|
|
|
|
|
|
|
|
props = new AnimatedObject(props) |
|
|
|
|
|
TreeContext.dependencies = null |
|
|
return [props, dependencies] |
|
|
} |
|
|
|
|
|
function updateRef<T>(ref: Ref<T>, value: T) { |
|
|
if (ref) { |
|
|
if (is.fun(ref)) ref(value) |
|
|
else (ref as any).current = value |
|
|
} |
|
|
return value |
|
|
} |
|
|
|