|
|
import { DependencyList, useContext, useMemo, useRef } from 'react' |
|
|
import { Lookup } from '@react-spring/types' |
|
|
import { |
|
|
is, |
|
|
each, |
|
|
usePrev, |
|
|
useOnce, |
|
|
useForceUpdate, |
|
|
useIsomorphicLayoutEffect, |
|
|
} from '@react-spring/shared' |
|
|
|
|
|
import { |
|
|
ControllerFlushFn, |
|
|
ControllerUpdate, |
|
|
PickAnimated, |
|
|
SpringValues, |
|
|
} from '../types' |
|
|
import { UseSpringProps } from './useSpring' |
|
|
import { declareUpdate } from '../SpringValue' |
|
|
import { |
|
|
Controller, |
|
|
getSprings, |
|
|
flushUpdateQueue, |
|
|
setSprings, |
|
|
} from '../Controller' |
|
|
import { hasProps, detachRefs, replaceRef } from '../helpers' |
|
|
import { SpringContext } from '../SpringContext' |
|
|
import { SpringRef } from '../SpringRef' |
|
|
import type { SpringRef as SpringRefType } from '../SpringRef' |
|
|
|
|
|
export type UseSpringsProps<State extends Lookup = Lookup> = unknown & |
|
|
ControllerUpdate<State> & { |
|
|
ref?: SpringRefType<State> |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useSprings<Props extends UseSpringProps>( |
|
|
length: number, |
|
|
props: (i: number, ctrl: Controller) => Props, |
|
|
deps?: readonly any[] |
|
|
): PickAnimated<Props> extends infer State |
|
|
? State extends Lookup<any> |
|
|
? [SpringValues<State>[], SpringRefType<State>] |
|
|
: never |
|
|
: never |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useSprings<Props extends UseSpringsProps>( |
|
|
length: number, |
|
|
props: Props[] & UseSpringsProps<PickAnimated<Props>>[] |
|
|
): SpringValues<PickAnimated<Props>>[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useSprings<Props extends UseSpringsProps>( |
|
|
length: number, |
|
|
props: Props[] & UseSpringsProps<PickAnimated<Props>>[], |
|
|
deps: DependencyList |
|
|
): PickAnimated<Props> extends infer State |
|
|
? State extends Lookup<any> |
|
|
? [SpringValues<State>[], SpringRefType<State>] |
|
|
: never |
|
|
: never |
|
|
|
|
|
|
|
|
export function useSprings( |
|
|
length: number, |
|
|
props: any[] | ((i: number, ctrl: Controller) => any), |
|
|
deps?: DependencyList |
|
|
): any { |
|
|
const propsFn = is.fun(props) && props |
|
|
if (propsFn && !deps) deps = [] |
|
|
|
|
|
|
|
|
const ref = useMemo( |
|
|
() => (propsFn || arguments.length == 3 ? SpringRef() : void 0), |
|
|
[] |
|
|
) |
|
|
|
|
|
interface State { |
|
|
|
|
|
ctrls: Controller[] |
|
|
|
|
|
queue: Array<() => void> |
|
|
|
|
|
flush: ControllerFlushFn |
|
|
} |
|
|
|
|
|
|
|
|
const layoutId = useRef(0) |
|
|
const forceUpdate = useForceUpdate() |
|
|
|
|
|
|
|
|
const state = useMemo( |
|
|
(): State => ({ |
|
|
ctrls: [], |
|
|
queue: [], |
|
|
flush(ctrl, updates) { |
|
|
const springs = getSprings(ctrl, updates) |
|
|
|
|
|
|
|
|
|
|
|
const canFlushSync = |
|
|
layoutId.current > 0 && |
|
|
!state.queue.length && |
|
|
!Object.keys(springs).some(key => !ctrl.springs[key]) |
|
|
|
|
|
return canFlushSync |
|
|
? flushUpdateQueue(ctrl, updates) |
|
|
: new Promise<any>(resolve => { |
|
|
setSprings(ctrl, springs) |
|
|
state.queue.push(() => { |
|
|
resolve(flushUpdateQueue(ctrl, updates)) |
|
|
}) |
|
|
forceUpdate() |
|
|
}) |
|
|
}, |
|
|
}), |
|
|
[] |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ctrls = useRef([...state.ctrls]) |
|
|
const updates = useRef<any[]>([]) |
|
|
|
|
|
|
|
|
const prevLength = usePrev(length) || 0 |
|
|
|
|
|
|
|
|
|
|
|
useMemo(() => { |
|
|
|
|
|
each(ctrls.current.slice(length, prevLength), ctrl => { |
|
|
detachRefs(ctrl, ref) |
|
|
ctrl.stop(true) |
|
|
}) |
|
|
ctrls.current.length = length |
|
|
|
|
|
declareUpdates(prevLength, length) |
|
|
}, [length]) |
|
|
|
|
|
|
|
|
useMemo(() => { |
|
|
declareUpdates(0, Math.min(prevLength, length)) |
|
|
|
|
|
}, deps) |
|
|
|
|
|
|
|
|
function declareUpdates(startIndex: number, endIndex: number) { |
|
|
for (let i = startIndex; i < endIndex; i++) { |
|
|
const ctrl = |
|
|
ctrls.current[i] || |
|
|
(ctrls.current[i] = new Controller(null, state.flush)) |
|
|
|
|
|
const update: UseSpringProps<any> = propsFn |
|
|
? propsFn(i, ctrl) |
|
|
: (props as any)[i] |
|
|
|
|
|
if (update) { |
|
|
updates.current[i] = declareUpdate(update) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const springs = ctrls.current.map((ctrl, i) => |
|
|
getSprings(ctrl, updates.current[i]) |
|
|
) |
|
|
|
|
|
const context = useContext(SpringContext) |
|
|
const prevContext = usePrev(context) |
|
|
const hasContext = context !== prevContext && hasProps(context) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useIsomorphicLayoutEffect(() => { |
|
|
layoutId.current++ |
|
|
|
|
|
|
|
|
state.ctrls = ctrls.current |
|
|
|
|
|
|
|
|
const { queue } = state |
|
|
if (queue.length) { |
|
|
state.queue = [] |
|
|
each(queue, cb => cb()) |
|
|
} |
|
|
|
|
|
|
|
|
each(ctrls.current, (ctrl, i) => { |
|
|
|
|
|
ref?.add(ctrl) |
|
|
|
|
|
|
|
|
if (hasContext) { |
|
|
ctrl.start({ default: context }) |
|
|
} |
|
|
|
|
|
|
|
|
const update = updates.current[i] |
|
|
if (update) { |
|
|
|
|
|
replaceRef(ctrl, update.ref) |
|
|
|
|
|
|
|
|
|
|
|
if (ctrl.ref) { |
|
|
ctrl.queue.push(update) |
|
|
} else { |
|
|
ctrl.start(update) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
useOnce(() => () => { |
|
|
each(state.ctrls, ctrl => ctrl.stop(true)) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const values = springs.map(x => ({ ...x })) |
|
|
|
|
|
return ref ? [values, ref] : values |
|
|
} |
|
|
|