UNPKG

@attio/react-native-bottom-sheet-toolbox

Version:
117 lines (111 loc) 5.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useBottomSheetActions = useBottomSheetActions; var React = _interopRequireWildcard(require("react")); var _reactNativeReanimated = require("react-native-reanimated"); var _context = require("../context"); var _utils = require("../utils"); var _constants = require("../utils/constants"); var _useBottomSheetOnSnapDetection = require("./use-bottom-sheet-on-snap-detection"); var _useBottomSheetPositionLock = require("./use-bottom-sheet-position-lock"); var _useWorkletUiCallback = require("./use-worklet-ui-callback"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } // import { nanoid } from "nanoid/non-secure"; /** * Creates a set of actions that can be used to imperatively control the bottom sheet. */ function useBottomSheetActions(id) { const { currentPositionSharedValue, normalizedSnapPointsSharedValue } = (0, _context.useBottomSheetToolboxInternalContext)(); const { lock: snapLock, release: setSnapRelease, setPositionAnimate: setSnapPositionAnimate, setVelocity: setSnapVelocity, hasLockSharedValue: hasSnapLockSharedValue } = (0, _useBottomSheetPositionLock.useBottomSheetPositionLock)(`useBottomSheetActions:${id}:snap`, _constants.BottomSheetPositionBuiltInLockPriority.ACTIONS_SNAP); // We apply imperative snaps reactively, like this, so we can adjust for changes in the snap points etc. const snapDestinationIndexSharedValue = (0, _reactNativeReanimated.useSharedValue)(null); const lastSnapPointsSharedValue = (0, _reactNativeReanimated.useSharedValue)(null); (0, _reactNativeReanimated.useAnimatedReaction)(() => ({ currentPosition: currentPositionSharedValue.get(), snapDestinationIndex: snapDestinationIndexSharedValue.get(), snapPoints: normalizedSnapPointsSharedValue.get(), hasSnapLock: hasSnapLockSharedValue.get() }), ({ currentPosition, snapPoints, snapDestinationIndex, hasSnapLock }, lastResult) => { if (!hasSnapLock && lastResult?.hasSnapLock === true) { snapDestinationIndexSharedValue.set(null); return; } if (snapPoints === null || snapDestinationIndex === null) return; const lastSnapPoints = lastSnapPointsSharedValue.get(); const didSnapChange = lastSnapPoints === null || lastSnapPoints[snapDestinationIndex] !== snapPoints[snapDestinationIndex] || lastResult?.snapDestinationIndex !== snapDestinationIndex; const snapPoint = snapPoints[snapDestinationIndex] ?? null; if (snapPoint === null || currentPosition === snapPoint) { snapDestinationIndexSharedValue.set(null); setSnapRelease(); return; } if (!didSnapChange) return; lastSnapPointsSharedValue.set(snapPoints); setSnapVelocity(0); setSnapPositionAnimate(snapPoint); }, [hasSnapLockSharedValue, currentPositionSharedValue, normalizedSnapPointsSharedValue]); const snapToIndex = (0, _useWorkletUiCallback.useWorkletUiCallback)(snapPointIndex => { "worklet"; const normalizedSnapPoints = normalizedSnapPointsSharedValue.get(); if (!normalizedSnapPoints || normalizedSnapPoints.length === 0) { throw new Error("No snap-points to snap to."); } if (!(snapPointIndex in normalizedSnapPoints)) { throw new Error(`Invalid snap point index: ${snapPointIndex}`); } snapLock(); snapDestinationIndexSharedValue.set(snapPointIndex); }, [snapLock, normalizedSnapPointsSharedValue]); const { lock: closeLock, release: closeRelease, setPositionAnimate: setClosePositionAnimate, setVelocity: setCloseVelocity, hasLockSharedValue: hasCloseLockSharedValue } = (0, _useBottomSheetPositionLock.useBottomSheetPositionLock)(`useBottomSheetActions:${id}:close`, _constants.BottomSheetPositionBuiltInLockPriority.ACTIONS_CLOSE); const expand = (0, _useWorkletUiCallback.useWorkletUiCallback)(() => { "worklet"; const normalizedSnapPoints = normalizedSnapPointsSharedValue.get(); if (!normalizedSnapPoints || normalizedSnapPoints.length === 0) { throw new Error("No snap-points to snap to."); } else if (normalizedSnapPoints.length === 1 && normalizedSnapPoints[0] === 0) { throw new Error("Sheet cannot expand as only available snap-point is '0'."); } snapToIndex(normalizedSnapPoints.length - 1); }, [normalizedSnapPointsSharedValue, snapToIndex]); const close = (0, _useWorkletUiCallback.useWorkletUiCallback)(() => { "worklet"; if (hasCloseLockSharedValue.get()) return; closeLock(); setCloseVelocity(0); setClosePositionAnimate(_constants.CLOSED_SNAP_POINT, _utils.DEFAULT_CLOSE_SHEET_SNAP_POINT_SPRING_CONFIG, closeRelease); }, [hasCloseLockSharedValue, closeLock, setCloseVelocity, setClosePositionAnimate, closeRelease]); const currentSnapRef = React.useRef(null); (0, _useBottomSheetOnSnapDetection.useBottomSheetOnSnapDetection)(React.useCallback(snap => { currentSnapRef.current = snap; }, [])); const getCurrentSnapIndex = React.useCallback(() => currentSnapRef.current, []); return React.useMemo(() => ({ snapToIndex, expand, close, getCurrentSnapIndex }), [snapToIndex, expand, close, getCurrentSnapIndex]); } //# sourceMappingURL=use-bottom-sheet-actions.js.map