-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathSurface.tsx
376 lines (343 loc) · 9.62 KB
/
Surface.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import * as React from 'react';
import {
Animated,
Platform,
ShadowStyleIOS,
StyleProp,
StyleSheet,
View,
ViewStyle,
} from 'react-native';
import { useInternalTheme } from '../core/theming';
import overlay, { isAnimatedValue } from '../styles/overlay';
import shadow from '../styles/shadow';
import type { ThemeProp, MD3Elevation } from '../types';
import { forwardRef } from '../utils/forwardRef';
import { splitStyles } from '../utils/splitStyles';
type Elevation = 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
export type Props = Omit<React.ComponentPropsWithRef<typeof View>, 'style'> & {
/**
* Content of the `Surface`.
*/
children: React.ReactNode;
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
/**
* @supported Available in v5.x with theme version 3
* Changes shadows and background on iOS and Android.
* Used to create UI hierarchy between components.
*
* Note: If `mode` is set to `flat`, Surface doesn't have a shadow.
*
* Note: In version 2 the `elevation` prop was accepted via `style` prop i.e. `style={{ elevation: 4 }}`.
* It's no longer supported with theme version 3 and you should use `elevation` property instead.
*/
elevation?: Elevation;
/**
* @supported Available in v5.x with theme version 3
* Mode of the Surface.
* - `elevated` - Surface with a shadow and background color corresponding to set `elevation` value.
* - `flat` - Surface without a shadow, with the background color corresponding to set `elevation` value.
*/
mode?: 'flat' | 'elevated';
/**
* @optional
*/
theme?: ThemeProp;
/**
* TestID used for testing purposes
*/
testID?: string;
ref?: React.RefObject<View>;
};
const MD2Surface = forwardRef<View, Props>(
({ style, theme: overrideTheme, ...rest }: Omit<Props, 'elevation'>, ref) => {
const { elevation = 4 } = (StyleSheet.flatten(style) || {}) as ViewStyle;
const { dark: isDarkTheme, mode, colors } = useInternalTheme(overrideTheme);
return (
<Animated.View
ref={ref}
{...rest}
style={[
{
backgroundColor:
isDarkTheme && mode === 'adaptive'
? overlay(elevation, colors?.surface)
: colors?.surface,
},
elevation ? shadow(elevation) : null,
style,
]}
/>
);
}
);
const outerLayerStyleProperties: (keyof ViewStyle)[] = [
'position',
'alignSelf',
'top',
'right',
'bottom',
'left',
'start',
'end',
'flex',
'flexShrink',
'flexGrow',
'width',
'height',
'transform',
'opacity',
];
const shadowColor = '#000';
const iOSShadowOutputRanges = [
{
shadowOpacity: 0.15,
height: [0, 1, 2, 4, 6, 8],
shadowRadius: [0, 3, 6, 8, 10, 12],
},
{
shadowOpacity: 0.3,
height: [0, 1, 1, 1, 2, 4],
shadowRadius: [0, 1, 2, 3, 3, 4],
},
];
const inputRange = [0, 1, 2, 3, 4, 5];
function getStyleForShadowLayer(
elevation: Elevation,
layer: 0 | 1
): Animated.WithAnimatedValue<ShadowStyleIOS> {
if (isAnimatedValue(elevation)) {
return {
shadowColor,
shadowOpacity: elevation.interpolate({
inputRange: [0, 1],
outputRange: [0, iOSShadowOutputRanges[layer].shadowOpacity],
extrapolate: 'clamp',
}),
shadowOffset: {
width: 0,
height: elevation.interpolate({
inputRange,
outputRange: iOSShadowOutputRanges[layer].height,
}),
},
shadowRadius: elevation.interpolate({
inputRange,
outputRange: iOSShadowOutputRanges[layer].shadowRadius,
}),
};
}
return {
shadowColor,
shadowOpacity: elevation ? iOSShadowOutputRanges[layer].shadowOpacity : 0,
shadowOffset: {
width: 0,
height: iOSShadowOutputRanges[layer].height[elevation],
},
shadowRadius: iOSShadowOutputRanges[layer].shadowRadius[elevation],
};
}
const SurfaceIOS = forwardRef<
View,
Omit<Props, 'elevation'> & {
elevation: Elevation;
backgroundColor?: string | Animated.AnimatedInterpolation<string | number>;
}
>(
(
{
elevation,
style,
backgroundColor,
testID,
children,
mode = 'elevated',
...props
},
ref
) => {
const [outerLayerViewStyles, innerLayerViewStyles] = React.useMemo(() => {
const flattenedStyles = (StyleSheet.flatten(style) || {}) as ViewStyle;
const [filteredStyles, outerLayerStyles, borderRadiusStyles] =
splitStyles(
flattenedStyles,
(style) =>
outerLayerStyleProperties.includes(style) ||
style.startsWith('margin'),
(style) => style.startsWith('border') && style.endsWith('Radius')
);
if (
process.env.NODE_ENV !== 'production' &&
filteredStyles.overflow === 'hidden' &&
elevation !== 0
) {
console.warn(
'When setting overflow to hidden on Surface the shadow will not be displayed correctly. Wrap the content of your component in a separate View with the overflow style.'
);
}
const bgColor = flattenedStyles.backgroundColor || backgroundColor;
const isElevated = mode === 'elevated';
const outerLayerViewStyles = {
...(isElevated && getStyleForShadowLayer(elevation, 0)),
...outerLayerStyles,
...borderRadiusStyles,
backgroundColor: bgColor,
};
const innerLayerViewStyles = {
...(isElevated && getStyleForShadowLayer(elevation, 1)),
...filteredStyles,
...borderRadiusStyles,
flex: flattenedStyles.height || flattenedStyles.flex ? 1 : undefined,
backgroundColor: bgColor,
};
return [outerLayerViewStyles, innerLayerViewStyles];
}, [style, elevation, backgroundColor, mode]);
return (
<Animated.View
ref={ref}
style={outerLayerViewStyles}
testID={`${testID}-outer-layer`}
>
<Animated.View {...props} style={innerLayerViewStyles} testID={testID}>
{children}
</Animated.View>
</Animated.View>
);
}
);
/**
* Surface is a basic container that can give depth to an element with elevation shadow.
* On dark theme with `adaptive` mode, surface is constructed by also placing a semi-transparent white overlay over a component surface.
* See [Dark Theme](https://callstack.github.io/react-native-paper/docs/guides/theming#dark-theme) for more information.
* Overlay and shadow can be applied by specifying the `elevation` property both on Android and iOS.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Surface, Text } from 'react-native-paper';
* import { StyleSheet } from 'react-native';
*
* const MyComponent = () => (
* <Surface style={styles.surface} elevation={4}>
* <Text>Surface</Text>
* </Surface>
* );
*
* export default MyComponent;
*
* const styles = StyleSheet.create({
* surface: {
* padding: 8,
* height: 80,
* width: 80,
* alignItems: 'center',
* justifyContent: 'center',
* },
* });
* ```
*/
const Surface = forwardRef<View, Props>(
(
{
elevation = 1,
children,
theme: overridenTheme,
style,
testID = 'surface',
mode = 'elevated',
...props
}: Props,
ref
) => {
const theme = useInternalTheme(overridenTheme);
if (!theme.isV3)
return (
<MD2Surface {...props} theme={theme} style={style} ref={ref}>
{children}
</MD2Surface>
);
const { colors } = theme;
const inputRange = [0, 1, 2, 3, 4, 5];
const backgroundColor = (() => {
if (isAnimatedValue(elevation)) {
return elevation.interpolate({
inputRange,
outputRange: inputRange.map((elevation) => {
return colors.elevation?.[`level${elevation as MD3Elevation}`];
}),
});
}
return colors.elevation?.[`level${elevation}`];
})();
const isElevated = mode === 'elevated';
if (Platform.OS === 'web') {
const { pointerEvents = 'auto' } = props;
return (
<Animated.View
{...props}
pointerEvents={pointerEvents}
ref={ref}
testID={testID}
style={[
{ backgroundColor },
elevation && isElevated ? shadow(elevation, theme.isV3) : null,
style,
]}
>
{children}
</Animated.View>
);
}
if (Platform.OS === 'android') {
const elevationLevel = [0, 3, 6, 9, 12, 15];
const getElevationAndroid = () => {
if (isAnimatedValue(elevation)) {
return elevation.interpolate({
inputRange,
outputRange: elevationLevel,
});
}
return elevationLevel[elevation];
};
const { margin, padding, transform, borderRadius } = (StyleSheet.flatten(
style
) || {}) as ViewStyle;
const outerLayerStyles = { margin, padding, transform, borderRadius };
const sharedStyle = [{ backgroundColor }, style];
return (
<Animated.View
{...props}
testID={testID}
ref={ref}
style={[
{
backgroundColor,
transform,
},
outerLayerStyles,
sharedStyle,
isElevated && {
elevation: getElevationAndroid(),
},
]}
>
{children}
</Animated.View>
);
}
return (
<SurfaceIOS
{...props}
ref={ref}
elevation={elevation}
backgroundColor={backgroundColor}
style={style}
testID={testID}
mode={mode}
>
{children}
</SurfaceIOS>
);
}
);
export default Surface;