| | 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<State extends Lookup = Lookup> { |
| | (i: number, ctrl: Controller<State>): ControllerUpdate<State> | Falsy |
| | } |
| |
|
| | export interface SpringRef<State extends Lookup = Lookup> { |
| | ( |
| | props?: ControllerUpdate<State> | ControllerUpdateFn<State> |
| | ): AsyncResult<Controller<State>>[] |
| | current: Controller<State>[] |
| |
|
| | |
| | add(ctrl: Controller<State>): void |
| |
|
| | |
| | delete(ctrl: Controller<State>): void |
| |
|
| | |
| | pause(): this |
| | |
| | pause(keys: OneOrMore<string>): this |
| | |
| | pause(keys?: OneOrMore<string>): this |
| |
|
| | |
| | resume(): this |
| | |
| | resume(keys: OneOrMore<string>): this |
| | |
| | resume(keys?: OneOrMore<string>): this |
| |
|
| | |
| | set(values: Partial<State>): void |
| | |
| | set(values: (index: number, ctrl: Controller<State>) => Partial<State>): void |
| |
|
| | |
| | start(): AsyncResult<Controller<State>>[] |
| | |
| | start(props: ControllerUpdate<State>): AsyncResult<Controller<State>>[] |
| | |
| | start(props: ControllerUpdateFn<State>): AsyncResult<Controller<State>>[] |
| | |
| | start( |
| | props?: ControllerUpdate<State> | ControllerUpdateFn<State> |
| | ): AsyncResult<Controller<State>>[] |
| |
|
| | |
| | stop(): this |
| | |
| | stop(keys: OneOrMore<string>): this |
| | |
| | stop(cancel: boolean): this |
| | |
| | stop(cancel: boolean, keys: OneOrMore<string>): this |
| | |
| | stop(keys?: OneOrMore<string>): this |
| | |
| | stop(cancel: boolean, keys?: OneOrMore<string>): this |
| |
|
| | |
| | update(props: ControllerUpdate<State>): this |
| | |
| | update(props: ControllerUpdateFn<State>): this |
| | |
| | update(props: ControllerUpdate<State> | ControllerUpdateFn<State>): this |
| |
|
| | _getProps( |
| | arg: ControllerUpdate<State> | ControllerUpdateFn<State>, |
| | ctrl: Controller<State>, |
| | index: number |
| | ): ControllerUpdate<State> | Falsy |
| | } |
| |
|
| | export const SpringRef = < |
| | State extends Lookup = Lookup, |
| | >(): SpringRef<State> => { |
| | const current: Controller<State>[] = [] |
| |
|
| | const SpringRef: SpringRef<State> = 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 |
| |
|
| | |
| | SpringRef.add = function (ctrl: Controller<State>) { |
| | if (!current.includes(ctrl)) { |
| | current.push(ctrl) |
| | } |
| | } |
| |
|
| | |
| | SpringRef.delete = function (ctrl: Controller<State>) { |
| | const i = current.indexOf(ctrl) |
| | if (~i) current.splice(i, 1) |
| | } |
| |
|
| | |
| | SpringRef.pause = function () { |
| | each(current, ctrl => ctrl.pause(...arguments)) |
| | return this |
| | } |
| |
|
| | |
| | SpringRef.resume = function () { |
| | each(current, ctrl => ctrl.resume(...arguments)) |
| | return this |
| | } |
| |
|
| | |
| | SpringRef.set = function ( |
| | values: |
| | | Partial<State> |
| | | ((i: number, ctrl: Controller<State>) => Partial<State>) |
| | ) { |
| | each(current, (ctrl, i) => { |
| | const update = is.fun(values) ? values(i, ctrl) : values |
| | if (update) { |
| | ctrl.set(update) |
| | } |
| | }) |
| | } |
| |
|
| | SpringRef.start = function (props?: object | ControllerUpdateFn<State>) { |
| | 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 |
| | } |
| |
|
| | |
| | SpringRef.stop = function () { |
| | each(current, ctrl => ctrl.stop(...arguments)) |
| | return this |
| | } |
| |
|
| | SpringRef.update = function (props: object | ControllerUpdateFn<State>) { |
| | each(current, (ctrl, i) => ctrl.update(this._getProps(props, ctrl, i))) |
| | return this |
| | } |
| |
|
| | |
| | const _getProps = function ( |
| | arg: ControllerUpdate<State> | ControllerUpdateFn<State>, |
| | ctrl: Controller<State>, |
| | index: number |
| | ) { |
| | return is.fun(arg) ? arg(index, ctrl) : arg |
| | } |
| |
|
| | SpringRef._getProps = _getProps |
| |
|
| | return SpringRef |
| | } |
| |
|