File size: 5,080 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
import { AnimatedObject } from '@react-spring/animated'
import { Lookup, OneOrMore } from '@react-spring/types'
import {
  is,
  each,
  toArray,
  eachProp,
  FluidValue,
  FluidEvent,
  getFluidValue,
  callFluidObservers,
  hasFluidValue,
  addFluidObserver,
  removeFluidObserver,
} from '@react-spring/shared'

/** The transform-functions
 * (https://developer.mozilla.org/fr/docs/Web/CSS/transform-function)
 * that you can pass as keys to your animated component style and that will be
 * animated. Perspective has been left out as it would conflict with the
 * non-transform perspective style.
 */
const domTransforms = /^(matrix|translate|scale|rotate|skew)/

// These keys have "px" units by default
const pxTransforms = /^(translate)/

// These keys have "deg" units by default
const degTransforms = /^(rotate|skew)/

type Value = number | string

/** Add a unit to the value when the value is unit-less (eg: a number) */
const addUnit = (value: Value, unit: string): string | 0 =>
  is.num(value) && value !== 0 ? value + unit : value

/**
 * Checks if the input value matches the identity value.
 *
 *     isValueIdentity(0, 0)              // => true
 *     isValueIdentity('0px', 0)          // => true
 *     isValueIdentity([0, '0px', 0], 0)  // => true
 */
const isValueIdentity = (value: OneOrMore<Value>, id: number): boolean =>
  is.arr(value)
    ? value.every(v => isValueIdentity(v, id))
    : is.num(value)
      ? value === id
      : parseFloat(value) === id

type Inputs = ReadonlyArray<Value | FluidValue<Value>>[]
type Transforms = ((value: any) => [string, boolean])[]

/**
 * This AnimatedStyle will simplify animated components transforms by
 * interpolating all transform function passed as keys in the style object
 * including shortcuts such as x, y and z for translateX/Y/Z
 */
export class AnimatedStyle extends AnimatedObject {
  constructor({ x, y, z, ...style }: Lookup) {
    /**
     * An array of arrays that contains the values (static or fluid)
     * used by each transform function.
     */
    const inputs: Inputs = []
    /**
     * An array of functions that take a list of values (static or fluid)
     * and returns (1) a CSS transform string and (2) a boolean that's true
     * when the transform has no effect (eg: an identity transform).
     */
    const transforms: Transforms = []

    // Combine x/y/z into translate3d
    if (x || y || z) {
      inputs.push([x || 0, y || 0, z || 0])
      transforms.push((xyz: Value[]) => [
        `translate3d(${xyz.map(v => addUnit(v, 'px')).join(',')})`, // prettier-ignore
        isValueIdentity(xyz, 0),
      ])
    }

    // Pluck any other transform-related props
    eachProp(style, (value, key) => {
      if (key === 'transform') {
        inputs.push([value || ''])
        transforms.push((transform: string) => [transform, transform === ''])
      } else if (domTransforms.test(key)) {
        delete style[key]
        if (is.und(value)) return

        const unit = pxTransforms.test(key)
          ? 'px'
          : degTransforms.test(key)
            ? 'deg'
            : ''

        inputs.push(toArray(value))
        transforms.push(
          key === 'rotate3d'
            ? ([x, y, z, deg]: [number, number, number, Value]) => [
                `rotate3d(${x},${y},${z},${addUnit(deg, unit)})`,
                isValueIdentity(deg, 0),
              ]
            : (input: Value[]) => [
                `${key}(${input.map(v => addUnit(v, unit)).join(',')})`,
                isValueIdentity(input, key.startsWith('scale') ? 1 : 0),
              ]
        )
      }
    })

    if (inputs.length) {
      style.transform = new FluidTransform(inputs, transforms)
    }

    super(style)
  }
}

/** @internal */
class FluidTransform extends FluidValue<string> {
  protected _value: string | null = null

  constructor(
    readonly inputs: Inputs,
    readonly transforms: Transforms
  ) {
    super()
  }

  get() {
    return this._value || (this._value = this._get())
  }

  protected _get() {
    let transform = ''
    let identity = true
    each(this.inputs, (input, i) => {
      const arg1 = getFluidValue(input[0])
      const [t, id] = this.transforms[i](
        is.arr(arg1) ? arg1 : input.map(getFluidValue)
      )
      transform += ' ' + t
      identity = identity && id
    })
    return identity ? 'none' : transform
  }

  // Start observing our inputs once we have an observer.
  protected observerAdded(count: number) {
    if (count == 1)
      each(this.inputs, input =>
        each(
          input,
          value => hasFluidValue(value) && addFluidObserver(value, this)
        )
      )
  }

  // Stop observing our inputs once we have no observers.
  protected observerRemoved(count: number) {
    if (count == 0)
      each(this.inputs, input =>
        each(
          input,
          value => hasFluidValue(value) && removeFluidObserver(value, this)
        )
      )
  }

  eventObserved(event: FluidEvent) {
    if (event.type == 'change') {
      this._value = null
    }
    callFluidObservers(this, event)
  }
}