https://github.com/oblador/react-native-vector-image
iOS/Android native vector assets generated from SVG
https://github.com/oblador/react-native-vector-image
react-native react-native-component svg vector-drawable
Last synced: 6 months ago
JSON representation
iOS/Android native vector assets generated from SVG
- Host: GitHub
- URL: https://github.com/oblador/react-native-vector-image
- Owner: oblador
- License: mit
- Created: 2021-04-18T17:06:42.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-20T18:20:17.000Z (about 1 year ago)
- Last Synced: 2025-05-14T11:51:43.658Z (7 months ago)
- Topics: react-native, react-native-component, svg, vector-drawable
- Language: JavaScript
- Homepage:
- Size: 522 KB
- Stars: 333
- Watchers: 7
- Forks: 14
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
React Native Vector Image
iOS/Android native vector assets generated from SVG.
[](https://github.com/oblador/react-native-vector-image/actions/workflows/tests.yml) [](https://npmjs.com/package/react-native-vector-image)
- Faster render – ~5x faster than `react-native-svg`.
- Smaller JS bundle = faster startup.
- Native support for dark mode.
## Installation
```sh
yarn add react-native-vector-image @klarna/react-native-vector-drawable
```
For expo, see [`@zamplyy/react-native-vector-image-plugin`](https://github.com/zamplyy/react-native-vector-image-plugin).
### Android
Edit `android/app/build.gradle` to look like this (without the +):
```diff
project.ext.react = [
enableHermes: false, // clean and rebuild if changing
]
apply from: "../../node_modules/react-native/react.gradle"
+ apply from: "../../node_modules/react-native-vector-image/strip_svgs.gradle"
```
### iOS
Open your project in Xcode, select the _Build Phases_ tab, and edit the `Bundle React Native code and images` script to look like this (without the +):
```diff
set -e
export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh
+ ../node_modules/react-native-vector-image/strip_svgs.sh
```

## Usage
Since native vector assets cannot be served over http via metro dev server, they must be generated and compiled into the app bundle.
### Step 1: import an .svg file
```js
import VectorImage from 'react-native-vector-image';
const App = () => ;
```
To add dark mode to your image, create a new file with an `.dark.svg` extension, ie `image.svg` = light and `image.dark.svg` = dark.
### Step 2: generate native assets
This takes a while as metro has to go through all the code to find the imported SVGs.
```sh
yarn react-native-vector-image generate
```
| Argument | Description | Default |
| ---------------------- | ---------------------------------------------------------------- | ----------------------------- |
| `--entry-file` | Path to the app entrypoint file. | `index.js` |
| `--config` | Path to the metro config file. | `metro.config.js` |
| `--reset-cache` | Reset metro cache before extracting SVG assets. | `false` |
| `--ios-output` | Path to an iOS `.xcassets` folder. | `ios/AppName/Images.xcassets` |
| `--no-ios-output` | Disable iOS output. | `false` |
| `--android-output` | Path to an Android `res` folder. | `android/app/src/main/res` |
| `--no-android-output` | Disable Android output. | `false` |
| `--current-color` | Replace any `currentColor` color references in SVGs. | `#000000` |
| `--current-color-dark` | Replace any `currentColor` color references in `.dark.svg` SVGs. | `#ffffff` |
### Step 3: recompile
```sh
yarn react-native run-ios
# or
yarn react-native run-android
```
## Troubleshooting
### `generate` command outputs "Error while parsing image.svg"
Some optimizations applied by SVGO are not compatible with the SVG parser on Android. Try to re-export the SVG without optimizing it.
### `` warns "Could not find image"
It means that the native vector asset does not exist or is out of sync with the SVG. Simply generate the files and recompile the app.
### the `generate` command does not generate any new assets
Make sure your image component is used (imported) somewhere in your code, otherwise the asset generator won't find it.
## License
[MIT License](http://opensource.org/licenses/mit-license.html). © Joel Arvidsson 2021
[`svg2vd`](https://github.com/Shopify/svg2vd): MIT © 2020 Shopify