File size: 1,155 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 |
import { AnimatedValue } from './AnimatedValue'
import { is, createInterpolator } from '@react-spring/shared'
type Value = string | number
export class AnimatedString extends AnimatedValue<Value> {
declare protected _value: number
protected _string: string | null = null
protected _toString: (input: number) => string
constructor(value: string) {
super(0)
this._toString = createInterpolator({
output: [value, value],
})
}
/** @internal */
static create(value: string) {
return new AnimatedString(value)
}
getValue() {
const value = this._string
return value == null ? (this._string = this._toString(this._value)) : value
}
setValue(value: Value) {
if (is.str(value)) {
if (value == this._string) {
return false
}
this._string = value
this._value = 1
} else if (super.setValue(value)) {
this._string = null
} else {
return false
}
return true
}
reset(goal?: string) {
if (goal) {
this._toString = createInterpolator({
output: [this.getValue(), goal],
})
}
this._value = 0
super.reset()
}
}
|