diff --git a/docs/docs/api/safe-area-view.mdx b/docs/docs/api/safe-area-view.mdx index 3ae765d3..04f63ee2 100644 --- a/docs/docs/api/safe-area-view.mdx +++ b/docs/docs/api/safe-area-view.mdx @@ -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 diff --git a/src/SafeAreaView.web.tsx b/src/SafeAreaView.web.tsx index c9e184db..83227a47 100644 --- a/src/SafeAreaView.web.tsx +++ b/src/SafeAreaView.web.tsx @@ -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, @@ -18,9 +18,13 @@ const defaultEdges: Record = { 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); @@ -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 @@ -53,7 +77,10 @@ export const SafeAreaView = React.forwardRef< }, [edges]); const appliedStyle = React.useMemo(() => { - const flatStyle = StyleSheet.flatten(style) as Record; + const flatStyle = StyleSheet.flatten(style) as Record< + string, + number | string + >; if (mode === 'margin') { const { diff --git a/src/__tests__/SafeAreaView.web-test.tsx b/src/__tests__/SafeAreaView.web-test.tsx new file mode 100644 index 00000000..0c1f1674 --- /dev/null +++ b/src/__tests__/SafeAreaView.web-test.tsx @@ -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( + + + , + ); + 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( + + + , + ); + 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( + + + , + ); + expect(renderedStyle()).toMatchObject({ + paddingTop: 1, + paddingBottom: 4, + }); + }); + + it('keeps auto margins', () => { + render( + + + , + ); + expect(renderedStyle()).toMatchObject({ + marginTop: 1, + marginRight: 'auto', + marginBottom: 4, + marginLeft: 'auto', + }); + }); +});