Skip to content

Commit 8a8d924

Browse files
fix: iOS Surface styles
1 parent 7c7241a commit 8a8d924

21 files changed

+844
-319
lines changed

example/src/Examples/SurfaceExample.tsx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
2-
import { StyleSheet } from 'react-native';
2+
import { StyleSheet, View } from 'react-native';
33

4-
import { MD3Elevation, Surface, Text } from 'react-native-paper';
4+
import { MD3Elevation, Surface, Text, MD3Colors } from 'react-native-paper';
55

66
import { useExampleTheme } from '..';
77
import { isWeb } from '../../utils';
@@ -31,6 +31,23 @@ const SurfaceExample = () => {
3131
</Text>
3232
</Surface>
3333
))}
34+
35+
<View style={styles.horizontalSurfacesContainer}>
36+
<Surface style={styles.horizontalSurface}>
37+
<Text style={styles.centerText}>Left</Text>
38+
</Surface>
39+
<Surface style={styles.horizontalSurface}>
40+
<Text style={styles.centerText}>Right</Text>
41+
</Surface>
42+
</View>
43+
<View style={styles.verticalSurfacesContainer}>
44+
<Surface style={styles.verticalSurface}>
45+
<Text style={styles.centerText}>Top</Text>
46+
</Surface>
47+
<Surface style={styles.verticalSurface}>
48+
<Text style={styles.centerText}>Bottom</Text>
49+
</Surface>
50+
</View>
3451
</ScreenWrapper>
3552
);
3653
};
@@ -61,6 +78,37 @@ const styles = StyleSheet.create({
6178
alignItems: 'center',
6279
justifyContent: 'center',
6380
},
81+
82+
horizontalSurfacesContainer: {
83+
flexDirection: 'row',
84+
justifyContent: 'space-between',
85+
width: '100%',
86+
marginBottom: 20,
87+
borderColor: MD3Colors.tertiary50,
88+
padding: 10,
89+
borderWidth: 1,
90+
},
91+
horizontalSurface: {
92+
width: '48%',
93+
},
94+
95+
verticalSurfacesContainer: {
96+
height: 400,
97+
justifyContent: 'space-between',
98+
width: '100%',
99+
marginBottom: 100,
100+
borderColor: MD3Colors.tertiary50,
101+
padding: 10,
102+
borderWidth: 1,
103+
},
104+
verticalSurface: {
105+
height: '48%',
106+
justifyContent: 'center',
107+
},
108+
109+
centerText: {
110+
textAlign: 'center',
111+
},
64112
});
65113

66114
export default SurfaceExample;

src/components/Surface.tsx

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import overlay, { isAnimatedValue } from '../styles/overlay';
1313
import shadow from '../styles/shadow';
1414
import type { ThemeProp, MD3Elevation } from '../types';
1515
import { forwardRef } from '../utils/forwardRef';
16+
import { splitStyles } from '../utils/splitStyles';
1617

1718
export type Props = React.ComponentPropsWithRef<typeof View> & {
1819
/**
@@ -229,10 +230,34 @@ const Surface = forwardRef<View, Props>(
229230
start,
230231
end,
231232
flex,
233+
width,
234+
height,
232235
...restStyle
233236
} = (StyleSheet.flatten(style) || {}) as ViewStyle;
234237

235-
const absoluteStyles = {
238+
const [filteredStyle, borderRadiusStyle, marginStyle, paddingStyle] =
239+
splitStyles(
240+
restStyle,
241+
(style) => style.startsWith('border') && style.endsWith('Radius'),
242+
(style) => style.startsWith('margin'),
243+
(style) => style.startsWith('padding')
244+
);
245+
246+
const sharedStyles = {
247+
flex: height ? 1 : undefined,
248+
...borderRadiusStyle,
249+
};
250+
251+
const middleLayerViewStyles = [
252+
sharedStyles,
253+
{
254+
backgroundColor,
255+
},
256+
];
257+
258+
const innerLayerViewStyles = [filteredStyle, sharedStyles, paddingStyle];
259+
260+
const outerLayerViewStyles = {
236261
position,
237262
alignSelf,
238263
top,
@@ -241,14 +266,13 @@ const Surface = forwardRef<View, Props>(
241266
left,
242267
start,
243268
end,
269+
flex,
270+
width,
271+
height,
272+
...borderRadiusStyle,
273+
...marginStyle,
244274
};
245275

246-
const sharedStyle = [{ backgroundColor, flex }, restStyle];
247-
248-
const innerLayerViewStyles = [{ flex }];
249-
250-
const outerLayerViewStyles = [absoluteStyles, innerLayerViewStyles];
251-
252276
if (isAnimatedValue(elevation)) {
253277
const inputRange = [0, 1, 2, 3, 4, 5];
254278

@@ -280,10 +304,14 @@ const Surface = forwardRef<View, Props>(
280304
testID={`${testID}-outer-layer`}
281305
>
282306
<Animated.View
283-
style={[getStyleForAnimatedShadowLayer(1), innerLayerViewStyles]}
284-
testID={`${testID}-inner-layer`}
307+
style={[getStyleForAnimatedShadowLayer(1), middleLayerViewStyles]}
308+
testID={`${testID}-middle-layer`}
285309
>
286-
<Animated.View {...props} testID={testID} style={sharedStyle}>
310+
<Animated.View
311+
{...props}
312+
testID={testID}
313+
style={innerLayerViewStyles}
314+
>
287315
{children}
288316
</Animated.View>
289317
</Animated.View>
@@ -312,10 +340,14 @@ const Surface = forwardRef<View, Props>(
312340
testID={`${testID}-outer-layer`}
313341
>
314342
<Animated.View
315-
style={[getStyleForShadowLayer(1), innerLayerViewStyles]}
316-
testID={`${testID}-inner-layer`}
343+
style={[getStyleForShadowLayer(1), middleLayerViewStyles]}
344+
testID={`${testID}-middle-layer`}
317345
>
318-
<Animated.View {...props} testID={testID} style={sharedStyle}>
346+
<Animated.View
347+
{...props}
348+
testID={testID}
349+
style={innerLayerViewStyles}
350+
>
319351
{children}
320352
</Animated.View>
321353
</Animated.View>

0 commit comments

Comments
 (0)