File size: 2,117 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
import { Lookup, Remap } from '@react-spring/types'
import { is } from '@react-spring/shared'

import { ControllerUpdate, PickAnimated, SpringValues } from '../types'
import { Valid } from '../types/common'
import { SpringRef } from '../SpringRef'
import { useSprings } from './useSprings'

/**
 * The props that `useSpring` recognizes.
 */
export type UseSpringProps<Props extends object = any> = unknown &
  PickAnimated<Props> extends infer State
  ? State extends Lookup
    ? Remap<
        ControllerUpdate<State> & {
          /**
           * Used to access the imperative API.
           *
           * When defined, the render animation won't auto-start.
           */
          ref?: SpringRef<State>
        }
      >
    : never
  : never

/**
 * The `props` function is only called on the first render, unless
 * `deps` change (when defined). State is inferred from forward props.
 */
export function useSpring<Props extends object>(
  props:
    | Function
    | (() => (Props & Valid<Props, UseSpringProps<Props>>) | UseSpringProps),
  deps?: readonly any[] | undefined
): PickAnimated<Props> extends infer State
  ? State extends Lookup
    ? [SpringValues<State>, SpringRef<State>]
    : never
  : never

/**
 * Updated on every render, with state inferred from forward props.
 */
export function useSpring<Props extends object>(
  props: (Props & Valid<Props, UseSpringProps<Props>>) | UseSpringProps
): SpringValues<PickAnimated<Props>>

/**
 * Updated only when `deps` change, with state inferred from forward props.
 */
export function useSpring<Props extends object>(
  props: (Props & Valid<Props, UseSpringProps<Props>>) | UseSpringProps,
  deps: readonly any[] | undefined
): PickAnimated<Props> extends infer State
  ? State extends Lookup
    ? [SpringValues<State>, SpringRef<State>]
    : never
  : never

/** @internal */
export function useSpring(props: any, deps?: readonly any[]) {
  const isFn = is.fun(props)
  const [[values], ref] = useSprings(
    1,
    isFn ? props : [props],
    isFn ? deps || [] : deps
  )
  return isFn || arguments.length == 2 ? [values, ref] : values
}