|
|
import { each, is, useIsomorphicLayoutEffect } from '@react-spring/shared' |
|
|
import { Lookup } from '@react-spring/types' |
|
|
|
|
|
import { Valid } from '../types/common' |
|
|
import { PickAnimated, SpringValues } from '../types' |
|
|
|
|
|
import { SpringRef } from '../SpringRef' |
|
|
import { Controller } from '../Controller' |
|
|
|
|
|
import { UseSpringProps } from './useSpring' |
|
|
import { useSprings } from './useSprings' |
|
|
import { replaceRef } from '../helpers' |
|
|
|
|
|
export type UseTrailProps<Props extends object = any> = UseSpringProps<Props> |
|
|
|
|
|
export function useTrail<Props extends object>( |
|
|
length: number, |
|
|
props: ( |
|
|
i: number, |
|
|
ctrl: Controller |
|
|
) => UseTrailProps | (Props & Valid<Props, UseTrailProps<Props>>), |
|
|
deps?: readonly any[] |
|
|
): PickAnimated<Props> extends infer State |
|
|
? State extends Lookup<any> |
|
|
? [SpringValues<State>[], SpringRef<State>] |
|
|
: never |
|
|
: never |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useTrail<Props extends object>( |
|
|
length: number, |
|
|
props: UseTrailProps | (Props & Valid<Props, UseTrailProps<Props>>) |
|
|
): SpringValues<PickAnimated<Props>>[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useTrail<Props extends object>( |
|
|
length: number, |
|
|
props: UseTrailProps | (Props & Valid<Props, UseTrailProps<Props>>), |
|
|
deps: readonly any[] |
|
|
): PickAnimated<Props> extends infer State |
|
|
? State extends Lookup<any> |
|
|
? [SpringValues<State>[], SpringRef<State>] |
|
|
: never |
|
|
: never |
|
|
|
|
|
export function useTrail( |
|
|
length: number, |
|
|
propsArg: unknown, |
|
|
deps?: readonly any[] |
|
|
) { |
|
|
const propsFn = is.fun(propsArg) && propsArg |
|
|
if (propsFn && !deps) deps = [] |
|
|
|
|
|
|
|
|
let reverse = true |
|
|
let passedRef: SpringRef | undefined = undefined |
|
|
|
|
|
const result = useSprings( |
|
|
length, |
|
|
(i, ctrl) => { |
|
|
const props = propsFn ? propsFn(i, ctrl) : propsArg |
|
|
passedRef = props.ref |
|
|
reverse = reverse && props.reverse |
|
|
|
|
|
return props |
|
|
}, |
|
|
|
|
|
|
|
|
deps || [{}] |
|
|
) |
|
|
|
|
|
useIsomorphicLayoutEffect(() => { |
|
|
|
|
|
|
|
|
|
|
|
each(result[1].current, (ctrl, i) => { |
|
|
const parent = result[1].current[i + (reverse ? 1 : -1)] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
replaceRef(ctrl, passedRef) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ctrl.ref) { |
|
|
if (parent) { |
|
|
ctrl.update({ to: parent.springs }) |
|
|
} |
|
|
|
|
|
return |
|
|
} |
|
|
|
|
|
if (parent) { |
|
|
ctrl.start({ to: parent.springs }) |
|
|
} else { |
|
|
ctrl.start() |
|
|
} |
|
|
}) |
|
|
}, deps) |
|
|
|
|
|
if (propsFn || arguments.length == 3) { |
|
|
const ref = passedRef ?? result[1] |
|
|
|
|
|
ref['_getProps'] = (propsArg, ctrl, i) => { |
|
|
const props = is.fun(propsArg) ? propsArg(i, ctrl) : propsArg |
|
|
if (props) { |
|
|
const parent = ref.current[i + (props.reverse ? 1 : -1)] |
|
|
if (parent) props.to = parent.springs |
|
|
return props |
|
|
} |
|
|
} |
|
|
return result |
|
|
} |
|
|
|
|
|
return result[0] |
|
|
} |
|
|
|