UNPKG

@fullstackhouse/react-native-nested-safe-area

Version:

A wrapper on top of react-native-safe-area-context that allows nested safe area contexts

71 lines (66 loc) 2.58 kB
"use strict"; import React, { useContext, useMemo, useCallback } from 'react'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; /** * Represents a safe area edge that can be consumed */ import { NestedSafeAreaContext } from "./NestedSafeAreaContext.js"; import { jsx as _jsx } from "react/jsx-runtime"; export const NestedSafeAreaProvider = ({ children, consumedInsets, consumedEdges, resetEdges }) => { const originalInsets = useSafeAreaInsets(); const parentContext = useContext(NestedSafeAreaContext); const parentInsets = parentContext ? parentContext.insets : originalInsets; const finalConsumedInsets = useMemo(() => { const insets = { top: consumedInsets?.top || 0, right: consumedInsets?.right || 0, bottom: consumedInsets?.bottom || 0, left: consumedInsets?.left || 0 }; if (consumedEdges && consumedEdges.length > 0) { consumedEdges.forEach(edge => { insets[edge] = parentInsets[edge]; }); } return insets; // eslint-disable-next-line react-hooks/exhaustive-deps }, [consumedEdges?.join(), JSON.stringify(consumedInsets), parentInsets]); const currentInsets = useMemo(() => { const insets = { top: Math.max(0, parentInsets.top - (finalConsumedInsets.top || 0)), right: Math.max(0, parentInsets.right - (finalConsumedInsets.right || 0)), bottom: Math.max(0, parentInsets.bottom - (finalConsumedInsets.bottom || 0)), left: Math.max(0, parentInsets.left - (finalConsumedInsets.left || 0)) }; // Apply resetEdges - reset specified edges to original safe area values if (resetEdges && resetEdges.length > 0) { resetEdges.forEach(edge => { insets[edge] = originalInsets[edge]; }); } return insets; // eslint-disable-next-line react-hooks/exhaustive-deps }, [parentInsets, finalConsumedInsets, resetEdges?.join(), originalInsets]); const consumeInsets = useCallback(consumed => { return { top: Math.max(0, currentInsets.top - (consumed.top || 0)), right: Math.max(0, currentInsets.right - (consumed.right || 0)), bottom: Math.max(0, currentInsets.bottom - (consumed.bottom || 0)), left: Math.max(0, currentInsets.left - (consumed.left || 0)) }; }, [currentInsets]); const contextValue = useMemo(() => ({ insets: currentInsets, consumeInsets }), [currentInsets, consumeInsets]); return /*#__PURE__*/_jsx(NestedSafeAreaContext.Provider, { value: contextValue, children: children }); }; //# sourceMappingURL=NestedSafeAreaProvider.js.map