|
|
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' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const domTransforms = /^(matrix|translate|scale|rotate|skew)/ |
|
|
|
|
|
|
|
|
const pxTransforms = /^(translate)/ |
|
|
|
|
|
|
|
|
const degTransforms = /^(rotate|skew)/ |
|
|
|
|
|
type Value = number | string |
|
|
|
|
|
|
|
|
const addUnit = (value: Value, unit: string): string | 0 => |
|
|
is.num(value) && value !== 0 ? value + unit : value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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])[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class AnimatedStyle extends AnimatedObject { |
|
|
constructor({ x, y, z, ...style }: Lookup) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const inputs: Inputs = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const transforms: Transforms = [] |
|
|
|
|
|
|
|
|
if (x || y || z) { |
|
|
inputs.push([x || 0, y || 0, z || 0]) |
|
|
transforms.push((xyz: Value[]) => [ |
|
|
`translate3d(${xyz.map(v => addUnit(v, 'px')).join(',')})`, |
|
|
isValueIdentity(xyz, 0), |
|
|
]) |
|
|
} |
|
|
|
|
|
|
|
|
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) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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 |
|
|
} |
|
|
|
|
|
|
|
|
protected observerAdded(count: number) { |
|
|
if (count == 1) |
|
|
each(this.inputs, input => |
|
|
each( |
|
|
input, |
|
|
value => hasFluidValue(value) && addFluidObserver(value, this) |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
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) |
|
|
} |
|
|
} |
|
|
|