-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathFABGroup.js
366 lines (349 loc) · 9.85 KB
/
FABGroup.js
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
/* @flow */
import * as React from 'react';
import {
View,
StyleSheet,
Animated,
TouchableWithoutFeedback,
StatusBar,
SafeAreaView,
} from 'react-native';
import { polyfill } from 'react-lifecycles-compat';
import color from 'color';
import FAB from './FAB';
import Text from '../Typography/Text';
import Card from '../Card/Card';
import { withTheme } from '../../core/theming';
import type { Theme } from '../../types';
import type { IconSource } from '../Icon';
type Props = {|
/**
* Action items to display in the form of a speed dial.
* An action item should contain the following properties:
* - `icon`: icon to display (required)
* - `label`: optional label text
* - `accessibilityLabel`: accessibility label for the action, uses label by default if specified
* - `color`: custom icon color of the action item
* - `style`: pass additional styles for the fab item, for example, `backgroundColor`
* - `onPress`: callback that is called when `FAB` is pressed (required)
*/
actions: Array<{
icon: string,
label?: string,
color?: string,
accessibilityLabel?: string,
style?: any,
onPress: () => mixed,
}>,
/**
* Icon to display for the `FAB`.
* You can toggle it based on whether the speed dial is open to display a different icon.
*/
icon: IconSource,
/**
* Accessibility label for the FAB. This is read by the screen reader when the user taps the FAB.
*/
accessibilityLabel?: string,
/**
* Custom color for the `FAB`.
*/
color?: string,
/**
* Function to execute on pressing the `FAB`.
*/
onPress?: () => mixed,
/**
* Whether the speed dial is open.
*/
open: boolean,
/**
* Callback which is called on opening and closing the speed dial.
* The open state needs to be updated when it's called, otherwise the change is dropped.
*/
onStateChange: (state: { open: boolean }) => mixed,
/**
* Whether `FAB` is currently visible.
*/
visible: boolean,
/**
* Style for the group. You can use it to pass additional styles if you need.
* For example, you can set an additional padding if you have a tab bar at the bottom.
*/
style?: any,
/**
* Style for the FAB. It allows to pass the FAB button styles, such as backgroundColor.
*/
fabStyle?: any,
/**
* @optional
*/
theme: Theme,
|};
type State = {
backdrop: Animated.Value,
animations: Animated.Value[],
};
/**
* A component to display a stack of FABs with related actions in a speed dial.
* To render the group above other components, you'll need to wrap it with the [`Portal`](portal.html) component.
*
* <div class="screenshots">
* <img src="screenshots/fab-group.png" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { FAB, Portal } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* open: false,
* };
*
* render() {
* return (
* <Portal>
* <FAB.Group
* open={this.state.open}
* icon={this.state.open ? 'today' : 'add'}
* actions={[
* { icon: 'add', onPress: () => console.log('Pressed add') },
* { icon: 'star', label: 'Star', onPress: () => console.log('Pressed star')},
* { icon: 'email', label: 'Email', onPress: () => console.log('Pressed email') },
* { icon: 'notifications', label: 'Remind', onPress: () => console.log('Pressed notifications') },
* ]}
* onStateChange={({ open }) => this.setState({ open })}
* onPress={() => {
* if (this.state.open) {
* // do something if the speed dial is open
* }
* }}
* />
* </Portal>
* );
* }
* }
* ```
*/
class FABGroup extends React.Component<Props, State> {
static displayName = 'FAB.Group';
static getDerivedStateFromProps(nextProps, prevState) {
return {
animations: nextProps.actions.map(
(_, i) =>
prevState.animations[i] || new Animated.Value(nextProps.open ? 1 : 0)
),
};
}
state = {
backdrop: new Animated.Value(0),
animations: [],
};
componentDidUpdate(prevProps) {
if (this.props.open === prevProps.open) {
return;
}
if (this.props.open) {
Animated.parallel([
Animated.timing(this.state.backdrop, {
toValue: 1,
duration: 250,
useNativeDriver: true,
}),
Animated.stagger(
50,
this.state.animations
.map(animation =>
Animated.timing(animation, {
toValue: 1,
duration: 150,
useNativeDriver: true,
})
)
.reverse()
),
]).start();
} else {
Animated.parallel([
Animated.timing(this.state.backdrop, {
toValue: 0,
duration: 200,
useNativeDriver: true,
}),
...this.state.animations.map(animation =>
Animated.timing(animation, {
toValue: 0,
duration: 150,
useNativeDriver: true,
})
),
]).start();
}
}
_close = () => this.props.onStateChange({ open: false });
_toggle = () => this.props.onStateChange({ open: !this.props.open });
render() {
const {
actions,
icon,
open,
onPress,
accessibilityLabel,
theme,
style,
fabStyle,
visible,
} = this.props;
const { colors } = theme;
const labelColor = theme.dark
? colors.text
: color(colors.text)
.fade(0.54)
.rgb()
.string();
const backdropOpacity = open
? this.state.backdrop.interpolate({
inputRange: [0, 0.5, 1],
outputRange: [0, 1, 1],
})
: this.state.backdrop;
const opacities = this.state.animations;
const scales = opacities.map(
opacity =>
open
? opacity.interpolate({
inputRange: [0, 1],
outputRange: [0.8, 1],
})
: 1
);
return (
<View pointerEvents="box-none" style={[styles.container, style]}>
{open ? <StatusBar barStyle="light-content" /> : null}
<TouchableWithoutFeedback onPress={this._close}>
<Animated.View
pointerEvents={open ? 'auto' : 'none'}
style={[
styles.backdrop,
{
opacity: backdropOpacity,
backgroundColor: colors.backdrop,
},
]}
/>
</TouchableWithoutFeedback>
<SafeAreaView pointerEvents="box-none" style={styles.safeArea}>
<View pointerEvents={open ? 'box-none' : 'none'}>
{actions.map((it, i) => (
<View
key={i} // eslint-disable-line react/no-array-index-key
style={styles.item}
pointerEvents="box-none"
>
{it.label && (
<Card
style={[
styles.label,
{
transform: [{ scale: scales[i] }],
opacity: opacities[i],
},
]}
onPress={() => {
it.onPress();
this._close();
}}
accessibilityLabel={
it.accessibilityLabel !== 'undefined'
? it.accessibilityLabel
: it.label
}
accessibilityTraits="button"
accessibilityComponentType="button"
accessibilityRole="button"
>
<Text style={{ color: labelColor }}>{it.label}</Text>
</Card>
)}
<FAB
small
icon={it.icon}
color={it.color}
style={[
{
transform: [{ scale: scales[i] }],
opacity: opacities[i],
backgroundColor: theme.colors.surface,
},
it.style,
]}
onPress={() => {
it.onPress();
this._close();
}}
accessibilityLabel={
typeof it.accessibilityLabel !== 'undefined'
? it.accessibilityLabel
: it.label
}
accessibilityTraits="button"
accessibilityComponentType="button"
accessibilityRole="button"
/>
</View>
))}
</View>
<FAB
onPress={() => {
onPress && onPress();
this._toggle();
}}
icon={icon}
color={this.props.color}
accessibilityLabel={accessibilityLabel}
accessibilityTraits="button"
accessibilityComponentType="button"
accessibilityRole="button"
style={[styles.fab, fabStyle]}
visible={visible}
/>
</SafeAreaView>
</View>
);
}
}
polyfill(FABGroup);
export default withTheme(FABGroup);
const styles = StyleSheet.create({
safeArea: {
alignItems: 'flex-end',
},
container: {
...StyleSheet.absoluteFillObject,
justifyContent: 'flex-end',
},
fab: {
marginHorizontal: 16,
marginBottom: 16,
marginTop: 0,
},
backdrop: {
...StyleSheet.absoluteFillObject,
},
label: {
borderRadius: 5,
paddingHorizontal: 12,
paddingVertical: 6,
marginVertical: 8,
marginHorizontal: 16,
elevation: 2,
},
item: {
marginHorizontal: 24,
marginBottom: 16,
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center',
},
});