Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/api/safe-area-view.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ sidebar_label: SafeAreaView

Padding or margin styles are added to the insets, for example `style={{paddingTop: 10}}` on a `SafeAreaView` that has insets of 20 will result in a top padding of 30.

On web, a percentage is combined with the inset through CSS `calc()` or `max()`. With `mode="margin"`, an `auto` margin stays `auto` and replaces the inset on that edge.

### Example

```tsx
Expand Down
35 changes: 31 additions & 4 deletions src/SafeAreaView.web.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import { StyleSheet, View, type DimensionValue } from 'react-native';
import {
Edge,
EdgeMode,
Expand All @@ -18,9 +18,13 @@ const defaultEdges: Record<Edge, EdgeMode> = {

function getEdgeValue(
inset: number,
current: number,
current: number | string,
mode: EdgeMode | undefined,
) {
): DimensionValue {
if (typeof current === 'string') {
// `calc()` and `max()` are valid styles on web but not DimensionValues.
return getStringEdgeValue(inset, current, mode) as DimensionValue;
}
switch (mode) {
case 'maximum':
return Math.max(current, inset);
Expand All @@ -32,6 +36,26 @@ function getEdgeValue(
}
}

// A string cannot be added to the inset in JS. A plain number such as '0' is
// converted, and CSS combines other values such as percentages with the inset.
// CSS cannot combine `auto` with a length, so an `auto` margin is kept.
function getStringEdgeValue(
inset: number,
current: string,
mode: EdgeMode | undefined,
) {
if (current === 'auto' || (mode !== 'additive' && mode !== 'maximum')) {
return current;
}
const value = Number(current);
if (Number.isFinite(value)) {
return getEdgeValue(inset, value, mode);
}
return mode === 'additive'
? `calc(${current} + ${inset}px)`
: `max(${current}, ${inset}px)`;
}

export const SafeAreaView = React.forwardRef<
NativeSafeAreaViewInstance,
NativeSafeAreaViewProps
Expand All @@ -53,7 +77,10 @@ export const SafeAreaView = React.forwardRef<
}, [edges]);

const appliedStyle = React.useMemo(() => {
const flatStyle = StyleSheet.flatten(style) as Record<string, number>;
const flatStyle = StyleSheet.flatten(style) as Record<
string,
number | string
>;

if (mode === 'margin') {
const {
Expand Down
87 changes: 87 additions & 0 deletions src/__tests__/SafeAreaView.web-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, expect, it } from '@jest/globals';
import { render, screen } from '@testing-library/react-native';
import * as React from 'react';
import { StyleSheet, type ViewStyle } from 'react-native';
import type { Metrics } from '../SafeArea.types';
import { SafeAreaProvider } from '../SafeAreaContext';
import { SafeAreaView } from '../SafeAreaView.web';

const INITIAL_METRICS: Metrics = {
insets: { top: 1, left: 2, right: 3, bottom: 4 },
frame: { x: 0, y: 0, height: 100, width: 100 },
};

function renderedStyle() {
return StyleSheet.flatten(screen.getByTestId('safe-area-view').props.style);
}

describe('SafeAreaView.web', () => {
it('adds the insets to percentage padding', () => {
render(
<SafeAreaProvider initialMetrics={INITIAL_METRICS}>
<SafeAreaView testID="safe-area-view" style={{ padding: '5%' }} />
</SafeAreaProvider>,
);
expect(renderedStyle()).toMatchObject({
paddingTop: 'calc(5% + 1px)',
paddingRight: 'calc(5% + 3px)',
paddingBottom: 'calc(5% + 4px)',
paddingLeft: 'calc(5% + 2px)',
});
});

it('uses the larger of percentage padding and the inset in maximum mode', () => {
render(
<SafeAreaProvider initialMetrics={INITIAL_METRICS}>
<SafeAreaView
testID="safe-area-view"
edges={{ top: 'maximum' }}
style={{ paddingTop: '10%', paddingBottom: '10%' }}
/>
</SafeAreaProvider>,
);
expect(renderedStyle()).toMatchObject({
paddingTop: 'max(10%, 1px)',
paddingBottom: '10%',
});
});

it('applies the insets to plain number strings', () => {
// react-native-web accepts plain number strings such as '0'.
const style = {
paddingTop: '0',
paddingBottom: '0',
} as unknown as ViewStyle;
render(
<SafeAreaProvider initialMetrics={INITIAL_METRICS}>
<SafeAreaView
testID="safe-area-view"
edges={{ top: 'maximum', bottom: 'additive' }}
style={style}
/>
</SafeAreaProvider>,
);
expect(renderedStyle()).toMatchObject({
paddingTop: 1,
paddingBottom: 4,
});
});

it('keeps auto margins', () => {
render(
<SafeAreaProvider initialMetrics={INITIAL_METRICS}>
<SafeAreaView
testID="safe-area-view"
mode="margin"
style={{ marginHorizontal: 'auto' }}
/>
</SafeAreaProvider>,
);
expect(renderedStyle()).toMatchObject({
marginTop: 1,
marginRight: 'auto',
marginBottom: 4,
marginLeft: 'auto',
});
});
});