All Projects → aeirola → React Native Svg Asset Plugin

aeirola / React Native Svg Asset Plugin

Licence: mit
Asset plugin for importing SVG images in React Native

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Svg Asset Plugin

Blade Zondicons
A package to easily make use of Zondicons in your Laravel Blade views.
Stars: ✭ 40 (-27.27%)
Mutual labels:  svg
Vue Svg Map
A set of Vue.js components to display an interactive SVG map
Stars: ✭ 48 (-12.73%)
Mutual labels:  svg
Squircle
Make all the squircles you need, in the browser. https://squircley.app/
Stars: ✭ 50 (-9.09%)
Mutual labels:  svg
Perfect Freehand
Draw perfect pressure-sensitive freehand strokes.
Stars: ✭ 999 (+1716.36%)
Mutual labels:  svg
Mobius
Scripts to extract data from the COVID-19 Google Community Mobility Reports
Stars: ✭ 47 (-14.55%)
Mutual labels:  svg
Imagine Svg
Contao Imagine SVG Library
Stars: ✭ 48 (-12.73%)
Mutual labels:  svg
Panzoom
Universal pan and zoom library (DOM, SVG, Custom)
Stars: ✭ 1,003 (+1723.64%)
Mutual labels:  svg
Androidsvgloader
Android library for load svg from internet to imageview
Stars: ✭ 52 (-5.45%)
Mutual labels:  svg
Yii2 Widget Rating
A Yii2 widget for the simple yet powerful bootstrap-star-rating plugin with fractional rating support (sub repo split from yii2-widgets)
Stars: ✭ 47 (-14.55%)
Mutual labels:  svg
Textusm
Online tool for Generate a User Story Map from indented text.
Stars: ✭ 49 (-10.91%)
Mutual labels:  svg
Readme Pagespeed Insights
Google lighthouse stats of your website that you can put in readme
Stars: ✭ 45 (-18.18%)
Mutual labels:  svg
Svg Radar Chart
Generate SVG radar charts.
Stars: ✭ 45 (-18.18%)
Mutual labels:  svg
Csswand
🎨✨ Hover your wand and use your magic spell to copy beautiful css
Stars: ✭ 1,046 (+1801.82%)
Mutual labels:  svg
Resonance
◾️Resonance | 5kb React animation library
Stars: ✭ 1,011 (+1738.18%)
Mutual labels:  svg
Pure Vue Chart
Simple and lightweight vue chart component without using chart library dependencies
Stars: ✭ 50 (-9.09%)
Mutual labels:  svg
Savesvgaspng
Save SVGs as PNGs from the browser.
Stars: ✭ 1,004 (+1725.45%)
Mutual labels:  svg
React Icomoon
It allows you to simply view the icons in the selection.json file provided by Icomoon.
Stars: ✭ 48 (-12.73%)
Mutual labels:  svg
React Native Svg Image
Load SVG images from network; Does not work with local svgs
Stars: ✭ 53 (-3.64%)
Mutual labels:  svg
Svg Snippets
🔰 A set of custom SVG snippets for Sublime Text 2/3
Stars: ✭ 50 (-9.09%)
Mutual labels:  svg
Badgen.net
Fast badge service
Stars: ✭ 1,045 (+1800%)
Mutual labels:  svg

react-native-svg-asset-plugin

Build Status

Asset plugin for React Native which enables using SVGs with Image components. Works by generating PNGs during compile time, and passing them to the metro transformer.

📱 If you also want to use SVG images for your application launcher icons, you might want to check out react-native-svg-app-icon.

Installation

npm

npm install --save-dev react-native-svg-asset-plugin

No dependencies outside of NPM. Uses sharp for SVG rasterization.

Requires React Native version 0.57 or later to work. Expo not supported, instead you might want to use react-native-svg-transformer.

metro

Add 'react-native-svg-asset-plugin' to the list of assetPlugins in your metro.config.js file under the transformer section.

For example;

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
    assetPlugins: ['react-native-svg-asset-plugin'],
  },
};

Usage

Just require your SVG files directly into React Native Image and ImageBackground components. For example:

<Image source={require('./assets/image.svg')} />

Scaled PNGs will be generated under the subdirectory .png-cache alongside the SVG files, so you might want to add a .gitignore entry to exclude the cache directory from your code repo.

Configuration

You can configure the plugin behaviour through the optional svgAssetPlugin field in your metro.config.js file under the transformer section.

For example;

module.exports = {
  transformer: {
    // ...
    assetPlugins: ['react-native-svg-asset-plugin'],
    svgAssetPlugin: {
      pngCacheDir: '.png-cache',
      scales: [1, 2, 3],
      output: {
        compressionLevel: 9,
      },
    },
  },
};

Where the possible configuration values are:

Field Type Default Description
cacheDir string '.png-cache' Name of directory to store cached PNGs.
scales number[] [1, 2, 3] PNG image scales to generate for different screen densities.
output object {} Sharp PNG output options.
ignoreRegex RegExp null Regex that will be matched against the source file's full path, if there's a match the file will be ignored.

You will need to reset the bundler cache with react-native start --reset-cache for configuration changes to take effect for already generated images.

Comparison with react-native-svg

Most alternative ways of displaying SVG content in React Native apps are based on the react-native-svg library, which provides runtime rendering of SVG images. react-native-svg-asset-plugin works differently by rasterizing the vector images to PNGs during compile time, and using the native image rendering APIs.

react-native-svg react-native-svg-asset-plugin
Rasterization Runtime Compile time
Runtime dependencies JS + Native None
Image compatibility No Yes
Remote assets Yes No
App size Smaller Larger

Technical details

The plugin works by intercepting loaded SVG assets, and transforming them to PNGs before they are loaded by the metro transformer. After being loaded by the transformer, they work as any other PNG file in React Native, meaning you can use and style them freely in Image components.

Each SVG file produces three PNG files in 1x, 2x and 3x scales. The size of the PNG images are defined by the width and height attributes of the SVG images.

SVGs are rasterized to PNGs using the sharp Node.js library, which is based on libvips C library, which includes the librsvg library that renders the SVG images.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].