• Stars
    star
    290
  • Rank 137,503 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

iOS/Android native vector assets generated from SVG

React Native Vector Image

iOS/Android native vector assets generated from SVG.

Tests npm

  • Faster render – ~5x faster than react-native-svg.
  • Smaller JS bundle = faster startup.
  • Native support for dark mode.

Installation

yarn add react-native-vector-image @klarna/react-native-vector-drawable

Android

Edit android/app/build.gradle to look like this (without the +):

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 +):

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

import VectorImage from 'react-native-vector-image';

const App = () => <VectorImage source={require('./image.svg')} />;

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.

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

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.

<VectorImage /> 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. Β© Joel Arvidsson 2021

svg2vd: MIT Β© 2020 Shopify

More Repositories

1

react-native-vector-icons

Customizable Icons for React Native with support for image source and full styling.
JavaScript
17,222
star
2

react-native-animatable

Standard set of easy to use animations and declarative transitions for React Native
JavaScript
9,663
star
3

react-native-progress

Progress indicators and spinners for React Native
JavaScript
3,577
star
4

react-native-keychain

πŸ”‘ Keychain Access for React Native
Java
3,046
star
5

hush

🀫 Noiseless Browsing – Content Blocker for Safari
JavaScript
3,017
star
6

react-native-lightbox

Images etc in Full Screen Lightbox Popovers for React Native
JavaScript
2,805
star
7

react-native-collapsible

Animated collapsible component for React Native, good for accordions, toggles etc
JavaScript
2,404
star
8

loki

πŸ‘ Visual Regression Testing for Storybook
JavaScript
1,729
star
9

react-native-image-progress

Progress indicator for networked images in React Native
JavaScript
1,701
star
10

angular-scroll

Scrollspy, animated scrollTo and scroll events for angular.js
JavaScript
1,485
star
11

react-native-performance

πŸ“ Monitor and measure React Native performance
TypeScript
864
star
12

react-native-store-review

Rate on App/Play Store directly in your React Native app
Java
717
star
13

react-native-shimmer

Simple shimmering effect for any view in React Native
Java
662
star
14

react-native-parallax

Parallax effects for React Native using Animated API
JavaScript
580
star
15

react-native-esbuild

Fast bundler and dev server for react-native using esbuild
JavaScript
571
star
16

react-native-pinchable

Instagram like pinch to zoom for React Native
Java
217
star
17

angular-parallax

Lightweight & performant parallax scrolling for angular.js.
JavaScript
203
star
18

angular-lazytube

Lightweight, responsive, lazy loaded YouTube videos that degrades gracefully.
JavaScript
46
star
19

diglett

Keep your JS project lean by detecting duplicate dependencies
JavaScript
44
star
20

esbuild-server

Fast, lightweight and powerful development server for esbuild
TypeScript
31
star
21

oblador.github.io

Landing page for my GitHub projects
HTML
10
star
22

oblador

5
star
23

node-cloud-imager

Powerful yet simple way to apply filters/crop/resize images and upload to cloud providers (Amazon S3, Rackspace, Azure).
JavaScript
3
star
24

node-ups-sdk

JavaScript
3
star
25

.github

1
star
26

react-native-fabric-image-unicode-issue

TypeScript
1
star