IconSymbol.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Fallback for using MaterialIcons on Android and web.
  2. import MaterialIcons from '@expo/vector-icons/MaterialIcons';
  3. import { SymbolWeight, SymbolViewProps } from 'expo-symbols';
  4. import { ComponentProps } from 'react';
  5. import { OpaqueColorValue, type StyleProp, type TextStyle } from 'react-native';
  6. type IconMapping = Record<SymbolViewProps['name'], ComponentProps<typeof MaterialIcons>['name']>;
  7. type IconSymbolName = keyof typeof MAPPING;
  8. /**
  9. * Add your SF Symbols to Material Icons mappings here.
  10. * - see Material Icons in the [Icons Directory](https://icons.expo.fyi).
  11. * - see SF Symbols in the [SF Symbols](https://developer.apple.com/sf-symbols/) app.
  12. */
  13. const MAPPING = {
  14. 'house.fill': 'home',
  15. 'paperplane.fill': 'send',
  16. 'chevron.left.forwardslash.chevron.right': 'code',
  17. 'chevron.right': 'chevron-right',
  18. } as IconMapping;
  19. /**
  20. * An icon component that uses native SF Symbols on iOS, and Material Icons on Android and web.
  21. * This ensures a consistent look across platforms, and optimal resource usage.
  22. * Icon `name`s are based on SF Symbols and require manual mapping to Material Icons.
  23. */
  24. export function IconSymbol({
  25. name,
  26. size = 24,
  27. color,
  28. style,
  29. }: {
  30. name: IconSymbolName;
  31. size?: number;
  32. color: string | OpaqueColorValue;
  33. style?: StyleProp<TextStyle>;
  34. weight?: SymbolWeight;
  35. }) {
  36. return <MaterialIcons color={color} size={size} name={MAPPING[name]} style={style} />;
  37. }