|
| 1 | +import { PaletteColorToken, useTheme } from '@/src/common/theme'; |
| 2 | +import { |
| 3 | + Ionicons, |
| 4 | + MaterialCommunityIcons, |
| 5 | + MaterialIcons, |
| 6 | +} from '@expo/vector-icons'; |
| 7 | +import { ComponentProps, ComponentType, ReactNode } from 'react'; |
| 8 | + |
| 9 | +function defineIcon<G extends string>( |
| 10 | + IconSet: ComponentType<any> & { glyphMap: Record<G, number | string> }, |
| 11 | + name: NoInfer<G>, |
| 12 | +) { |
| 13 | + return { |
| 14 | + iconSet: IconSet, |
| 15 | + name, |
| 16 | + render: (p: object) => <IconSet name={name} {...p} />, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Map of icon names to their icon set definitions |
| 22 | + */ |
| 23 | +export const IconsMap = { |
| 24 | + add: defineIcon(Ionicons, 'add-circle'), |
| 25 | + addOutline: defineIcon(Ionicons, 'add-circle-outline'), |
| 26 | + arrowBack: defineIcon(Ionicons, 'arrow-back'), |
| 27 | + ban: defineIcon(Ionicons, 'ban'), |
| 28 | + camera: defineIcon(Ionicons, 'camera-outline'), |
| 29 | + checkmark: defineIcon(Ionicons, 'checkmark'), |
| 30 | + checkmarkCircle: defineIcon(Ionicons, 'checkmark-circle'), |
| 31 | + checkmarkSharp: defineIcon(Ionicons, 'checkmark-sharp'), |
| 32 | + chevronBack: defineIcon(Ionicons, 'chevron-back'), |
| 33 | + chevronDown: defineIcon(Ionicons, 'chevron-down'), |
| 34 | + chevronForward: defineIcon(Ionicons, 'chevron-forward'), |
| 35 | + close: defineIcon(Ionicons, 'close'), |
| 36 | + closeSharp: defineIcon(Ionicons, 'close-sharp'), |
| 37 | + copy: defineIcon(Ionicons, 'copy-outline'), |
| 38 | + dotsVertical: defineIcon(MaterialCommunityIcons, 'dots-vertical'), |
| 39 | + emoji: defineIcon(MaterialIcons, 'emoji-emotions'), |
| 40 | + flag: defineIcon(Ionicons, 'flag-outline'), |
| 41 | + home: defineIcon(Ionicons, 'home-outline'), |
| 42 | + image: defineIcon(Ionicons, 'image-outline'), |
| 43 | + images: defineIcon(Ionicons, 'images-outline'), |
| 44 | + more: defineIcon(Ionicons, 'ellipsis-horizontal'), |
| 45 | + personAdd: defineIcon(Ionicons, 'person-add'), |
| 46 | + personAddOutline: defineIcon(Ionicons, 'person-add-outline'), |
| 47 | + personOutline: defineIcon(Ionicons, 'person-outline'), |
| 48 | + personRemove: defineIcon(Ionicons, 'person-remove'), |
| 49 | + quote: defineIcon(Ionicons, 'create'), |
| 50 | + reaction: defineIcon(Ionicons, 'heart'), |
| 51 | + reactionOutline: defineIcon(Ionicons, 'heart-outline'), |
| 52 | + remove: defineIcon(Ionicons, 'remove-circle-outline'), |
| 53 | + reply: defineIcon(Ionicons, 'chatbubble-outline'), |
| 54 | + repost: defineIcon(Ionicons, 'repeat'), |
| 55 | + search: defineIcon(Ionicons, 'search'), |
| 56 | + searchOutline: defineIcon(Ionicons, 'search-outline'), |
| 57 | + settings: defineIcon(Ionicons, 'settings-outline'), |
| 58 | + share: defineIcon(Ionicons, 'share-social-outline'), |
| 59 | + themeDark: defineIcon(Ionicons, 'moon'), |
| 60 | + themeLight: defineIcon(Ionicons, 'sunny'), |
| 61 | + time: defineIcon(Ionicons, 'time-outline'), |
| 62 | + trash: defineIcon(Ionicons, 'trash-outline'), |
| 63 | + trashBin: defineIcon(Ionicons, 'trash-bin'), |
| 64 | +}; |
| 65 | + |
| 66 | +export type IconName = keyof typeof IconsMap; |
| 67 | + |
| 68 | +/** A palette token (resolved against the theme) or any raw color string. */ |
| 69 | +type IconColor = PaletteColorToken | (string & {}); |
| 70 | + |
| 71 | +type IconProps = Omit< |
| 72 | + ComponentProps<typeof Ionicons>, |
| 73 | + 'name' | 'color' | 'size' |
| 74 | +> & { |
| 75 | + name: IconName; |
| 76 | + size?: number; |
| 77 | + /** Palette token (resolved against the active theme) or a raw color string. */ |
| 78 | + color?: IconColor; |
| 79 | +}; |
| 80 | + |
| 81 | +export default function Icon({ |
| 82 | + name, |
| 83 | + size = 16, |
| 84 | + color = 'neutral_1000', |
| 85 | + ...rest |
| 86 | +}: IconProps): ReactNode { |
| 87 | + const { theme } = useTheme(); |
| 88 | + |
| 89 | + // Palette tokens resolve against the theme; anything else is passed through as |
| 90 | + // a literal color (e.g. a resolved color injected via render-prop / cloneElement). |
| 91 | + const resolvedColor = |
| 92 | + color in theme.palette ? theme.palette[color as PaletteColorToken] : color; |
| 93 | + |
| 94 | + return IconsMap[name].render({ size, color: resolvedColor, ...rest }); |
| 95 | +} |
0 commit comments