@tamagui/react-native-web-lite
Version:
React Native for Web
239 lines (238 loc) • 7.43 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var AnimatedValue_exports = {};
__export(AnimatedValue_exports, {
AnimatedValue: () => AnimatedValue,
default: () => AnimatedValue_default
});
module.exports = __toCommonJS(AnimatedValue_exports);
var import_AnimatedInterpolation = require("./AnimatedInterpolation.cjs");
var import_AnimatedWithChildren = require("./AnimatedWithChildren.cjs");
var import_react_native_web_internals = require("@tamagui/react-native-web-internals");
var import_NativeAnimatedHelper = require("../NativeAnimatedHelper.cjs");
const NativeAnimatedAPI = import_NativeAnimatedHelper.NativeAnimatedHelper.API;
function _flush(rootNode) {
const animatedStyles = /* @__PURE__ */new Set();
function findAnimatedStyles(node) {
if (typeof node.update === "function") {
animatedStyles.add(node);
} else {
node.__getChildren().forEach(findAnimatedStyles);
}
}
findAnimatedStyles(rootNode);
animatedStyles.forEach(animatedStyle => animatedStyle.update());
}
function _executeAsAnimatedBatch(id, operation) {
NativeAnimatedAPI.setWaitingForIdentifier(id);
operation();
NativeAnimatedAPI.unsetWaitingForIdentifier(id);
}
class AnimatedValue extends import_AnimatedWithChildren.AnimatedWithChildren {
_value;
_startingValue;
_offset;
_animation;
_tracking;
constructor(value, config) {
super();
if (typeof value !== "number") {
throw new Error("AnimatedValue: Attempting to set value to undefined");
}
this._startingValue = this._value = value;
this._offset = 0;
this._animation = null;
if (config && config.useNativeDriver) {
this.__makeNative();
}
}
__detach() {
if (this.__isNative) {
NativeAnimatedAPI.getValue(this.__getNativeTag(), value => {
this._value = value - this._offset;
});
}
this.stopAnimation();
super.__detach();
}
__getValue() {
return this._value + this._offset;
}
/**
* Directly set the value. This will stop any animations running on the value
* and update all the bound properties.
*
* See https://reactnative.dev/docs/animatedvalue#setvalue
*/
setValue(value) {
if (this._animation) {
this._animation.stop();
this._animation = null;
}
this._updateValue(value, !this.__isNative
/* don't perform a flush for natively driven values */);
if (this.__isNative) {
_executeAsAnimatedBatch(this.__getNativeTag().toString(), () => NativeAnimatedAPI.setAnimatedNodeValue(this.__getNativeTag(), value));
}
}
/**
* Sets an offset that is applied on top of whatever value is set, whether via
* `setValue`, an animation, or `Animated.event`. Useful for compensating
* things like the start of a pan gesture.
*
* See https://reactnative.dev/docs/animatedvalue#setoffset
*/
setOffset(offset) {
this._offset = offset;
if (this.__isNative) {
NativeAnimatedAPI.setAnimatedNodeOffset(this.__getNativeTag(), offset);
}
}
/**
* Merges the offset value into the base value and resets the offset to zero.
* The final output of the value is unchanged.
*
* See https://reactnative.dev/docs/animatedvalue#flattenoffset
*/
flattenOffset() {
this._value += this._offset;
this._offset = 0;
if (this.__isNative) {
NativeAnimatedAPI.flattenAnimatedNodeOffset(this.__getNativeTag());
}
}
/**
* Sets the offset value to the base value, and resets the base value to zero.
* The final output of the value is unchanged.
*
* See https://reactnative.dev/docs/animatedvalue#extractoffset
*/
extractOffset() {
this._offset += this._value;
this._value = 0;
if (this.__isNative) {
NativeAnimatedAPI.extractAnimatedNodeOffset(this.__getNativeTag());
}
}
/**
* Stops any running animation or tracking. `callback` is invoked with the
* final value after stopping the animation, which is useful for updating
* state to match the animation position with layout.
*
* See https://reactnative.dev/docs/animatedvalue#stopanimation
*/
stopAnimation(callback) {
this.stopTracking();
this._animation && this._animation.stop();
this._animation = null;
if (callback) {
if (this.__isNative) {
NativeAnimatedAPI.getValue(this.__getNativeTag(), callback);
} else {
callback(this.__getValue());
}
}
}
/**
* Stops any animation and resets the value to its original.
*
* See https://reactnative.dev/docs/animatedvalue#resetanimation
*/
resetAnimation(callback) {
this.stopAnimation(callback);
this._value = this._startingValue;
if (this.__isNative) {
NativeAnimatedAPI.setAnimatedNodeValue(this.__getNativeTag(), this._startingValue);
}
}
__onAnimatedValueUpdateReceived(value) {
this._updateValue(value, false
/*flush*/);
}
/**
* Interpolates the value before updating the property, e.g. mapping 0-1 to
* 0-10.
*/
interpolate(config) {
return new import_AnimatedInterpolation.AnimatedInterpolation(this, config);
}
/**
* Typically only used internally, but could be used by a custom Animation
* class.
*
* See https://reactnative.dev/docs/animatedvalue#animate
*/
animate(animation, callback) {
let handle = null;
if (animation.__isInteraction) {
handle = import_react_native_web_internals.InteractionManager.createInteractionHandle();
}
const previousAnimation = this._animation;
this._animation && this._animation.stop();
this._animation = animation;
animation.start(this._value, value => {
this._updateValue(value, true
/* flush */);
}, result => {
this._animation = null;
if (handle !== null) {
import_react_native_web_internals.InteractionManager.clearInteractionHandle(handle);
}
callback && callback(result);
}, previousAnimation, this);
}
/**
* Typically only used internally.
*/
stopTracking() {
this._tracking && this._tracking.__detach();
this._tracking = null;
}
/**
* Typically only used internally.
*/
track(tracking) {
this.stopTracking();
this._tracking = tracking;
this._tracking && this._tracking.update();
}
_updateValue(value, flush) {
if (value === void 0) {
throw new Error("AnimatedValue: Attempting to set value to undefined");
}
this._value = value;
if (flush) {
_flush(this);
}
super.__callListeners(this.__getValue());
}
__getNativeConfig() {
return {
type: "value",
value: this._value,
offset: this._offset
};
}
}
var AnimatedValue_default = AnimatedValue;