File size: 5,367 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {
  is,
  toArray,
  eachProp,
  getFluidValue,
  isAnimatedString,
  FluidValue,
  Globals as G,
} from '@react-spring/shared'
import { AnyFn, OneOrMore, Lookup } from '@react-spring/types'
import { ReservedProps, ForwardProps, InferTo } from './types'
import type { Controller } from './Controller'
import type { SpringRef } from './SpringRef'

export function callProp<T>(
  value: T,
  ...args: T extends AnyFn ? Parameters<T> : unknown[]
): T extends AnyFn<any, infer U> ? U : T {
  return is.fun(value) ? value(...args) : value
}

/** Try to coerce the given value into a boolean using the given key */
export const matchProp = (
  value: boolean | OneOrMore<string> | ((key: any) => boolean) | undefined,
  key: string | undefined
) =>
  value === true ||
  !!(
    key &&
    value &&
    (is.fun(value) ? value(key) : toArray(value).includes(key))
  )

export const resolveProp = <T>(
  prop: T | Lookup<T> | undefined,
  key: string | undefined
) => (is.obj(prop) ? key && (prop as any)[key] : prop)

export const concatFn = <T extends AnyFn>(first: T | undefined, last: T) =>
  first ? (...args: Parameters<T>) => (first(...args), last(...args)) : last

/** Returns `true` if the given prop is having its default value set. */
export const hasDefaultProp = <T extends Lookup>(props: T, key: keyof T) =>
  !is.und(getDefaultProp(props, key))

/** Get the default value being set for the given `key` */
export const getDefaultProp = <T extends Lookup, P extends keyof T>(
  props: T,
  key: P
): T[P] =>
  props.default === true
    ? props[key]
    : props.default
      ? props.default[key]
      : undefined

const noopTransform = (value: any) => value

/**
 * Extract the default props from an update.
 *
 * When the `default` prop is falsy, this function still behaves as if
 * `default: true` was used. The `default` prop is always respected when
 * truthy.
 */
export const getDefaultProps = <T extends Lookup>(
  props: Lookup,
  transform: (value: any, key: string) => any = noopTransform
): T => {
  let keys: readonly string[] = DEFAULT_PROPS
  if (props.default && props.default !== true) {
    props = props.default
    keys = Object.keys(props)
  }
  const defaults: any = {}
  for (const key of keys) {
    const value = transform(props[key], key)
    if (!is.und(value)) {
      defaults[key] = value
    }
  }
  return defaults
}

/**
 * These props are implicitly used as defaults when defined in a
 * declarative update (eg: render-based) or any update with `default: true`.
 *
 * Use `default: {}` or `default: false` to opt-out of these implicit defaults
 * for any given update.
 *
 * Note: These are not the only props with default values. For example, the
 * `pause`, `cancel`, and `immediate` props. But those must be updated with
 * the object syntax (eg: `default: { immediate: true }`).
 */
export const DEFAULT_PROPS = [
  'config',
  'onProps',
  'onStart',
  'onChange',
  'onPause',
  'onResume',
  'onRest',
] as const

const RESERVED_PROPS: {
  [key: string]: 1 | undefined
} = {
  config: 1,
  from: 1,
  to: 1,
  ref: 1,
  loop: 1,
  reset: 1,
  pause: 1,
  cancel: 1,
  reverse: 1,
  immediate: 1,
  default: 1,
  delay: 1,
  onProps: 1,
  onStart: 1,
  onChange: 1,
  onPause: 1,
  onResume: 1,
  onRest: 1,
  onResolve: 1,

  // Transition props
  items: 1,
  trail: 1,
  sort: 1,
  expires: 1,
  initial: 1,
  enter: 1,
  update: 1,
  leave: 1,
  children: 1,
  onDestroyed: 1,

  // Internal props
  keys: 1,
  callId: 1,
  parentId: 1,
}

/**
 * Extract any properties whose keys are *not* reserved for customizing your
 * animations. All hooks use this function, which means `useTransition` props
 * are reserved for `useSpring` calls, etc.
 */
function getForwardProps<Props extends ReservedProps>(
  props: Props
): ForwardProps<Props> | undefined {
  const forward: any = {}

  let count = 0
  eachProp(props, (value, prop) => {
    if (!RESERVED_PROPS[prop]) {
      forward[prop] = value
      count++
    }
  })

  if (count) {
    return forward
  }
}

/**
 * Clone the given `props` and move all non-reserved props
 * into the `to` prop.
 */
export function inferTo<T extends object>(props: T): InferTo<T> {
  const to = getForwardProps(props)
  if (to) {
    const out: any = { to }
    eachProp(props, (val, key) => key in to || (out[key] = val))
    return out
  }
  return { ...props } as any
}

// Compute the goal value, converting "red" to "rgba(255, 0, 0, 1)" in the process
export function computeGoal<T>(value: T | FluidValue<T>): T {
  value = getFluidValue(value)
  return is.arr(value)
    ? value.map(computeGoal)
    : isAnimatedString(value)
      ? (G.createStringInterpolator({
          range: [0, 1],
          output: [value, value] as any,
        })(1) as any)
      : value
}

export function hasProps(props: object) {
  for (const _ in props) return true
  return false
}

export function isAsyncTo(to: any) {
  return is.fun(to) || (is.arr(to) && is.obj(to[0]))
}

/** Detach `ctrl` from `ctrl.ref` and (optionally) the given `ref` */
export function detachRefs(ctrl: Controller, ref?: SpringRef) {
  ctrl.ref?.delete(ctrl)
  ref?.delete(ctrl)
}

/** Replace `ctrl.ref` with the given `ref` (if defined) */
export function replaceRef(ctrl: Controller, ref?: SpringRef) {
  if (ref && ctrl.ref !== ref) {
    ctrl.ref?.delete(ctrl)
    ref.add(ctrl)
    ctrl.ref = ref
  }
}