-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathCard.tsx
368 lines (337 loc) · 9.38 KB
/
Card.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
import * as React from 'react';
import {
Animated,
GestureResponderEvent,
StyleProp,
StyleSheet,
Pressable,
View,
ViewStyle,
} from 'react-native';
import useLatestCallback from 'use-latest-callback';
import CardActions from './CardActions';
import CardContent from './CardContent';
import CardCover from './CardCover';
import CardTitle from './CardTitle';
import { getCardColors } from './utils';
import { useInternalTheme } from '../../core/theming';
import type { $Omit, ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';
import hasTouchHandler from '../../utils/hasTouchHandler';
import { splitStyles } from '../../utils/splitStyles';
import Surface from '../Surface';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type OutlinedCardProps = {
mode: 'outlined';
elevation?: never;
};
type ElevatedCardProps = {
mode?: 'elevated';
elevation?: number;
};
type ContainedCardProps = {
mode?: 'contained';
elevation?: never;
};
type HandlePressType = 'in' | 'out';
type Mode = 'elevated' | 'outlined' | 'contained';
export type Props = $Omit<React.ComponentProps<typeof Surface>, 'mode'> & {
/**
* Mode of the Card.
* - `elevated` - Card with elevation.
* - `contained` - Card without outline and elevation @supported Available in v5.x with theme version 3
* - `outlined` - Card with an outline.
*/
mode?: Mode;
/**
* Content of the `Card`.
*/
children: React.ReactNode;
/**
* Function to execute on long press.
*/
onLongPress?: () => void;
/**
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Function to execute as soon as the touchable element is pressed and invoked even before onPress.
*/
onPressIn?: (e: GestureResponderEvent) => void;
/**
* Function to execute as soon as the touch is released even before onPress.
*/
onPressOut?: (e: GestureResponderEvent) => void;
/**
* The number of milliseconds a user must touch the element before executing `onLongPress`.
*/
delayLongPress?: number;
/**
* If true, disable all interactions for this component.
*/
disabled?: boolean;
/**
* Changes Card shadow and background on iOS and Android.
*/
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
/**
* Style of card's inner content.
*/
contentStyle?: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme?: ThemeProp;
/**
* Pass down testID from card props to touchable
*/
testID?: string;
/**
* Pass down accessible from card props to touchable
*/
accessible?: boolean;
};
/**
* A card is a sheet of material that serves as an entry point to more detailed information.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Avatar, Button, Card, Text } from 'react-native-paper';
*
* const LeftContent = props => <Avatar.Icon {...props} icon="folder" />
*
* const MyComponent = () => (
* <Card>
* <Card.Title title="Card Title" subtitle="Card Subtitle" left={LeftContent} />
* <Card.Content>
* <Text variant="titleLarge">Card title</Text>
* <Text variant="bodyMedium">Card content</Text>
* </Card.Content>
* <Card.Cover source={{ uri: 'https://picsum.photos/700' }} />
* <Card.Actions>
* <Button>Cancel</Button>
* <Button>Ok</Button>
* </Card.Actions>
* </Card>
* );
*
* export default MyComponent;
* ```
*/
const Card = (
{
elevation: cardElevation = 1,
delayLongPress,
onPress,
onLongPress,
onPressOut,
onPressIn,
mode: cardMode = 'elevated',
children,
style,
contentStyle,
theme: themeOverrides,
testID = 'card',
accessible,
disabled,
...rest
}: (OutlinedCardProps | ElevatedCardProps | ContainedCardProps) & Props,
ref: React.ForwardedRef<View>
) => {
const theme = useInternalTheme(themeOverrides);
const isMode = React.useCallback(
(modeToCompare: Mode) => {
return cardMode === modeToCompare;
},
[cardMode]
);
const hasPassedTouchHandler = hasTouchHandler({
onPress,
onLongPress,
onPressIn,
onPressOut,
});
// Default animated value
const { current: elevation } = React.useRef<Animated.Value>(
new Animated.Value(cardElevation)
);
// Dark adaptive animated value, used in case of toggling the theme,
// it prevents animating the background with native drivers inside Surface
const { current: elevationDarkAdaptive } = React.useRef<Animated.Value>(
new Animated.Value(cardElevation)
);
const { animation, dark, mode, roundness, isV3 } = theme;
const prevDarkRef = React.useRef<boolean>(dark);
React.useEffect(() => {
prevDarkRef.current = dark;
});
const prevDark = prevDarkRef.current;
const isAdaptiveMode = mode === 'adaptive';
const animationDuration = 150 * animation.scale;
React.useEffect(() => {
/**
* Resets animations values if updating to dark adaptive mode,
* otherwise, any card that is in the middle of animation while
* toggling the theme will stay at that animated value until
* the next press-in
*/
if (dark && isAdaptiveMode && !prevDark) {
elevation.setValue(cardElevation);
elevationDarkAdaptive.setValue(cardElevation);
}
}, [
prevDark,
dark,
isAdaptiveMode,
cardElevation,
elevation,
elevationDarkAdaptive,
]);
const runElevationAnimation = (pressType: HandlePressType) => {
const isPressTypeIn = pressType === 'in';
if (dark && isAdaptiveMode) {
Animated.timing(elevationDarkAdaptive, {
toValue: isPressTypeIn ? (isV3 ? 2 : 8) : cardElevation,
duration: animationDuration,
useNativeDriver: false,
}).start();
} else {
Animated.timing(elevation, {
toValue: isPressTypeIn ? (isV3 ? 2 : 8) : cardElevation,
duration: animationDuration,
useNativeDriver: false,
}).start();
}
};
const handlePressIn = useLatestCallback((e: GestureResponderEvent) => {
onPressIn?.(e);
runElevationAnimation('in');
});
const handlePressOut = useLatestCallback((e: GestureResponderEvent) => {
onPressOut?.(e);
runElevationAnimation('out');
});
const total = React.Children.count(children);
const siblings = React.Children.map(children, (child) =>
React.isValidElement(child) && child.type
? (child.type as any).displayName
: null
);
const computedElevation =
dark && isAdaptiveMode ? elevationDarkAdaptive : elevation;
const { backgroundColor, borderColor: themedBorderColor } = getCardColors({
theme,
mode: cardMode,
});
const flattenedStyles = (StyleSheet.flatten(style) || {}) as ViewStyle;
const { borderColor = themedBorderColor } = flattenedStyles;
const [, borderRadiusStyles] = splitStyles(
flattenedStyles,
(style) => style.startsWith('border') && style.endsWith('Radius')
);
const borderRadiusCombinedStyles = {
borderRadius: (isV3 ? 3 : 1) * roundness,
...borderRadiusStyles,
};
const content = (
<View style={[styles.innerContainer, contentStyle]} testID={testID}>
{React.Children.map(children, (child, index) =>
React.isValidElement(child)
? React.cloneElement(child as React.ReactElement<any>, {
index,
total,
siblings,
borderRadiusStyles,
})
: child
)}
</View>
);
return (
<Surface
ref={ref}
style={[
isV3 && !isMode('elevated') && { backgroundColor },
!isV3 && isMode('outlined')
? styles.resetElevation
: {
elevation: computedElevation as unknown as number,
},
borderRadiusCombinedStyles,
style,
]}
theme={theme}
{...(isV3 && {
elevation: isMode('elevated') ? computedElevation : 0,
})}
testID={`${testID}-container`}
{...rest}
>
{isMode('outlined') && (
<View
pointerEvents="none"
testID={`${testID}-outline`}
style={[
{
borderColor,
},
styles.outline,
borderRadiusCombinedStyles,
]}
/>
)}
{hasPassedTouchHandler ? (
<Pressable
accessible={accessible}
unstable_pressDelay={0}
disabled={disabled}
delayLongPress={delayLongPress}
onLongPress={onLongPress}
onPress={onPress}
onPressIn={handlePressIn}
onPressOut={handlePressOut}
>
{content}
</Pressable>
) : (
content
)}
</Surface>
);
};
const Component = forwardRef(Card);
Component.displayName = 'Card';
const CardComponent = Component as typeof Component & {
Content: typeof CardContent;
Actions: typeof CardActions;
Cover: typeof CardCover;
Title: typeof CardTitle;
};
// @component ./CardContent.tsx
CardComponent.Content = CardContent;
// @component ./CardActions.tsx
CardComponent.Actions = CardActions;
// @component ./CardCover.tsx
CardComponent.Cover = CardCover;
// @component ./CardTitle.tsx
CardComponent.Title = CardTitle;
const styles = StyleSheet.create({
innerContainer: {
flexShrink: 1,
},
outline: {
borderWidth: 1,
position: 'absolute',
width: '100%',
height: '100%',
zIndex: 2,
},
resetElevation: {
elevation: 0,
},
});
export default CardComponent;