File size: 2,348 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 { raf, Rafz } from '@react-spring/rafz'
import {
  OneOrMore,
  InterpolatorConfig,
  InterpolatorArgs,
} from '@react-spring/types'

import { FluidValue } from './fluids'
import type { OpaqueAnimation } from './FrameLoop'
import { noop } from './helpers'

//
// Required
//

export let createStringInterpolator: (
  config: InterpolatorConfig<string>
) => (input: number) => string

//
// Optional
//

export let to: <Input, Output>(
  source: OneOrMore<FluidValue>,
  args: InterpolatorArgs<Input, Output>
) => FluidValue<Output>

export let colors = null as { [key: string]: number } | null

export let skipAnimation = false as boolean

export let willAdvance: (animation: OpaqueAnimation) => void = noop

//
// Configuration
//

export interface AnimatedGlobals {
  /** Returns a new `Interpolation` object */
  to?: typeof to
  /** Used to measure frame length. Read more [here](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now) */
  now?: typeof raf.now
  /** Provide custom color names for interpolation */
  colors?: typeof colors
  /** Make all animations instant and skip the frameloop entirely */
  skipAnimation?: typeof skipAnimation
  /** Provide custom logic for string interpolation */
  createStringInterpolator?: typeof createStringInterpolator
  /** Schedule a function to run on the next frame */
  requestAnimationFrame?: (cb: () => void) => void
  /** Event props are called with `batchedUpdates` to reduce extraneous renders */
  batchedUpdates?: typeof raf.batchedUpdates
  /** @internal Exposed for testing purposes */
  willAdvance?: typeof willAdvance
  /** sets the global frameLoop setting for the global raf instance */
  frameLoop?: Rafz['frameLoop']
}

export const assign = (globals: AnimatedGlobals) => {
  if (globals.to) to = globals.to
  if (globals.now) raf.now = globals.now
  if (globals.colors !== undefined) colors = globals.colors
  if (globals.skipAnimation != null) skipAnimation = globals.skipAnimation
  if (globals.createStringInterpolator)
    createStringInterpolator = globals.createStringInterpolator
  if (globals.requestAnimationFrame) raf.use(globals.requestAnimationFrame)
  if (globals.batchedUpdates) raf.batchedUpdates = globals.batchedUpdates
  if (globals.willAdvance) willAdvance = globals.willAdvance
  if (globals.frameLoop) raf.frameLoop = globals.frameLoop
}