UNPKG

@tamagui/react-native-web-lite

Version:
125 lines (124 loc) 4.67 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __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: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var TouchableOpacity_exports = {}; __export(TouchableOpacity_exports, { TouchableOpacity: () => TouchableOpacity }); module.exports = __toCommonJS(TouchableOpacity_exports); var React = __toESM(require("react"), 1); var import_react = require("react"); var import_react_native_web_internals = require("@tamagui/react-native-web-internals"); var import_View = require("./View/index.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); function TouchableOpacityImpl(props, forwardedRef) { const { activeOpacity, delayPressIn, delayPressOut, delayLongPress, disabled, focusable, onLongPress, onPress, onPressIn, onPressOut, rejectResponderTermination, style, ...rest } = props; const hostRef = (0, import_react.useRef)(null); const setRef = (0, import_react_native_web_internals.useMergeRefs)(forwardedRef, hostRef); const [duration, setDuration] = (0, import_react.useState)("0s"); const [opacityOverride, setOpacityOverride] = (0, import_react.useState)(null); const setOpacityTo = (0, import_react.useCallback)((value, duration2) => { setOpacityOverride(value); setDuration(duration2 ? `${duration2 / 1e3}s` : "0s"); }, [setOpacityOverride, setDuration]); const setOpacityActive = (0, import_react.useCallback)(duration2 => { setOpacityTo(activeOpacity ?? 0.2, duration2); }, [activeOpacity, setOpacityTo]); const setOpacityInactive = (0, import_react.useCallback)(duration2 => { setOpacityTo(null, duration2); }, [setOpacityTo]); const pressConfig = (0, import_react.useMemo)(() => ({ cancelable: !rejectResponderTermination, disabled, delayLongPress, delayPressStart: delayPressIn, delayPressEnd: delayPressOut, onLongPress, onPress, onPressStart(event) { const isGrant = event.dispatchConfig != null ? event.dispatchConfig.registrationName === "onResponderGrant" : event.type === "keydown"; setOpacityActive(isGrant ? 0 : 150); if (onPressIn != null) { onPressIn(event); } }, onPressEnd(event) { setOpacityInactive(250); if (onPressOut != null) { onPressOut(event); } } }), [delayLongPress, delayPressIn, delayPressOut, disabled, onLongPress, onPress, onPressIn, onPressOut, rejectResponderTermination, setOpacityActive, setOpacityInactive]); const pressEventHandlers = (0, import_react_native_web_internals.usePressEvents)(hostRef, pressConfig); return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_View.View, { ...rest, ...pressEventHandlers, accessibilityDisabled: disabled, focusable: !disabled && focusable !== false, ref: setRef, style: [styles.root, !disabled && styles.actionable, style, opacityOverride != null && { opacity: opacityOverride }, { transitionDuration: duration }] }); } const styles = import_react_native_web_internals.StyleSheet.create({ root: { transitionProperty: "opacity", transitionDuration: "0.15s", userSelect: "none" }, actionable: { cursor: "pointer", touchAction: "manipulation" } }); const TouchableOpacity = React.memo(React.forwardRef(TouchableOpacityImpl)); TouchableOpacity.displayName = "TouchableOpacity"; TouchableOpacity.Mixin = {};