UNPKG

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

Version:
96 lines (94 loc) 3.17 kB
"use strict"; import * as React from "react"; import { BottomSheetToolboxBackdropProvider } from "@attio/react-native-bottom-sheet-toolbox"; import { jsx as _jsx } from "react/jsx-runtime"; const BottomSheetToolboxStackContext = /*#__PURE__*/React.createContext(undefined); export function useIsBottomSheetToolboxStackContext() { const context = React.useContext(BottomSheetToolboxStackContext); return context !== undefined; } export function useBottomSheetToolboxStackContext() { const context = React.useContext(BottomSheetToolboxStackContext); if (!context) { throw new Error("BottomSheetToolboxStackProvider not found."); } return context; } export function BottomSheetToolboxStackProvider({ children, initialSheetName, onClose, ref }) { const [sheetStack, setSheetStack] = React.useState(() => initialSheetName !== null ? [initialSheetName] : []); const activeSheet = sheetStack.at(-1) ?? null; const sheetRefsRef = React.useRef({}); const nextNotifyClosedBehaviorRef = React.useRef("POP"); const push = React.useCallback(sheetName => { setSheetStack(s => [...s, sheetName]); }, []); const pop = React.useCallback(() => { if (!activeSheet) return; const sheetRefs = sheetRefsRef.current; const topSheet = sheetRefs[activeSheet]; topSheet?.close(); }, [activeSheet]); const canPopToTop = sheetStack.length > 1; const popToTop = React.useCallback(() => { if (!canPopToTop) return; nextNotifyClosedBehaviorRef.current = "POP_TO_TOP"; pop(); }, [canPopToTop, pop]); const closeAll = React.useCallback(() => { nextNotifyClosedBehaviorRef.current = "CLOSE_ALL"; pop(); }, [pop]); const notifyClosed = React.useCallback(name => { switch (nextNotifyClosedBehaviorRef.current) { case "POP": setSheetStack(s => { const currentActiveSheet = s.at(-1) ?? null; return name === currentActiveSheet ? s.slice(0, -1) : s; }); break; case "POP_TO_TOP": setSheetStack(s => s.slice(0, 1)); break; case "CLOSE_ALL": setSheetStack([]); break; } nextNotifyClosedBehaviorRef.current = "POP"; }, []); // When the last sheet is removed, consider the stack to be closed. const hadContentRef = React.useRef(sheetStack.length > 0); React.useEffect(() => { const hasContent = sheetStack.length > 0; const hadContent = hadContentRef.current; if (hadContent && !hasContent) { onClose?.(); } hadContentRef.current = hasContent; }, [onClose, sheetStack]); const contextValue = React.useMemo(() => ({ sheetStack, push, popToTop, pop, notifyClosed, activeSheet, closeAll, sheetRefsRef }), [sheetStack, push, notifyClosed, activeSheet, popToTop, pop, closeAll]); React.useImperativeHandle(ref, () => ({ close: closeAll }), [closeAll]); return /*#__PURE__*/_jsx(BottomSheetToolboxBackdropProvider, { reset: true, children: /*#__PURE__*/_jsx(BottomSheetToolboxStackContext.Provider, { value: contextValue, children: children }) }); } //# sourceMappingURL=bottom-sheet-toolbox-stack-context.js.map