import { useContext, useEffect } from 'react'; import { DEFAULT_INSETS, SafeAreaContext } from './'; import type { SafeAreaContextType, SafeAreaInsets } from '../type'; /** * Returns the full SafeAreaContext value (insets + the updater). Use this * when a component needs both read and write access to the provider. */ export const useSafeArea = () => useContext(SafeAreaContext); /** * Convenience hook returning only the current `insets` slice of the * context. Prefer this in read-only consumers to avoid re-rendering * when the setter identity changes. */ export const useGetSafeAreaInsets = (): SafeAreaInsets => { const { insets } = useContext(SafeAreaContext); return insets; }; /** * Returns the context's inset setter. Useful when a descendant has * measured its own safe area (for example from a layout event) and * wants to propagate the value up for siblings to consume. */ export const useHandleSafeAreaInsets = () => { const { handleInsets } = useContext(SafeAreaContext); return handleInsets; }; /** * Imperatively pushes a `{ top, bottom }` inset pair into the provider * for the lifetime of the calling component. On unmount the context is * reset to `DEFAULT_INSETS` so nothing leaks between screens. */ export const useSetSafeAreaInsets = (top?: number, bottom?: number) => { const { handleInsets } = useContext(SafeAreaContext); useEffect(() => { handleInsets?.({ top, bottom }); return () => { handleInsets?.(DEFAULT_INSETS); }; }, [handleInsets, top, bottom]); };