| import * as React from 'react' |
| import { useContext, useRef, useMemo } from 'react' |
| import { Lookup, OneOrMore, UnknownProps } from '@react-spring/types' |
| import { |
| is, |
| toArray, |
| useForceUpdate, |
| useOnce, |
| usePrev, |
| each, |
| useIsomorphicLayoutEffect, |
| } from '@react-spring/shared' |
|
|
| import { |
| Change, |
| ControllerUpdate, |
| ItemKeys, |
| PickAnimated, |
| TransitionFn, |
| TransitionState, |
| TransitionTo, |
| UseTransitionProps, |
| } from '../types' |
| import { Valid } from '../types/common' |
| import { |
| callProp, |
| detachRefs, |
| getDefaultProps, |
| hasProps, |
| inferTo, |
| replaceRef, |
| } from '../helpers' |
| import { Controller, getSprings } from '../Controller' |
| import { SpringContext } from '../SpringContext' |
| import { SpringRef } from '../SpringRef' |
| import type { SpringRef as SpringRefType } from '../SpringRef' |
| import { TransitionPhase } from '../TransitionPhase' |
|
|
| declare function setTimeout(handler: Function, timeout?: number): number |
| declare function clearTimeout(timeoutId: number): void |
|
|
| export function useTransition<Item, Props extends object>( |
| data: OneOrMore<Item>, |
| props: () => |
| | UseTransitionProps<Item> |
| | (Props & Valid<Props, UseTransitionProps<Item>>), |
| deps?: any[] |
| ): PickAnimated<Props> extends infer State |
| ? State extends Lookup |
| ? [TransitionFn<Item, PickAnimated<Props>>, SpringRefType<State>] |
| : never |
| : never |
|
|
| export function useTransition<Item, Props extends object>( |
| data: OneOrMore<Item>, |
| props: |
| | UseTransitionProps<Item> |
| | (Props & Valid<Props, UseTransitionProps<Item>>) |
| ): TransitionFn<Item, PickAnimated<Props>> |
|
|
| export function useTransition<Item, Props extends object>( |
| data: OneOrMore<Item>, |
| props: |
| | UseTransitionProps<Item> |
| | (Props & Valid<Props, UseTransitionProps<Item>>), |
| deps: any[] | undefined |
| ): PickAnimated<Props> extends infer State |
| ? State extends Lookup |
| ? [TransitionFn<Item, State>, SpringRefType<State>] |
| : never |
| : never |
|
|
| export function useTransition( |
| data: unknown, |
| props: UseTransitionProps | (() => any), |
| deps?: any[] |
| ): any { |
| const propsFn = is.fun(props) && props |
|
|
| const { |
| reset, |
| sort, |
| trail = 0, |
| expires = true, |
| exitBeforeEnter = false, |
| onDestroyed, |
| ref: propsRef, |
| config: propsConfig, |
| }: UseTransitionProps<any> = propsFn ? propsFn() : props |
|
|
| |
| const ref = useMemo( |
| () => (propsFn || arguments.length == 3 ? SpringRef() : void 0), |
| [] |
| ) |
|
|
| |
| const items = toArray(data) |
| const transitions: TransitionState[] = [] |
|
|
| |
| const usedTransitions = useRef<TransitionState[] | null>(null) |
| const prevTransitions = reset ? null : usedTransitions.current |
|
|
| useIsomorphicLayoutEffect(() => { |
| usedTransitions.current = transitions |
| }) |
|
|
| useOnce(() => { |
| |
| |
| |
| |
| |
| |
| |
|
|
| each(transitions, t => { |
| ref?.add(t.ctrl) |
| t.ctrl.ref = ref |
| }) |
|
|
| |
| return () => { |
| each(usedTransitions.current!, t => { |
| if (t.expired) { |
| clearTimeout(t.expirationId!) |
| } |
| detachRefs(t.ctrl, ref) |
| t.ctrl.stop(true) |
| }) |
| } |
| }) |
|
|
| |
| |
| |
| |
| const keys = getKeys(items, propsFn ? propsFn() : props, prevTransitions) |
|
|
| |
| const expired = (reset && usedTransitions.current) || [] |
| useIsomorphicLayoutEffect(() => |
| each(expired, ({ ctrl, item, key }) => { |
| detachRefs(ctrl, ref) |
| callProp(onDestroyed, item, key) |
| }) |
| ) |
|
|
| |
| const reused: number[] = [] |
| if (prevTransitions) |
| each(prevTransitions, (t, i) => { |
| |
| if (t.expired) { |
| clearTimeout(t.expirationId!) |
| expired.push(t) |
| } else { |
| i = reused[i] = keys.indexOf(t.key) |
| if (~i) transitions[i] = t |
| } |
| }) |
|
|
| |
| each(items, (item, i) => { |
| if (!transitions[i]) { |
| transitions[i] = { |
| key: keys[i], |
| item, |
| phase: TransitionPhase.MOUNT, |
| ctrl: new Controller(), |
| } |
|
|
| transitions[i].ctrl.item = item |
| } |
| }) |
|
|
| |
| |
| if (reused.length) { |
| let i = -1 |
| const { leave }: UseTransitionProps<any> = propsFn ? propsFn() : props |
| each(reused, (keyIndex, prevIndex) => { |
| const t = prevTransitions![prevIndex] |
| if (~keyIndex) { |
| i = transitions.indexOf(t) |
| transitions[i] = { ...t, item: items[keyIndex] } |
| } else if (leave) { |
| transitions.splice(++i, 0, t) |
| } |
| }) |
| } |
|
|
| if (is.fun(sort)) { |
| transitions.sort((a, b) => sort(a.item, b.item)) |
| } |
|
|
| |
| let delay = -trail |
|
|
| |
| const forceUpdate = useForceUpdate() |
|
|
| |
| const defaultProps = getDefaultProps<UseTransitionProps>(props) |
| |
| const changes = new Map<TransitionState, Change>() |
| const exitingTransitions = useRef(new Map<TransitionState, Change>()) |
|
|
| const forceChange = useRef(false) |
| each(transitions, (t, i) => { |
| const key = t.key |
| const prevPhase = t.phase |
|
|
| const p: UseTransitionProps<any> = propsFn ? propsFn() : props |
|
|
| let to: TransitionTo<any> |
| let phase: TransitionPhase |
|
|
| const propsDelay = callProp(p.delay || 0, key) |
|
|
| if (prevPhase == TransitionPhase.MOUNT) { |
| to = p.enter |
| phase = TransitionPhase.ENTER |
| } else { |
| const isLeave = keys.indexOf(key) < 0 |
| if (prevPhase != TransitionPhase.LEAVE) { |
| if (isLeave) { |
| to = p.leave |
| phase = TransitionPhase.LEAVE |
| } else if ((to = p.update)) { |
| phase = TransitionPhase.UPDATE |
| } else return |
| } else if (!isLeave) { |
| to = p.enter |
| phase = TransitionPhase.ENTER |
| } else return |
| } |
|
|
| |
| |
| to = callProp(to, t.item, i) |
| to = is.obj(to) ? inferTo(to) : { to } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if (!to.config) { |
| const config = propsConfig || defaultProps.config |
| to.config = callProp(config, t.item, i, phase) |
| } |
|
|
| delay += trail |
|
|
| |
| const payload: ControllerUpdate<UnknownProps> = { |
| ...defaultProps, |
| |
| delay: propsDelay + delay, |
| ref: propsRef, |
| immediate: p.immediate, |
| |
| reset: false, |
| |
| ...(to as any), |
| } |
|
|
| if (phase == TransitionPhase.ENTER && is.und(payload.from)) { |
| const p = propsFn ? propsFn() : props |
| |
| |
| |
| const from = is.und(p.initial) || prevTransitions ? p.from : p.initial |
|
|
| payload.from = callProp(from, t.item, i) |
| } |
|
|
| const { onResolve } = payload |
| payload.onResolve = result => { |
| callProp(onResolve, result) |
|
|
| const transitions = usedTransitions.current! |
| const t = transitions.find(t => t.key === key) |
| if (!t) return |
|
|
| |
| |
| if (result.cancelled && t.phase != TransitionPhase.UPDATE) { |
| |
| |
| |
| |
| |
| |
| |
| return |
| } |
|
|
| if (t.ctrl.idle) { |
| const idle = transitions.every(t => t.ctrl.idle) |
| if (t.phase == TransitionPhase.LEAVE) { |
| const expiry = callProp(expires, t.item) |
| if (expiry !== false) { |
| const expiryMs = expiry === true ? 0 : expiry |
| t.expired = true |
|
|
| |
| if (!idle && expiryMs > 0) { |
| |
| if (expiryMs <= 0x7fffffff) |
| t.expirationId = setTimeout(forceUpdate, expiryMs) |
| return |
| } |
| } |
| } |
| |
| if (idle && transitions.some(t => t.expired)) { |
| |
| |
| |
| |
| exitingTransitions.current.delete(t) |
|
|
| if (exitBeforeEnter) { |
| |
| |
| |
| |
| forceChange.current = true |
| } |
|
|
| forceUpdate() |
| } |
| } |
| } |
|
|
| const springs = getSprings(t.ctrl, payload) |
|
|
| |
| |
| |
| if (phase === TransitionPhase.LEAVE && exitBeforeEnter) { |
| exitingTransitions.current.set(t, { phase, springs, payload }) |
| } else { |
| changes.set(t, { phase, springs, payload }) |
| } |
| }) |
|
|
| |
| const context = useContext(SpringContext) |
| const prevContext = usePrev(context) |
| const hasContext = context !== prevContext && hasProps(context) |
|
|
| |
| useIsomorphicLayoutEffect(() => { |
| if (hasContext) { |
| each(transitions, t => { |
| t.ctrl.start({ default: context }) |
| }) |
| } |
| }, [context]) |
|
|
| each(changes, (_, t) => { |
| |
| |
| |
| |
| |
| if (exitingTransitions.current.size) { |
| const ind = transitions.findIndex(state => state.key === t.key) |
| transitions.splice(ind, 1) |
| } |
| }) |
|
|
| useIsomorphicLayoutEffect( |
| () => { |
| |
| |
| |
| |
| each( |
| exitingTransitions.current.size ? exitingTransitions.current : changes, |
| ({ phase, payload }, t) => { |
| const { ctrl } = t |
|
|
| t.phase = phase |
|
|
| |
| ref?.add(ctrl) |
|
|
| |
| if (hasContext && phase == TransitionPhase.ENTER) { |
| ctrl.start({ default: context }) |
| } |
|
|
| if (payload) { |
| |
| replaceRef(ctrl, payload.ref) |
|
|
| |
| |
| |
| |
| |
| |
| if ((ctrl.ref || ref) && !forceChange.current) { |
| ctrl.update(payload) |
| } else { |
| ctrl.start(payload) |
|
|
| if (forceChange.current) { |
| forceChange.current = false |
| } |
| } |
| } |
| } |
| ) |
| }, |
| reset ? void 0 : deps |
| ) |
|
|
| const renderTransitions: TransitionFn = render => ( |
| <> |
| {transitions.map((t, i) => { |
| const { springs } = changes.get(t) || t.ctrl |
| const elem: any = render({ ...springs }, t.item, t, i) |
| |
| const key = is.str(t.key) || is.num(t.key) ? t.key : t.ctrl.id |
| const isLegacyReact = React.version < '19.0.0' |
| |
| const props = elem?.props ?? {} |
| |
| if (isLegacyReact) { |
| props.ref = elem.ref |
| } |
| |
| return elem && elem.type ? <elem.type key={key} {...props} /> : elem |
| })} |
| </> |
| ) |
|
|
| return ref ? [renderTransitions, ref] : renderTransitions |
| } |
|
|
| |
| let nextKey = 1 |
|
|
| function getKeys( |
| items: readonly any[], |
| { key, keys = key }: { key?: ItemKeys; keys?: ItemKeys }, |
| prevTransitions: TransitionState[] | null |
| ): readonly any[] { |
| if (keys === null) { |
| const reused = new Set() |
| return items.map(item => { |
| const t = |
| prevTransitions && |
| prevTransitions.find( |
| t => |
| t.item === item && |
| t.phase !== TransitionPhase.LEAVE && |
| !reused.has(t) |
| ) |
| if (t) { |
| reused.add(t) |
| return t.key |
| } |
| return nextKey++ |
| }) |
| } |
| return is.und(keys) ? items : is.fun(keys) ? items.map(keys) : toArray(keys) |
| } |
|
|