|
| 1 | +--- |
| 2 | +title: Adding Icons |
| 3 | +description: Wrap your icon components with `iconWithClassName` to add a class name to the icon. |
| 4 | +--- |
| 5 | + |
| 6 | +{/* prettier-ignore-start */} |
| 7 | +{/* prettier-ignore-end */} |
| 8 | + |
| 9 | +import { Aside, Code, Steps } from '@astrojs/starlight/components'; |
| 10 | + |
| 11 | +## Set Up |
| 12 | + |
| 13 | + <Steps> |
| 14 | + 1. Install the following packages: |
| 15 | + </Steps> |
| 16 | + |
| 17 | + ```bash |
| 18 | + npx expo install react-native-svg lucide-react-native |
| 19 | + ``` |
| 20 | + <Steps> |
| 21 | + 2. Resolve the className strings into icon styles |
| 22 | + </Steps> |
| 23 | + |
| 24 | + Add the `iconWithClassName` function in the `~/lib/icons/iconWithClassName.ts` file. |
| 25 | + |
| 26 | + ```tsx |
| 27 | + import type { LucideIcon } from 'lucide-react-native'; |
| 28 | + import { cssInterop } from 'nativewind'; |
| 29 | + |
| 30 | + export function iconWithClassName(icon: LucideIcon) { |
| 31 | + cssInterop(icon, { |
| 32 | + className: { |
| 33 | + target: 'style', |
| 34 | + nativeStyleToProp: { |
| 35 | + color: true, |
| 36 | + opacity: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + } |
| 41 | + ``` |
| 42 | + |
| 43 | + <Steps> |
| 44 | + 3. Create a file for each Icon component |
| 45 | + </Steps> |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +## Create Icon Components |
| 50 | + |
| 51 | + Wrap your icon components with `iconWithClassName` to add a class name to the icon. |
| 52 | + |
| 53 | + <Aside type='caution'> |
| 54 | + Neglecting to wrap icons with `iconWithClassName` will prevent you from being able to update the |
| 55 | + `color` or `opacity` props via the `className` prop. |
| 56 | + </Aside> |
| 57 | + |
| 58 | +### Create a file for each icon in `~/lib/icons/` |
| 59 | + |
| 60 | + Wrap your icon components with `iconWithClassName` to add a class name to the icon. |
| 61 | + |
| 62 | + **First**, create a new file in the `~/lib/icons/` directory with the name of the [LucideIcon](https://lucide.dev/icons/). |
| 63 | + |
| 64 | + |
| 65 | + **Then**, in each file: |
| 66 | + |
| 67 | + <Steps> |
| 68 | + 1. Import the Icon from LucideIcon |
| 69 | + </Steps> |
| 70 | + <Steps> |
| 71 | + 2. Import the `iconWithClassName` function |
| 72 | + </Steps> |
| 73 | + <Steps> |
| 74 | + 3. Call the `iconWithClassName` function and pass the icon as its argument |
| 75 | + </Steps> |
| 76 | + <Steps> |
| 77 | + 4. Export the Icon as a named export |
| 78 | + </Steps> |
| 79 | + |
| 80 | +### Examples |
| 81 | + |
| 82 | + **Sun** |
| 83 | + |
| 84 | + <Code title="~/lib/icons/Sun" lang="tsx" code={` |
| 85 | + import { Sun } from 'lucide-react-native'; |
| 86 | + import { iconWithClassName } from './iconWithClassName'; |
| 87 | + iconWithClassName(Sun); |
| 88 | + export { Sun };`} /> |
| 89 | + |
| 90 | + **MoonStar** |
| 91 | + |
| 92 | + <Code title="~/lib/icons/MoonStar" lang="tsx" code={` |
| 93 | + import { MoonStar } from 'lucide-react-native'; |
| 94 | + import { iconWithClassName } from './iconWithClassName'; |
| 95 | + iconWithClassName(MoonStar); |
| 96 | + export { MoonStar };`} /> |
| 97 | + |
0 commit comments