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.

147 lines (146 loc) 4.2 kB
import { createContext, useCallback, useContext, useState } from "react"; import { Animated } from "react-native"; const useRouter = ({ onNavigate, onNavigateBack, initialRoute, routes, getRef, routeOpacity }) => { const [stack, setStack] = useState([]); const currentRoute = stack == null ? void 0 : stack[stack.length - 1]; const animate = useCallback( (snap = 0, opacity = 0, delay = 0) => { getRef == null ? void 0 : getRef().snapToRelativeOffset(snap); Animated.timing(routeOpacity, { toValue: opacity, duration: 150, useNativeDriver: true, delay }).start(); }, [getRef, routeOpacity] ); const navigate = 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]]); } 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 = createContext(void 0); function useSheetRouter(_id) { return useContext(RouterContext); } const RouterParamsContext = createContext(void 0); function useSheetRouteParams(_id, _routeKey) { const context = useContext(RouterParamsContext); return context; } export { RouterContext, RouterParamsContext, useRouter, useSheetRouteParams, useSheetRouter }; //# sourceMappingURL=use-router.js.map