import { each, is, deprecateDirectCall } from '@react-spring/shared' import { Lookup, Falsy, OneOrMore } from '@react-spring/types' import { AsyncResult, ControllerUpdate } from './types' import { Controller } from './Controller' export interface ControllerUpdateFn { (i: number, ctrl: Controller): ControllerUpdate | Falsy } export interface SpringRef { ( props?: ControllerUpdate | ControllerUpdateFn ): AsyncResult>[] current: Controller[] /** Add a controller to this ref */ add(ctrl: Controller): void /** Remove a controller from this ref */ delete(ctrl: Controller): void /** Pause all animations. */ pause(): this /** Pause animations for the given keys. */ pause(keys: OneOrMore): this /** Pause some or all animations. */ pause(keys?: OneOrMore): this /** Resume all animations. */ resume(): this /** Resume animations for the given keys. */ resume(keys: OneOrMore): this /** Resume some or all animations. */ resume(keys?: OneOrMore): this /** Update the state of each controller without animating. */ set(values: Partial): void /** Update the state of each controller without animating based on their passed state. */ set(values: (index: number, ctrl: Controller) => Partial): void /** Start the queued animations of each controller. */ start(): AsyncResult>[] /** Update every controller with the same props. */ start(props: ControllerUpdate): AsyncResult>[] /** Update controllers based on their state. */ start(props: ControllerUpdateFn): AsyncResult>[] /** Start animating each controller. */ start( props?: ControllerUpdate | ControllerUpdateFn ): AsyncResult>[] /** Stop all animations. */ stop(): this /** Stop animations for the given keys. */ stop(keys: OneOrMore): this /** Cancel all animations. */ stop(cancel: boolean): this /** Cancel animations for the given keys. */ stop(cancel: boolean, keys: OneOrMore): this /** Stop some or all animations. */ stop(keys?: OneOrMore): this /** Cancel some or all animations. */ stop(cancel: boolean, keys?: OneOrMore): this /** Add the same props to each controller's update queue. */ update(props: ControllerUpdate): this /** Generate separate props for each controller's update queue. */ update(props: ControllerUpdateFn): this /** Add props to each controller's update queue. */ update(props: ControllerUpdate | ControllerUpdateFn): this _getProps( arg: ControllerUpdate | ControllerUpdateFn, ctrl: Controller, index: number ): ControllerUpdate | Falsy } export const SpringRef = < State extends Lookup = Lookup, >(): SpringRef => { const current: Controller[] = [] const SpringRef: SpringRef = function (props) { deprecateDirectCall() const results: AsyncResult[] = [] each(current, (ctrl, i) => { if (is.und(props)) { results.push(ctrl.start()) } else { const update = _getProps(props, ctrl, i) if (update) { results.push(ctrl.start(update)) } } }) return results } SpringRef.current = current /** Add a controller to this ref */ SpringRef.add = function (ctrl: Controller) { if (!current.includes(ctrl)) { current.push(ctrl) } } /** Remove a controller from this ref */ SpringRef.delete = function (ctrl: Controller) { const i = current.indexOf(ctrl) if (~i) current.splice(i, 1) } /** Pause all animations. */ SpringRef.pause = function () { each(current, ctrl => ctrl.pause(...arguments)) return this } /** Resume all animations. */ SpringRef.resume = function () { each(current, ctrl => ctrl.resume(...arguments)) return this } /** Update the state of each controller without animating. */ SpringRef.set = function ( values: | Partial | ((i: number, ctrl: Controller) => Partial) ) { each(current, (ctrl, i) => { const update = is.fun(values) ? values(i, ctrl) : values if (update) { ctrl.set(update) } }) } SpringRef.start = function (props?: object | ControllerUpdateFn) { const results: AsyncResult[] = [] each(current, (ctrl, i) => { if (is.und(props)) { results.push(ctrl.start()) } else { const update = this._getProps(props, ctrl, i) if (update) { results.push(ctrl.start(update)) } } }) return results } /** Stop all animations. */ SpringRef.stop = function () { each(current, ctrl => ctrl.stop(...arguments)) return this } SpringRef.update = function (props: object | ControllerUpdateFn) { each(current, (ctrl, i) => ctrl.update(this._getProps(props, ctrl, i))) return this } /** Overridden by `useTrail` to manipulate props */ const _getProps = function ( arg: ControllerUpdate | ControllerUpdateFn, ctrl: Controller, index: number ) { return is.fun(arg) ? arg(index, ctrl) : arg } SpringRef._getProps = _getProps return SpringRef }