Skip to content

Commit df3cdfd

Browse files
authored
fix: make elevation shadows on iOS & Web match android more closely (#5062)
### Motivation On iOS and Web, we manually calculate elevation shadows. They are originally from material-web: #3089 But looking at them side-by-side on all platforms, they look nothing like Android. This reworks the shadows so they more closely match Android's elevation. Note that it's an approximation. On Android, the elevation is dynamic and changes based on where the view is. Views toward the bottom have a larger shadow than views toward the top. But we can't achieve it with shadow styles directly, so we need to approximate it. Also changed the example, as the huge rectangles fit fewer ones on screen and made it harder to see the shadows. ### Known issues - Animated elevation doesn't work on Web. But it's a pre-existing bug. It should be fixed once we move to Reanimated. ### Test plan **Before** <img width="2668" height="1604" alt="elevation-before" src="https://github.com/user-attachments/assets/d127b3e7-d74e-474e-a393-d9385a69e665" /> **After** <img width="2668" height="1626" alt="elevation-after" src="https://github.com/user-attachments/assets/601fd2cd-94df-4df5-bddd-6be82bdad0e1" />
1 parent ea80c26 commit df3cdfd

10 files changed

Lines changed: 416 additions & 237 deletions

File tree

‎example/src/Examples/SurfaceExample.tsx‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,87 @@
1-
import { ScrollView, StyleSheet, View } from 'react-native';
1+
import * as React from 'react';
2+
import { Animated, ScrollView, StyleSheet, View } from 'react-native';
23

3-
import { Surface, Text, Palette, List } from 'react-native-paper';
4+
import { Surface, Text, Palette, List, IconButton } from 'react-native-paper';
45
import type { Elevation } from 'react-native-paper';
56

67
import ScreenWrapper from '../ScreenWrapper';
78

9+
const elevationLevels: Elevation[] = [0, 1, 2, 3, 4, 5];
10+
11+
const AnimatedSurface = () => {
12+
const [index, setIndex] = React.useState(3);
13+
14+
const level = elevationLevels[index];
15+
const elevation = React.useRef(new Animated.Value(level)).current;
16+
17+
React.useEffect(() => {
18+
Animated.timing(elevation, {
19+
toValue: level,
20+
duration: 250,
21+
useNativeDriver: false,
22+
}).start();
23+
}, [elevation, level]);
24+
25+
return (
26+
<View style={styles.scroll}>
27+
<Surface style={styles.surface} elevation={elevation}>
28+
<Text variant="bodySmall">{`Elevation ${level}`}</Text>
29+
</Surface>
30+
<View style={styles.actions}>
31+
<IconButton
32+
mode="contained-tonal"
33+
icon="minus"
34+
disabled={index === 0}
35+
onPress={() => setIndex(index - 1)}
36+
/>
37+
<IconButton
38+
mode="contained-tonal"
39+
icon="plus"
40+
disabled={index === elevationLevels.length - 1}
41+
onPress={() => setIndex(index + 1)}
42+
/>
43+
</View>
44+
</View>
45+
);
46+
};
47+
848
const SurfaceExample = () => {
949
const elevationValues: Elevation[] = [0, 1, 2, 3, 4, 5];
1050

1151
const renderSurface = (index: Elevation, mode: 'flat' | 'elevated') => (
12-
<Surface
13-
key={index}
14-
style={[styles.surface, styles.v3Surface]}
15-
mode={mode}
16-
elevation={index}
17-
>
18-
<Text variant="bodyLarge">
19-
{`Elevation ${index === 1 ? '(default)' : ''} ${index}`}
20-
</Text>
52+
<Surface key={index} style={styles.surface} mode={mode} elevation={index}>
53+
<Text variant="bodySmall">{`Elevation ${index}`}</Text>
2154
</Surface>
2255
);
2356

2457
return (
2558
<ScreenWrapper>
2659
<List.Section title="Elevated surface">
27-
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
60+
<ScrollView
61+
horizontal
62+
showsHorizontalScrollIndicator={false}
63+
contentContainerStyle={styles.scroll}
64+
>
2865
{elevationValues.map((elevation) =>
2966
renderSurface(elevation, 'elevated')
3067
)}
3168
</ScrollView>
3269
</List.Section>
3370

3471
<List.Section title="Flat surface">
35-
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
72+
<ScrollView
73+
horizontal
74+
showsHorizontalScrollIndicator={false}
75+
contentContainerStyle={styles.scroll}
76+
>
3677
{elevationValues.map((elevation) => renderSurface(elevation, 'flat'))}
3778
</ScrollView>
3879
</List.Section>
3980

81+
<List.Section title="Animated elevation">
82+
<AnimatedSurface />
83+
</List.Section>
84+
4085
<List.Section title="Layout">
4186
<View style={styles.content}>
4287
<View style={styles.horizontalSurfacesContainer}>
@@ -68,21 +113,22 @@ const styles = StyleSheet.create({
68113
padding: 24,
69114
alignItems: 'center',
70115
},
116+
scroll: {
117+
gap: 24,
118+
paddingHorizontal: 16,
119+
paddingVertical: 24,
120+
},
71121
surface: {
72-
margin: 24,
73-
height: 80,
74-
width: 80,
122+
height: 120,
123+
width: 120,
124+
borderRadius: 8,
75125
alignItems: 'center',
76126
justifyContent: 'center',
77127
},
78-
v3Surface: {
79-
borderRadius: 16,
80-
height: 200,
81-
width: 200,
82-
alignItems: 'center',
83-
justifyContent: 'center',
128+
actions: {
129+
flexDirection: 'row',
130+
gap: 16,
84131
},
85-
86132
horizontalSurfacesContainer: {
87133
flexDirection: 'row',
88134
justifyContent: 'space-between',

‎src/components/FAB/useVisibility.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import {
1111

1212
import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext';
1313
import {
14-
IOS_SHADOW_RADIUS_FACTOR,
15-
SHADOW_OPACITY,
1614
androidElevationLevels,
1715
shadow,
1816
shadowLayers,
@@ -79,6 +77,7 @@ export function useVisibility({
7977
const restingElevationDp = androidElevationLevels[elevation];
8078
const shadowOffsetHeight = shadowLayers[0].height[elevation];
8179
const shadowRadius = shadowLayers[0].shadowRadius[elevation];
80+
const shadowOpacity = elevation ? shadowLayers[0].shadowOpacity : 0;
8281
const shadowColor = theme.colors.shadow;
8382

8483
const webShadow =
@@ -93,9 +92,9 @@ export function useVisibility({
9392
}
9493
return {
9594
shadowColor,
96-
shadowOpacity: alpha.value * (elevation ? SHADOW_OPACITY : 0),
95+
shadowOpacity: alpha.value * shadowOpacity,
9796
shadowOffset: { width: 0, height: shadowOffsetHeight },
98-
shadowRadius: shadowRadius * IOS_SHADOW_RADIUS_FACTOR,
97+
shadowRadius,
9998
};
10099
});
101100

0 commit comments

Comments
 (0)