File size: 2,779 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
import { InterpolatorConfig } from '@react-spring/types'

import { getFluidValue } from './fluids'
import { createInterpolator } from './createInterpolator'
import { colorToRgba } from './colorToRgba'
import * as G from './globals'
import {
  cssVariableRegex,
  colorRegex,
  unitRegex,
  numberRegex,
  rgbaRegex,
} from './regexs'
import { variableToRgba } from './variableToRgba'

// Covers color names (transparent, blue, etc.)
let namedColorRegex: RegExp

// rgba requires that the r,g,b are integers.... so we want to round them,
// but we *dont* want to round the opacity (4th column).
const rgbaRound = (_: any, p1: number, p2: number, p3: number, p4: number) =>
  `rgba(${Math.round(p1)}, ${Math.round(p2)}, ${Math.round(p3)}, ${p4})`

/**
 * Supports string shapes by extracting numbers so new values can be computed,
 * and recombines those values into new strings of the same shape.  Supports
 * things like:
 *
 *     "rgba(123, 42, 99, 0.36)"           // colors
 *     "-45deg"                            // values with units
 *     "0 2px 2px 0px rgba(0, 0, 0, 0.12)" // CSS box-shadows
 *     "rotate(0deg) translate(2px, 3px)"  // CSS transforms
 */
export const createStringInterpolator = (
  config: InterpolatorConfig<string>
) => {
  if (!namedColorRegex)
    namedColorRegex = G.colors
      ? // match color names, ignore partial matches
        new RegExp(`(${Object.keys(G.colors).join('|')})(?!\\w)`, 'g')
      : // never match
        /^\b$/

  // Convert colors to rgba(...)
  const output = config.output.map(value => {
    return getFluidValue(value)
      .replace(cssVariableRegex, variableToRgba)
      .replace(colorRegex, colorToRgba)
      .replace(namedColorRegex, colorToRgba)
  })

  // Convert ["1px 2px", "0px 0px"] into [[1, 2], [0, 0]]
  const keyframes = output.map(value => value.match(numberRegex)!.map(Number))

  // Convert ["1px 2px", "0px 0px"] into [[1, 0], [2, 0]]
  const outputRanges = keyframes[0].map((_, i) =>
    keyframes.map(values => {
      if (!(i in values)) {
        throw Error('The arity of each "output" value must be equal')
      }
      return values[i]
    })
  )

  // Create an interpolator for each animated number
  const interpolators = outputRanges.map(output =>
    createInterpolator({ ...config, output })
  )

  // Use the first `output` as a template for each call
  return (input: number) => {
    // Convert numbers to units if available (allows for ["0", "100%"])
    const missingUnit =
      !unitRegex.test(output[0]) &&
      output.find(value => unitRegex.test(value))?.replace(numberRegex, '')

    let i = 0
    return output[0]
      .replace(
        numberRegex,
        () => `${interpolators[i++](input)}${missingUnit || ''}`
      )
      .replace(rgbaRegex, rgbaRound)
  }
}