UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

221 lines (220 loc) 9.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TinderPhotoSwiperCard = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _reactNativeGestureHandler = require("react-native-gesture-handler"); var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } const dimensions = _reactNative.Dimensions.get('screen'); function TinderPhotoSwiperCardInner(_ref) { let { status, likeActivationValue, dislikeActivationValue, favouriteActivationValue, photo, imageProps, states, resetAnimation = { duration: 300, easing: _reactNativeReanimated.Easing.out(_reactNativeReanimated.Easing.ease) }, swipeAnimation = { duration: 700, easing: _reactNativeReanimated.Easing.out(_reactNativeReanimated.Easing.ease) }, maxActivationTranslation = { x: dimensions.width / 4, y: dimensions.height / 5 }, onLike, onDislike, onFavorite, ...props } = _ref; const state = (0, _reactNativeReanimated.useSharedValue)(states[status]); const handleFavorite = () => { // @ts-ignore onFavorite === null || onFavorite === void 0 ? void 0 : onFavorite(); }; const pan = _reactNativeGestureHandler.Gesture.Pan().onUpdate(e => { state.value = { ...state.value, left: e.translationX, top: e.translationY }; dislikeActivationValue.value = Math.abs(Math.min(e.translationX, 0)) / maxActivationTranslation.x; likeActivationValue.value = Math.abs(Math.max(e.translationX, 0)) / maxActivationTranslation.x; favouriteActivationValue.value = (0, _reactNativeReanimated.interpolate)(Math.abs(Math.min(e.translationY, 0)) / maxActivationTranslation.y, [0, 1], !(photo !== null && photo !== void 0 && photo.favorite) ? [0, 1] : [1, 0], _reactNativeReanimated.Extrapolate.CLAMP); }).onEnd(e => { if (status !== 'active') { return; } dislikeActivationValue.value = (0, _reactNativeReanimated.withTiming)(0, resetAnimation); likeActivationValue.value = (0, _reactNativeReanimated.withTiming)(0, resetAnimation); if (Math.abs(Math.min(e.translationX, 0)) > maxActivationTranslation.x) { (0, _reactNativeReanimated.runOnJS)(onDislike)(); return; } if (Math.abs(Math.max(e.translationX, 0)) > maxActivationTranslation.x) { (0, _reactNativeReanimated.runOnJS)(onLike)(); return; } if (Math.abs(Math.min(e.translationY, 0)) > maxActivationTranslation.y && photo) { (0, _reactNativeReanimated.runOnJS)(handleFavorite)(); } else { favouriteActivationValue.value = (0, _reactNativeReanimated.interpolate)(0, [0, 1], !(photo !== null && photo !== void 0 && photo.favorite) ? [0, 1] : [1, 0], _reactNativeReanimated.Extrapolate.CLAMP); } state.value = (0, _reactNativeReanimated.withTiming)({ ...state.value, left: 0, top: 0 }, resetAnimation); }).enabled(status === 'active'); const animatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({ left: state.value.left ?? 0, top: state.value.top ?? 0, opacity: state.value.opacity ?? 1, transform: [{ scale: state.value.scale ?? 1 }, { rotate: `${state.value.rotation ?? 0}deg` }] }), []); const overlayAnimatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({ ...(state.value.overlayColor && { backgroundColor: `rgba(${state.value.overlayColor[0]}, ${state.value.overlayColor[1]}, ${state.value.overlayColor[2]}, ${state.value.overlayColor[3]})` }) }), []); (0, _react.useEffect)(() => { const newState = states[status]; state.value = (0, _reactNativeReanimated.withTiming)(newState, swipeAnimation); }, [status]); (0, _react.useEffect)(() => { if (status !== 'active') { return; } favouriteActivationValue.value = photo !== null && photo !== void 0 && photo.favorite ? 1 : 0; }, [status, photo === null || photo === void 0 ? void 0 : photo.favorite]); return /*#__PURE__*/_react.default.createElement(_reactNativeGestureHandler.GestureDetector, { gesture: pan }, /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, _extends({}, props, { pointerEvents: status === 'active' ? 'auto' : 'none', style: [{ position: 'absolute', left: 0, top: 0, width: '100%', height: '100%' }, props.style, animatedStyle] }), photo && /*#__PURE__*/_react.default.createElement(_reactNative.Image, _extends({ resizeMode: "cover" }, imageProps, { source: { uri: photo.uri }, style: [{ flex: 1, backgroundColor: 'rgba(255,255,255,0.05)' }, imageProps === null || imageProps === void 0 ? void 0 : imageProps.style] })), /*#__PURE__*/_react.default.createElement(_reactNativeReanimated.default.View, { style: [{ position: 'absolute', left: -3, right: -3, top: -3, bottom: -3 }, overlayAnimatedStyle, imageProps === null || imageProps === void 0 ? void 0 : imageProps.style] }))); } function TinderPhotoSwiperCardWrapper(_ref2, ref) { let { likeActivationValue, dislikeActivationValue, favouriteActivationValue, photo: initialPhoto, imageProps, states, resetAnimation = { duration: 300, easing: _reactNativeReanimated.Easing.out(_reactNativeReanimated.Easing.ease) }, swipeAnimation = { duration: 700, easing: _reactNativeReanimated.Easing.out(_reactNativeReanimated.Easing.ease) }, maxActivationTranslation = { x: dimensions.width / 4, y: dimensions.height / 4 }, onLike, onDislike, onFavorite, ...props } = _ref2; const photoLoaderRef = (0, _react.useRef)(initialPhoto); const [photo, setPhoto] = (0, _react.useState)(!photoLoaderRef.current.loader ? photoLoaderRef.current : null); const [status, setStatus] = (0, _react.useState)('inactive'); const [isVisible, setIsVisible] = (0, _react.useState)(false); const isPhotoLoaded = (0, _react.useRef)(false); const toggleFavorite = () => { setPhoto(x => x ? { ...x, favorite: !x.favorite } : x); }; (0, _react.useImperativeHandle)(ref, () => ({ setStatus(value) { setStatus(value); }, setVisibility(value) { setIsVisible(value); }, toggleFavorite }), []); (0, _react.useEffect)(() => { if (!photoLoaderRef.current.loader || !isVisible || photo) { return; } (async () => { const value = await photoLoaderRef.current.loader(); setPhoto(value); })(); }, [photo, isVisible]); (0, _react.useEffect)(() => { if (!photo) { return; } if (!isPhotoLoaded.current) { isPhotoLoaded.current = true; return; } onFavorite(photo, !!photo.favorite); }, [photo === null || photo === void 0 ? void 0 : photo.favorite]); if (!isVisible) { return null; } return /*#__PURE__*/_react.default.createElement(TinderPhotoSwiperCardInner, _extends({ status, likeActivationValue, dislikeActivationValue, favouriteActivationValue, photo, imageProps, states, resetAnimation, swipeAnimation, maxActivationTranslation, onLike, onDislike, onFavorite: toggleFavorite }, props)); } const TinderPhotoSwiperCard = /*#__PURE__*/(0, _react.forwardRef)(TinderPhotoSwiperCardWrapper); exports.TinderPhotoSwiperCard = TinderPhotoSwiperCard; //# sourceMappingURL=TinderPhotoSwiperCard.js.map