From f7175884fbcf7e12d1b3a8c543a8de5674d122a1 Mon Sep 17 00:00:00 2001 From: Mantas Kantautas Date: Fri, 23 Oct 2026 18:27:26 +0300 Subject: [PATCH] fix: keep percentage and auto styles in SafeAreaView on web The web SafeAreaView added each inset to the matching padding or margin in JS. A percentage such as '5%' became '5%44', which the browser rejects, so both the style and the inset were dropped. A plain number string such as '0' became '044', and an auto margin was lost the same way. Percentages are now combined with the inset through calc() and max(), plain number strings use the numeric arithmetic, and an auto margin stays auto. --- docs/docs/api/safe-area-view.mdx | 2 + src/SafeAreaView.web.tsx | 35 ++++++++-- src/__tests__/SafeAreaView.web-test.tsx | 87 +++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/SafeAreaView.web-test.tsx 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', + }); + }); +});