File size: 5,478 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 a controller to this ref */
  add(ctrl: Controller<State>): void

  /** Remove a controller from this ref */
  delete(ctrl: Controller<State>): void

  /** Pause all animations. */
  pause(): this
  /** Pause animations for the given keys. */
  pause(keys: OneOrMore<string>): this
  /** Pause some or all animations. */
  pause(keys?: OneOrMore<string>): this

  /** Resume all animations. */
  resume(): this
  /** Resume animations for the given keys. */
  resume(keys: OneOrMore<string>): this
  /** Resume some or all animations. */
  resume(keys?: OneOrMore<string>): this

  /** Update the state of each controller without animating. */
  set(values: Partial<State>): void
  /** Update the state of each controller without animating based on their passed state. */
  set(values: (index: number, ctrl: Controller<State>) => Partial<State>): void

  /** Start the queued animations of each controller. */
  start(): AsyncResult<Controller<State>>[]
  /** Update every controller with the same props. */
  start(props: ControllerUpdate<State>): AsyncResult<Controller<State>>[]
  /** Update controllers based on their state. */
  start(props: ControllerUpdateFn<State>): AsyncResult<Controller<State>>[]
  /** Start animating each controller. */
  start(
    props?: ControllerUpdate<State> | ControllerUpdateFn<State>
  ): AsyncResult<Controller<State>>[]

  /** Stop all animations. */
  stop(): this
  /** Stop animations for the given keys. */
  stop(keys: OneOrMore<string>): this
  /** Cancel all animations. */
  stop(cancel: boolean): this
  /** Cancel animations for the given keys. */
  stop(cancel: boolean, keys: OneOrMore<string>): this
  /** Stop some or all animations. */
  stop(keys?: OneOrMore<string>): this
  /** Cancel some or all animations. */
  stop(cancel: boolean, keys?: OneOrMore<string>): this

  /** Add the same props to each controller's update queue. */
  update(props: ControllerUpdate<State>): this
  /** Generate separate props for each controller's update queue. */
  update(props: ControllerUpdateFn<State>): this
  /** Add props to each controller's update queue. */
  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

  /** Add a controller to this ref */
  SpringRef.add = function (ctrl: Controller<State>) {
    if (!current.includes(ctrl)) {
      current.push(ctrl)
    }
  }

  /** Remove a controller from this ref */
  SpringRef.delete = function (ctrl: Controller<State>) {
    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<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
  }

  /** Stop all animations. */
  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
  }

  /** Overridden by `useTrail` to manipulate props */
  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
}