UNPKG

@crossed/sheet

Version:

A Cross Platform(Android & iOS) ActionSheet with a robust and flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.

174 lines (173 loc) 5.53 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var use_router_exports = {}; __export(use_router_exports, { RouterContext: () => RouterContext, RouterParamsContext: () => RouterParamsContext, useRouter: () => useRouter, useSheetRouteParams: () => useSheetRouteParams, useSheetRouter: () => useSheetRouter }); module.exports = __toCommonJS(use_router_exports); var import_react = require("react"); var import_react_native = require("react-native"); const useRouter = ({ onNavigate, onNavigateBack, initialRoute, routes, getRef, routeOpacity }) => { const [stack, setStack] = (0, import_react.useState)([]); const currentRoute = stack == null ? void 0 : stack[stack.length - 1]; const animate = (0, import_react.useCallback)( (snap = 0, opacity = 0, delay = 0) => { getRef == null ? void 0 : getRef().snapToRelativeOffset(snap); import_react_native.Animated.timing(routeOpacity, { toValue: opacity, duration: 150, useNativeDriver: true, delay }).start(); }, [getRef, routeOpacity] ); const navigate = (0, import_react.useCallback)( (name, params, snap) => { animate(snap || 20, 0); setTimeout(() => { setStack((state) => { const next = routes == null ? void 0 : routes.find((route) => route.name === name); if (!next) { animate(0, 1); return state; } const currentIndex = state.findIndex( (route) => route.name === next.name ); if (currentIndex > -1) { const nextStack = [...state]; nextStack.splice(currentIndex, 1); return [...nextStack, { ...next, params: params || next.params }]; } onNavigate == null ? void 0 : onNavigate(next.name); animate(0, 1, 150); return [...state, { ...next, params: params || next.params }]; }); }, 100); }, [animate, routes, onNavigate] ); const initialNavigation = () => { if (!routes) return; if (initialRoute) { const route = routes == null ? void 0 : routes.find((rt) => rt.name === initialRoute); if (route) { setStack([route]); } } else { setStack([routes[0]]); } import_react_native.Animated.timing(routeOpacity, { toValue: 1, duration: 150, useNativeDriver: true }).start(); }; const goBack = (name, snap) => { getRef == null ? void 0 : getRef().snapToRelativeOffset(snap || -10); animate(snap || -10, 0); setTimeout(() => { setStack((state) => { var _a, _b; const next = routes == null ? void 0 : routes.find((route) => route.name === name); if (state.length === 1) { close(); animate(0, 1); return state; } if (!next) { const nextStack = [...state]; nextStack.pop(); if (currentRoute) { onNavigateBack == null ? void 0 : onNavigateBack((_a = nextStack[nextStack.length - 1]) == null ? void 0 : _a.name); animate(0, 1, 150); } return nextStack; } const currentIndex = stack.findIndex( (route) => route.name === next.name ); if (currentIndex > -1) { const nextStack = [...state]; nextStack.splice(currentIndex); onNavigateBack == null ? void 0 : onNavigateBack((_b = nextStack[nextStack.length - 1]) == null ? void 0 : _b.name); animate(0, 1, 150); return [...nextStack, next]; } animate(0, 1, 150); onNavigateBack == null ? void 0 : onNavigateBack(next.name); return [...stack, next]; }); }, 100); }; const close = () => { var _a; (_a = getRef == null ? void 0 : getRef()) == null ? void 0 : _a.hide(); }; const popToTop = () => { if (!stack[0]) { return; } goBack(stack[0].name); }; const canGoBack = () => { return stack && stack.length > 1; }; return { currentRoute, navigate, goBack, close, popToTop, hasRoutes: () => routes && routes.length > 0, stack, initialNavigation, canGoBack }; }; const RouterContext = (0, import_react.createContext)(void 0); function useSheetRouter(_id) { return (0, import_react.useContext)(RouterContext); } const RouterParamsContext = (0, import_react.createContext)(void 0); function useSheetRouteParams(_id, _routeKey) { const context = (0, import_react.useContext)(RouterParamsContext); return context; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RouterContext, RouterParamsContext, useRouter, useSheetRouteParams, useSheetRouter }); //# sourceMappingURL=use-router.js.map