A wrapper on top of react-native-safe-area-context that allows nested safe area contexts
npm install @fullstackhouse/react-native-nested-safe-areaA wrapper on top of react-native-safe-area-context that allows nested safe area contexts with automatic inset consumption tracking.
- Nested Context Support: Stack multiple safe area providers without overlapping insets
- Automatic Consumption: Automatically tracks and subtracts consumed insets in nested contexts
- React Integration: Built-in React Context and hooks
- Cross-Platform: Works on iOS, Android, and Web
- TypeScript Support: Fully typed with TypeScript
- Edge Control: Selectively apply safe area insets to specific edges
``sh`
npm install react-native-nested-safe-areaor
yarn add react-native-nested-safe-area
This library depends on react-native-safe-area-context. If you don't have it installed:
`sh`
npm install react-native-safe-area-contextor
yarn add react-native-safe-area-context
Follow the react-native-safe-area-context installation guide for platform-specific setup.
`jsx
import React from 'react';
import { View, Text } from 'react-native';
import { NestedSafeAreaView } from 'react-native-nested-safe-area';
export default function App() {
return (
style={{ flex: 1, backgroundColor: 'lightblue' }}
>
style={{ backgroundColor: 'blue', marginTop: 'auto' }}
>
);
}
`
`jsx
import React from 'react';
import { View, Text } from 'react-native';
import {
NestedSafeAreaProvider,
useNestedSafeAreaInsets,
} from 'react-native-nested-safe-area';
function MyComponent() {
const insets = useNestedSafeAreaInsets();
return (
);
}
export default function App() {
return (
);
}
`
The consumedEdges prop provides a convenient way to completely consume specific edges, setting them to zero in nested contexts:
`jsx
import React from 'react';
import { View, Text } from 'react-native';
import { NestedSafeAreaProvider } from 'react-native-nested-safe-area';
export default function App() {
return (
{/ Header consumes top edge completely /}
{/ Main content - top edge is now zero for nested components /}
{/ Footer consumes bottom edge /}
);
}
`
The resetEdges prop allows you to reset specific edges back to the original safe area values, useful when you need to restore safe areas in deeply nested contexts:
`jsx
import React from 'react';
import { View, Text } from 'react-native';
import { NestedSafeAreaProvider } from 'react-native-nested-safe-area';
export default function App() {
return (
{/ First level consumes top and bottom edges /}
{/ Modal/overlay that needs original top safe area restored /}
);
}
`
`jsx
import React from 'react';
import { View, Text } from 'react-native';
import { NestedSafeAreaView } from 'react-native-nested-safe-area';
export default function App() {
return (
{/ Header with only top safe area /}
{/ Content area with left/right safe areas /}
{/ Footer with only bottom safe area /}
style={{ backgroundColor: 'green' }}
>
);
}
`
A View component that applies safe area insets as padding and automatically provides consumed insets to nested contexts.
`typescript`
Props:
- edges?: Array<'top' | 'right' | 'bottom' | 'left'> - Which edges to apply safe area to (default: all edges)View
- Inherits all React Native props
Provides nested safe area context with automatic inset consumption tracking.
`typescript`
consumedEdges?: Edge[]
resetEdges?: Edge[]
>
{children}
Props:
- consumedInsets?: Partial - Insets to subtract from parent contextconsumedEdges?: Edge[]
- - List of edges to completely consume (set to zero). Takes precedence over consumedInsets when providedresetEdges?: Edge[]
- - List of edges to reset to the original safe area values. Takes precedence over both consumedInsets and consumedEdgeschildren: ReactNode
- - Child components
#### Edge Type
`typescript`
type Edge = 'top' | 'right' | 'bottom' | 'left';
Hook to access current safe area insets in nested context.
`typescript`
const insets = useNestedSafeAreaInsets();
Returns:
- insets: EdgeInsets - Current safe area insets ({ top, right, bottom, left })
Hook to access the full nested safe area context.
`typescript`
const { insets, consumeInsets } = useNestedSafeAreaContext();
Returns:
- insets: EdgeInsets - Current safe area insetsconsumeInsets: (consumed: Partial
- - Function to calculate consumed insets
1. Base Context: The root NestedSafeAreaProvider gets insets from react-native-safe-area-contextNestedSafeAreaView
2. Consumption Tracking: Each nested provider subtracts consumed insets from its parent context
3. Automatic Management: automatically applies insets as padding and marks them as consumededges` prop to selectively apply insets to specific sides
4. Edge Control: Use the
- ✅ iOS: Full support with react-native-safe-area-context
- ✅ Android: Full support with react-native-safe-area-context
- ✅ Web: Full support with react-native-safe-area-context
MIT