UNPKG

react-native-zoom-toolkit

Version:

Smoothly zoom any image, video or component you want!

176 lines (174 loc) 7.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _reactNativeReanimated = require("react-native-reanimated"); var _reactNativeGestureHandler = require("react-native-gesture-handler"); var _types = require("../../commons/types"); var _clamp = require("../../commons/utils/clamp"); var _getMaxScale = require("../../commons/utils/getMaxScale"); var _getPinchPanningStatus = require("../../commons/utils/getPinchPanningStatus"); var _Reflection = _interopRequireDefault(require("./Reflection")); var _GalleryItem = _interopRequireDefault(require("./GalleryItem")); var _context = require("./context"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const Gallery = props => { const { reference, data, renderItem, keyExtractor, initialIndex = 0, windowSize = 5, maxScale: userMaxScale = 6, vertical = false, tapOnEdgeToItem = true, pinchCenteringMode = _types.PinchCenteringMode.CLAMP, allowPinchPanning: pinchPanning, customTransition, onIndexChange, onScroll, onTap, onPanStart, onPanEnd, onPinchStart, onPinchEnd, onSwipe, onZoomBegin, onZoomEnd, onVerticalPull } = props; const allowPinchPanning = pinchPanning ?? (0, _getPinchPanningStatus.getPinchPanningStatus)(); const nextItems = Math.floor(windowSize / 2); const [scrollIndex, setScrollIndex] = (0, _react.useState)(initialIndex); const { activeIndex, fetchIndex, rootSize, rootChildSize, scroll, translate, scale, hasZoomed } = (0, _react.useContext)(_context.GalleryContext); const itemSize = (0, _reactNativeReanimated.useDerivedValue)(() => { return vertical ? rootSize.height.value : rootSize.width.value; }, [vertical, rootSize]); const maxScale = (0, _reactNativeReanimated.useDerivedValue)(() => { if (typeof userMaxScale === 'object') { if (userMaxScale.length === 0) return 6; return (0, _getMaxScale.getMaxScale)({ width: rootChildSize.width.value, height: rootChildSize.height.value }, userMaxScale[activeIndex.value]); } return userMaxScale; }, [userMaxScale, activeIndex, rootChildSize]); const measureRoot = e => { const { width, height } = e.nativeEvent.layout; rootSize.width.value = width; rootSize.height.value = height; const direction = vertical ? height : width; scroll.value = activeIndex.value * direction; }; (0, _reactNativeReanimated.useAnimatedReaction)(() => ({ scroll: scroll.value, itemSize: itemSize.value }), value => onScroll === null || onScroll === void 0 ? void 0 : onScroll(value.scroll, (data.length - 1) * value.itemSize), [scroll, itemSize]); (0, _reactNativeReanimated.useAnimatedReaction)(() => activeIndex.value, value => onIndexChange && (0, _reactNativeReanimated.runOnJS)(onIndexChange)(value), [activeIndex]); (0, _reactNativeReanimated.useAnimatedReaction)(() => fetchIndex.value, value => (0, _reactNativeReanimated.runOnJS)(setScrollIndex)(value), [fetchIndex]); (0, _reactNativeReanimated.useAnimatedReaction)(() => ({ vertical, size: { width: rootSize.width.value, height: rootSize.height.value } }), value => { const direction = value.vertical ? value.size.height : value.size.width; scroll.value = activeIndex.value * direction; }, [vertical, rootSize]); (0, _reactNativeReanimated.useAnimatedReaction)(() => scale.value, (value, previousValue) => { if (value !== 1 && !hasZoomed.value) { hasZoomed.value = true; onZoomBegin && (0, _reactNativeReanimated.runOnJS)(onZoomBegin)(activeIndex.value); } else if (value === 1 && previousValue !== 1 && hasZoomed.value) { hasZoomed.value = false; onZoomEnd && (0, _reactNativeReanimated.runOnJS)(onZoomEnd)(activeIndex.value); } }, [scale]); // Reference handling const setIndex = index => { const clamped = (0, _clamp.clamp)(index, 0, data.length - 1); activeIndex.value = clamped; fetchIndex.value = clamped; scroll.value = clamped * itemSize.value; }; const requestState = () => ({ width: rootChildSize.width.value, height: rootChildSize.height.value, translateX: translate.x.value, translateY: translate.y.value, scale: scale.value }); const reset = (animate = true) => { translate.x.value = animate ? (0, _reactNativeReanimated.withTiming)(0) : 0; translate.y.value = animate ? (0, _reactNativeReanimated.withTiming)(0) : 0; scale.value = animate ? (0, _reactNativeReanimated.withTiming)(1) : 1; }; (0, _react.useImperativeHandle)(reference, () => ({ setIndex, reset, requestState })); return /*#__PURE__*/_react.default.createElement(_reactNativeGestureHandler.GestureHandlerRootView, { style: styles.root, onLayout: measureRoot }, data.map((item, index) => { const inLowerHalf = index < scrollIndex - nextItems; const inUpperHalf = index > scrollIndex + nextItems; if (inLowerHalf || inUpperHalf) return null; const key = (keyExtractor === null || keyExtractor === void 0 ? void 0 : keyExtractor(item, index)) ?? `item-${index}`; return /*#__PURE__*/_react.default.createElement(_GalleryItem.default, { key: key, zIndex: data.length - index, index: index, item: item, vertical: vertical, renderItem: renderItem, customTransition: customTransition }); }), /*#__PURE__*/_react.default.createElement(_Reflection.default, { maxScale: maxScale, itemSize: itemSize, length: data.length, vertical: vertical, tapOnEdgeToItem: tapOnEdgeToItem, allowPinchPanning: allowPinchPanning, pinchCenteringMode: pinchCenteringMode, onTap: onTap, onPanStart: onPanStart, onPanEnd: onPanEnd, onPinchStart: onPinchStart, onPinchEnd: onPinchEnd, onSwipe: onSwipe, onVerticalPull: onVerticalPull })); }; const styles = _reactNative.StyleSheet.create({ root: { flex: 1, justifyContent: 'center', alignItems: 'center', overflow: 'hidden' } }); var _default = exports.default = Gallery; //# sourceMappingURL=Gallery.js.map