import * as React from 'react'; import { StyleSheet } from 'react-native'; import { EdgeInsets, InsetChangedEvent } from './SafeArea.types'; import NativeSafeAreaView from './NativeSafeAreaView'; const SafeAreaContext = React.createContext(null); export interface SafeAreaViewProps { children?: React.ReactNode; } export function SafeAreaProvider({ children }: SafeAreaViewProps) { const [insets, setInsets] = React.useState(null); const onInsetsChange = React.useCallback((event: InsetChangedEvent) => { setInsets(event.nativeEvent.insets); }, []); return ( {insets !== null ? ( {children} ) : null} ); } const styles = StyleSheet.create({ fill: { flex: 1 }, }); export const SafeAreaConsumer = SafeAreaContext.Consumer; export function useSafeArea(): EdgeInsets { const safeArea = React.useContext(SafeAreaContext); if (safeArea == null) { throw new Error( 'No safe area value available. Make sure you are rendering `` at the top of your app.', ); } return safeArea; } export { EdgeInsets };