import { Badge } from '@react-navigation/elements'; import type { Route } from '@react-navigation/native'; import React from 'react'; import { type StyleProp, StyleSheet, type TextStyle, View, type ViewStyle, } from 'react-native'; type Props = { route: Route; variant: 'uikit' | 'material'; size: 'compact' | 'regular'; badge?: string | number; badgeStyle?: StyleProp; activeOpacity: number; inactiveOpacity: number; activeTintColor: string; inactiveTintColor: string; renderIcon: (props: { focused: boolean; color: string; size: number; }) => React.ReactNode; allowFontScaling?: boolean; style: StyleProp; }; /** * Icon sizes taken from Apple HIG * https://developer.apple.com/design/human-interface-guidelines/tab-bars */ const ICON_SIZE_WIDE = 31; const ICON_SIZE_WIDE_COMPACT = 23; const ICON_SIZE_TALL = 28; const ICON_SIZE_TALL_COMPACT = 20; const ICON_SIZE_ROUND = 25; const ICON_SIZE_ROUND_COMPACT = 18; const ICON_SIZE_MATERIAL = 24; export function TabBarIcon({ route: _, variant, size, badge, badgeStyle, activeOpacity, inactiveOpacity, activeTintColor, inactiveTintColor, renderIcon, allowFontScaling, style, }: Props) { const iconSize = variant === 'material' ? ICON_SIZE_MATERIAL : size === 'compact' ? ICON_SIZE_ROUND_COMPACT : ICON_SIZE_ROUND; // We render the icon twice at the same position on top of each other: // active and inactive one, so we can fade between them. return ( = 0.54 layout bug minWidth: iconSize, }, ]} > {renderIcon({ focused: true, size: iconSize, color: activeTintColor, })} {renderIcon({ focused: false, size: iconSize, color: inactiveTintColor, })} {badge} ); } const styles = StyleSheet.create({ icon: { // We render the icon twice at the same position on top of each other: // active and inactive one, so we can fade between them: // Cover the whole iconContainer: position: 'absolute', alignSelf: 'center', alignItems: 'center', justifyContent: 'center', height: '100%', width: '100%', }, wrapperUikit: { width: ICON_SIZE_WIDE, height: ICON_SIZE_TALL, }, wrapperUikitCompact: { width: ICON_SIZE_WIDE_COMPACT, height: ICON_SIZE_TALL_COMPACT, }, wrapperMaterial: { width: ICON_SIZE_MATERIAL, height: ICON_SIZE_MATERIAL, }, badge: { position: 'absolute', end: -3, top: -3, }, });