import color from 'color'; import * as React from 'react'; import { Animated, SafeAreaView, StyleProp, StyleSheet, TouchableWithoutFeedback, View, ViewStyle, } from 'react-native'; import { withTheme } from '../../core/theming'; import isNativeAnimationSupported from '../../utils/isNativeAnimationSupported'; import Card from '../Card/Card'; import type { IconSource } from '../Icon'; import Text from '../Typography/Text'; import FAB from './FAB'; 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 * - `labelTextColor`: custom label text color of the action item * - `style`: pass additional styles for the fab item, for example, `backgroundColor` * - `labelStyle`: pass additional styles for the fab item label, for example, `backgroundColor` * - `small`: boolean describing whether small or normal sized FAB is rendered. Defaults to `true` * - `onPress`: callback that is called when `FAB` is pressed (required) */ actions: Array<{ icon: IconSource; label?: string; color?: string; labelTextColor?: string; accessibilityLabel?: string; style?: StyleProp; labelStyle?: StyleProp; small?: boolean; onPress: () => void; testID?: string; }>; /** * 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?: () => void; /** * 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 }) => void; /** * 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?: StyleProp; /** * Style for the FAB. It allows to pass the FAB button styles, such as backgroundColor. */ fabStyle?: StyleProp; /** * @optional */ theme: ReactNativePaper.Theme; /** * Pass down testID from Group props to FAB. */ testID?: string; }; /** * 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. * *
* *
* * ## Usage * ```js * import * as React from 'react'; * import { FAB, Portal, Provider } from 'react-native-paper'; * * const MyComponent = () => { * const [state, setState] = React.useState({ open: false }); * * const onStateChange = ({ open }) => setState({ open }); * * const { open } = state; * * return ( * * * console.log('Pressed add') }, * { * icon: 'star', * label: 'Star', * onPress: () => console.log('Pressed star'), * }, * { * icon: 'email', * label: 'Email', * onPress: () => console.log('Pressed email'), * }, * { * icon: 'bell', * label: 'Remind', * onPress: () => console.log('Pressed notifications'), * small: false, * }, * ]} * onStateChange={onStateChange} * onPress={() => { * if (open) { * // do something if the speed dial is open * } * }} * /> * * * ); * }; * * export default MyComponent; * ``` */ const FABGroup = ({ actions, icon, open, onPress, accessibilityLabel, theme, style, fabStyle, visible, testID, onStateChange, color: colorProp, }: Props) => { const { current: backdrop } = React.useRef( new Animated.Value(0) ); const animations = React.useRef( actions.map(() => new Animated.Value(open ? 1 : 0)) ); const [prevActions, setPrevActions] = React.useState< | { icon: IconSource; label?: string; color?: string; accessibilityLabel?: string; style?: StyleProp; onPress: () => void; testID?: string; }[] | null >(null); const { scale } = theme.animation; React.useEffect(() => { if (open) { Animated.parallel([ Animated.timing(backdrop, { toValue: 1, duration: 250 * scale, useNativeDriver: isNativeAnimationSupported(), }), Animated.stagger( 50 * scale, animations.current .map((animation) => Animated.timing(animation, { toValue: 1, duration: 150 * scale, useNativeDriver: isNativeAnimationSupported(), }) ) .reverse() ), ]).start(); } else { Animated.parallel([ Animated.timing(backdrop, { toValue: 0, duration: 200 * scale, useNativeDriver: isNativeAnimationSupported(), }), ...animations.current.map((animation) => Animated.timing(animation, { toValue: 0, duration: 150 * scale, useNativeDriver: isNativeAnimationSupported(), }) ), ]).start(); } }, [open, actions, backdrop, scale]); const close = () => onStateChange({ open: false }); const toggle = () => onStateChange({ open: !open }); const { colors } = theme; const labelColor = theme.dark ? colors.text : color(colors.text).fade(0.54).rgb().string(); const backdropOpacity = open ? backdrop.interpolate({ inputRange: [0, 0.5, 1], outputRange: [0, 1, 1], }) : backdrop; const opacities = animations.current; const scales = opacities.map((opacity) => open ? opacity.interpolate({ inputRange: [0, 1], outputRange: [0.8, 1], }) : 1 ); if (actions.length !== prevActions?.length) { animations.current = actions.map( (_, i) => animations.current[i] || new Animated.Value(open ? 1 : 0) ); setPrevActions(actions); } return ( {actions.map((it, i) => ( {it.label && ( } onPress={() => { it.onPress(); close(); }} accessibilityLabel={ it.accessibilityLabel !== 'undefined' ? it.accessibilityLabel : it.label } // @ts-expect-error We keep old a11y props for backwards compat with old RN versions accessibilityTraits="button" accessibilityComponentType="button" accessibilityRole="button" > {it.label} )} } onPress={() => { it.onPress(); close(); }} accessibilityLabel={ typeof it.accessibilityLabel !== 'undefined' ? it.accessibilityLabel : it.label } // @ts-expect-error We keep old a11y props for backwards compat with old RN versions accessibilityTraits="button" accessibilityComponentType="button" accessibilityRole="button" testID={it.testID} visible={open} /> ))} { onPress?.(); toggle(); }} icon={icon} color={colorProp} accessibilityLabel={accessibilityLabel} // @ts-expect-error We keep old a11y props for backwards compat with old RN versions accessibilityTraits="button" accessibilityComponentType="button" accessibilityRole="button" accessibilityState={{ expanded: open }} style={[styles.fab, fabStyle]} visible={visible} testID={testID} /> ); }; FABGroup.displayName = 'FAB.Group'; export default withTheme(FABGroup); // @component-docs ignore-next-line const FABGroupWithTheme = withTheme(FABGroup); // @component-docs ignore-next-line export { FABGroupWithTheme as 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: { marginBottom: 16, flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center', }, });