UNPKG

@tamagui/react-native-web-lite

Version:
175 lines (174 loc) 8.76 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target, mod )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var AnimatedEvent_exports = {}; __export(AnimatedEvent_exports, { AnimatedEvent: () => AnimatedEvent, attachNativeEvent: () => attachNativeEvent }); module.exports = __toCommonJS(AnimatedEvent_exports); var import_AnimatedValue = __toESM(require("./nodes/AnimatedValue")), import_NativeAnimatedHelper = __toESM(require("./NativeAnimatedHelper")), import_react_native_web_internals = require("@tamagui/react-native-web-internals"), import_NativeAnimatedHelper2 = require("./NativeAnimatedHelper"); function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } function _define_property(obj, key, value) { return key in obj ? Object.defineProperty(obj, key, { value, enumerable: !0, configurable: !0, writable: !0 }) : obj[key] = value, obj; } var __DEV__ = process.env.NODE_ENV !== "production"; function attachNativeEvent(viewRef, eventName, argMapping) { var eventMappings = [], traverse = function(value, path) { if (value instanceof import_AnimatedValue.default) value.__makeNative(), eventMappings.push({ nativeEventPath: path, animatedValueTag: value.__getNativeTag() }); else if (typeof value == "object") for (var key in value) traverse(value[key], path.concat(key)); }; return (0, import_react_native_web_internals.invariant)(argMapping[0] && argMapping[0].nativeEvent, "Native driven events only support animated values contained inside `nativeEvent`."), traverse(argMapping[0].nativeEvent, []), viewRef != null && eventMappings.forEach(function(mapping) { import_NativeAnimatedHelper.default.API.addAnimatedEventToView(viewRef, eventName, mapping); }), { detach() { viewRef != null && eventMappings.forEach(function(mapping) { import_NativeAnimatedHelper.default.API.removeAnimatedEventFromView(viewRef, eventName, mapping.animatedValueTag); }); } }; } function validateMapping(argMapping, args) { var validate = function(recMapping, recEvt, key) { if (recMapping instanceof import_AnimatedValue.default) { (0, import_react_native_web_internals.invariant)(typeof recEvt == "number", "Bad mapping of event key " + key + ", should be number but got " + typeof recEvt); return; } if (typeof recEvt == "number") { (0, import_react_native_web_internals.invariant)(recMapping instanceof import_AnimatedValue.default, "Bad mapping of type " + typeof recMapping + " for key " + key + ", event value must map to AnimatedValue"); return; } (0, import_react_native_web_internals.invariant)(typeof recMapping == "object", "Bad mapping of type " + typeof recMapping + " for key " + key), (0, import_react_native_web_internals.invariant)(typeof recEvt == "object", "Bad event of type " + typeof recEvt + " for key " + key); for (var mappingKey in recMapping) validate(recMapping[mappingKey], recEvt[mappingKey], mappingKey); }; (0, import_react_native_web_internals.invariant)(args.length >= argMapping.length, "Event has less arguments than mapping"), argMapping.forEach(function(mapping, idx) { validate(mapping, args[idx], "arg" + idx); }); } var AnimatedEvent = /* @__PURE__ */ function() { function AnimatedEvent2(argMapping, config) { _class_call_check(this, AnimatedEvent2), _define_property(this, "_argMapping", void 0), _define_property(this, "_listeners", []), _define_property(this, "_attachedEvent", void 0), _define_property(this, "__isNative", void 0), this._argMapping = argMapping, config == null && (console.warn("Animated.event now requires a second argument for options"), config = { useNativeDriver: !1 }), config.listener && this.__addListener(config.listener), this._callListeners = this._callListeners.bind(this), this._attachedEvent = null, this.__isNative = (0, import_NativeAnimatedHelper2.shouldUseNativeDriver)(config); } return _create_class(AnimatedEvent2, [ { key: "__addListener", value: function(callback) { this._listeners.push(callback); } }, { key: "__removeListener", value: function(callback) { this._listeners = this._listeners.filter(function(listener) { return listener !== callback; }); } }, { key: "__attach", value: function(viewRef, eventName) { (0, import_react_native_web_internals.invariant)(this.__isNative, "Only native driven events need to be attached."), this._attachedEvent = attachNativeEvent(viewRef, eventName, this._argMapping); } }, { key: "__detach", value: function(viewTag, eventName) { (0, import_react_native_web_internals.invariant)(this.__isNative, "Only native driven events need to be detached."), this._attachedEvent && this._attachedEvent.detach(); } }, { key: "__getHandler", value: function() { var _this = this; if (this.__isNative) if (__DEV__) { var _this1 = this, validatedMapping = !1; return function() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) args[_key] = arguments[_key]; validatedMapping || (validateMapping(_this1._argMapping, args), validatedMapping = !0), _this1._callListeners(...args); }; } else return this._callListeners; var validatedMapping1 = !1; return function() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) args[_key] = arguments[_key]; __DEV__ && !validatedMapping1 && (validateMapping(_this._argMapping, args), validatedMapping1 = !0); var traverse = function(recMapping, recEvt, key) { if (recMapping instanceof import_AnimatedValue.default) typeof recEvt == "number" && recMapping.setValue(recEvt); else if (typeof recMapping == "object") for (var mappingKey in recMapping) traverse(recMapping[mappingKey], recEvt[mappingKey], mappingKey); }; _this._argMapping.forEach(function(mapping, idx) { traverse(mapping, args[idx], "arg" + idx); }), _this._callListeners(...args); }; } }, { key: "_callListeners", value: function() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) args[_key] = arguments[_key]; this._listeners.forEach(function(listener) { return listener(...args); }); } } ]), AnimatedEvent2; }(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AnimatedEvent, attachNativeEvent }); //# sourceMappingURL=AnimatedEvent.js.map