import React, {useLayoutEffect, useMemo, useRef} from 'react'; import type {LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native'; import {Animated, Easing, Platform, View} from 'react-native'; import type {ButtonSizeType} from '../Button'; import type {IconName} from '../Icon'; import {IconButton} from '../IconButton'; type Styles = { Fab?: StyleProp; FabItem?: StyleProp; }; export type FabProps = { isActive?: boolean; icons?: IconName[]; animationDuration?: number; onPressFab: () => void; onPressItem?: (item?: IconName) => void; buttonSize?: ButtonSizeType | number; fabIcon?: IconName; gap?: number; style?: StyleProp; styles?: Styles; iconButtonProps?: Omit< React.ComponentProps, 'icon' | 'onPress' | 'size' >; }; function FloatingActionButtons({ isActive = false, style, styles, fabIcon = 'Plus', icons = [], onPressFab, onPressItem, buttonSize = 'medium', gap = Platform.select({ web: 60, default: 12, }), animationDuration = 200, iconButtonProps, }: FabProps): JSX.Element { const {Fab, FabItem} = styles ?? {}; const spinValue = useRef(new Animated.Value(0)); const positionValue = useRef(new Animated.Value(0)); const fabHeight = useRef(0); useLayoutEffect(() => { const config = { toValue: isActive ? 1 : 0, duration: animationDuration, easing: Easing.linear, useNativeDriver: true, }; Animated.parallel([ Animated.timing(spinValue.current, config), Animated.timing(positionValue.current, config), ]).start(); }, [isActive, animationDuration]); const offsets = useMemo( () => icons?.map((_, idx) => positionValue.current.interpolate({ inputRange: [0, 1], outputRange: [0, -1 * (idx + 1) * (fabHeight.current + gap)], }), ), [icons, fabHeight, gap], ); const onLayout = (e: LayoutChangeEvent): void => { fabHeight.current = e.nativeEvent.layout.height; }; return ( {icons.map((icon, idx) => { return ( onPressItem?.(icon)} size={buttonSize} testID={icon} {...iconButtonProps} /> ); })} ); } export {FloatingActionButtons as Fab};