|
| 1 | +import React, {useMemo} from 'react'; |
| 2 | +import Animated, {FadeInRight, LinearTransition, FadeOutRight} from 'react-native-reanimated'; |
| 3 | +import {TouchableOpacity, type StyleProp, type TextStyle, type TouchableOpacityProps} from 'react-native'; |
| 4 | + |
| 5 | +import {useTheme} from '../../theme/useTheme.hook'; |
| 6 | +import {type IconProps} from '../../icons/icon-props'; |
| 7 | +import {EditIcon} from '../../icons/edit-icon/EditIcon.component'; |
| 8 | +import {useTypography} from '../../typography/useTypography.component'; |
| 9 | +import {getBaseFloatingButtonShape, styles} from './floating-button.styles'; |
| 10 | + |
| 11 | +export enum FloatingButtonSize { |
| 12 | + SMALL = 'SMALL', |
| 13 | + BIG = 'BIG', |
| 14 | +} |
| 15 | + |
| 16 | +export enum FloatingButtonType { |
| 17 | + SURFACE = 'SURFACE', |
| 18 | + PRIMARY = 'PRIMARY', |
| 19 | + SECONDARY = 'SECONDARY', |
| 20 | + TERTIARY = 'TERTIARY', |
| 21 | +} |
| 22 | + |
| 23 | +export interface FloatingButtonProps extends TouchableOpacityProps { |
| 24 | + type?: FloatingButtonType; |
| 25 | + label?: string; |
| 26 | + extended?: boolean; |
| 27 | + Icon?: React.FC<IconProps>; |
| 28 | + iconProps?: IconProps; |
| 29 | + size?: FloatingButtonSize; |
| 30 | + labelStyle?: StyleProp<TextStyle>; |
| 31 | +} |
| 32 | + |
| 33 | +export const FloatingButton: React.FC<FloatingButtonProps> = ({ |
| 34 | + label, |
| 35 | + iconProps, |
| 36 | + labelStyle, |
| 37 | + Icon = EditIcon, |
| 38 | + extended = true, |
| 39 | + size = FloatingButtonSize.SMALL, |
| 40 | + type = FloatingButtonType.PRIMARY, |
| 41 | + style, |
| 42 | + ...props |
| 43 | +}) => { |
| 44 | + const {labelLarge} = useTypography(); |
| 45 | + const {shadow, primaryContainer, surfaceContainer, secondaryContainer, tertiaryContainer, primary} = useTheme(); |
| 46 | + |
| 47 | + const colors = useMemo( |
| 48 | + () => ({ |
| 49 | + [FloatingButtonType.SURFACE]: {backgroundColor: surfaceContainer.backgroundHigh, iconColor: primary.background}, |
| 50 | + [FloatingButtonType.PRIMARY]: {backgroundColor: primaryContainer.background, iconColor: primaryContainer.text}, |
| 51 | + [FloatingButtonType.SECONDARY]: {backgroundColor: secondaryContainer.background, iconColor: secondaryContainer.text}, |
| 52 | + [FloatingButtonType.TERTIARY]: {backgroundColor: tertiaryContainer.background, iconColor: tertiaryContainer.text}, |
| 53 | + }), |
| 54 | + [] |
| 55 | + ); |
| 56 | + |
| 57 | + const [paddingStart, paddingEnd] = label && extended ? [16, 20] : [0, 0]; |
| 58 | + |
| 59 | + return ( |
| 60 | + <TouchableOpacity {...props}> |
| 61 | + <Animated.View |
| 62 | + layout={LinearTransition} |
| 63 | + style={[ |
| 64 | + styles.container, |
| 65 | + {backgroundColor: colors[type].backgroundColor, shadowColor: shadow, paddingStart, paddingEnd}, |
| 66 | + getBaseFloatingButtonShape(size).container, |
| 67 | + style, |
| 68 | + ]}> |
| 69 | + {Icon ? <Icon size={24} color={colors[type].iconColor} {...iconProps} /> : null} |
| 70 | + {label && extended ? ( |
| 71 | + <Animated.Text layout={LinearTransition} exiting={FadeOutRight} entering={FadeInRight} style={[labelLarge, labelStyle]}> |
| 72 | + {label} |
| 73 | + </Animated.Text> |
| 74 | + ) : null} |
| 75 | + </Animated.View> |
| 76 | + </TouchableOpacity> |
| 77 | + ); |
| 78 | +}; |
0 commit comments