• Stars
    star
    1,236
  • Rank 36,535 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Import SVG files in your React Native project the same way that you would in a Web application.

react-native-svg-transformer NPM version Downloads per month contributions welcome

React Native SVG transformer allows you to import SVG files in your React Native project the same way that you would in a Web application when using a library like SVGR to transform your imported SVG images into React components.

This makes it possible to use the same code for React Native and Web.

Usage

Import your .svg file inside a React component:

import Logo from "./logo.svg";

You can then use your image as a component:

<Logo width={120} height={40} />

Demo / Expo demo (iOS/Android/Web)

Installation and configuration

Step 1: Install react-native-svg library

Make sure that you have installed the react-native-svg library:

Step 2: Install react-native-svg-transformer library

yarn add --dev react-native-svg-transformer

Step 3: Configure the react native packager

For Expo SDK v41.0.0 or newer

Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).

metro.config.js:

const { getDefaultConfig } = require("expo/metro-config");

module.exports = (() => {
  const config = getDefaultConfig(__dirname);

  const { transformer, resolver } = config;

  config.transformer = {
    ...transformer,
    babelTransformerPath: require.resolve("react-native-svg-transformer"),
  };
  config.resolver = {
    ...resolver,
    assetExts: resolver.assetExts.filter((ext) => ext !== "svg"),
    sourceExts: [...resolver.sourceExts, "svg"],
  };

  return config;
})();

For React Native v0.59 or newer

Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).

metro.config.js:

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-svg-transformer")
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"]
    }
  };
})();

Using TypeScript

If you are using TypeScript, you need to add this to your declarations.d.ts file (create one if you don't have one already):

declare module "*.svg" {
  import React from 'react';
  import { SvgProps } from "react-native-svg";
  const content: React.FC<SvgProps>;
  export default content;
}

Configuring SVGR (how SVG images get transformed)

SVGR has a configuration file, which makes it possible for you to customize how SVG images get transformed to React/React Native.

Read more about the configuration options: Configuring SVGR and SVGR options.

For example, if you want to change SVG image's fill color from red to currentColor (keep in mind that this will be used for all SVG images in your app).

.svgrrc (create the file in your project's root folder if it does not exist)

{
  "replaceAttrValues": {
    "red": "currentColor"
  }
}

Changing SVG fill color in JS Code

Edit your .svgrrc file and include a line for replaceAttrValues that matching a hex code to {props.fill}

{
  "replaceAttrValues": {
    "#000": "{props.fill}"
  }
}

And then make sure your path tag inside the SVG file fill attribute is the hex code (in this case #000).

<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M2.965 6.0925C4.045 8.215 ..." fill="#000"/>
</svg>

You can then use your image as a component:

<Logo width={120} height={40} fill={"any color"} />

Usage with Jest

To use Jest to test your React Native components that import .svg images, you need to add this configuration that mocks the SVG images that are transformed to React components:

// __mocks__/svgMock.js
module.exports = "SvgMock";
module.exports.ReactComponent = "SvgMock";

Then, depending on where you have your Jest configuration:

// package.json
{
  "jest": {
    "moduleNameMapper": {
      "\\.svg": "<rootDir>/__mocks__/svgMock.js"
    }
  }
}

or

// jest.config.js
module.exports = {
  moduleNameMapper: {
    "\\.svg": "<rootDir>/__mocks__/svgMock.js"
  }
};

Rendering custom fonts in iOS

At the moment react-native-svg does not support custom font families in iOS right out of the box. A workaround is to take your .svg with custom fonts and convert it to outlines. This will replace text tags for path tags in your .svg file.

Dependencies

In addition to React Native, this transformer depends on the following libraries:

More Repositories

1

react-native-css-modules

Style React Native components using CSS, PostCSS, Sass, Less or Stylus.
290
star
2

normalize.scss

SCSS version of normalize.css
CSS
261
star
3

pinar

πŸŒ²β˜€οΈ Customizable, lightweight React Native carousel component with accessibility support.
TypeScript
260
star
4

react-native-sass-transformer

Use Sass to style your React Native apps.
JavaScript
202
star
5

awesome-bundle-size

πŸ“ An awesome list of tools and techniques to make your web bundle size smaller and your web apps load faster.
182
star
6

umatrix-recipes

"uMatrix Recipes" is a collection of rules for uMatrix that you can use to fix some popular websites by whitelisting the 3rd-party requests that are critical for the websites to function.
99
star
7

stylelint-declaration-block-no-ignored-properties

Disallow property values that are ignored due to another property value in the same rule.
JavaScript
90
star
8

css-to-react-native-transform

Turn valid CSS into React Native Stylesheet objects.
JavaScript
86
star
9

stylelint-high-performance-animation

Stylelint rule for preventing the use of low performance animation and transition properties.
JavaScript
64
star
10

stylelint-react-native

A collection of React Native specific linting rules for stylelint (in a form of a plugin).
JavaScript
56
star
11

react-native-css-transformer

Use CSS to style your React Native apps.
JavaScript
34
star
12

react-native-svg-example

A simple example app that shows how you can use SVG files in React Native
Java
31
star
13

iOSBadge

iOS style notification badges for web
CoffeeScript
28
star
14

react-native-less-transformer

Use Less to style your React Native apps.
JavaScript
27
star
15

stylelint-config-react-native-styled-components

Shareable stylelint config for styled components when using React Native
JavaScript
26
star
16

react-native-css-modules-with-typescript-example

A simple example app that shows how you can use CSS modules + Typescript with React Native and React (for browser)
Java
18
star
17

stylelint-z-index-value-constraint

Stylelint rule for setting minimum and maximum constraint value for z-index.
JavaScript
18
star
18

react-native-css-modules-with-media-queries-example

An example app to show how CSS Media Queries work in React Native.
JavaScript
18
star
19

react-native-postcss-transformer

Use PostCSS to style your React Native apps.
JavaScript
17
star
20

react-native-css-modules-example

A simple example app that shows how you can use CSS modules with React Native and React (for browser)
JavaScript
15
star
21

travis-chrome

An example repo to show how to run tests with Karma runner using Chrome browser (with Xvfb) in Travis CI
JavaScript
14
star
22

karma-bdd-using

DRY your Karma + Jasmine or Mocha tests using the data provider pattern
JavaScript
14
star
23

react-native-stylus-transformer

Use Stylus to style your React Native apps.
JavaScript
13
star
24

babel-plugin-react-native-platform-specific-extensions

Allow react-native platform specific extensions to be used for other file types than Javascript.
JavaScript
13
star
25

react-native-types-for-css-modules

React Native Typescript types with an extra added type: className property
Ruby
13
star
26

react-native-css-media-query-processor

Match style objects containing CSS Media Queries with React Native
JavaScript
12
star
27

HTML-newsletter-builder

A command line tool to help build and preprocess email newsletters
JavaScript
12
star
28

babel-plugin-react-native-classname-to-dynamic-style

Transform JSX className property to a style property that gets calculated at runtime in React Native. The plugin is used to match style objects containing parsed CSS media queries and CSS viewport units with React Native.
JavaScript
12
star
29

jQuery-Geolocator

A Geolocation API plugin for jQuery or Zepto
JavaScript
12
star
30

babel-plugin-react-native-classname-to-style

Transform JSX className property to style property in react-native.
JavaScript
10
star
31

observable-redux-json-api

Observables + Redux + JSON API
TypeScript
8
star
32

css-calc-transform

Tiny Javascript library to transform CSS properties with calc() function values to pixels based on window and element dimensions.
JavaScript
8
star
33

react-native-typed-sass-transformer

Sass transformer with Typescript support for React Native
JavaScript
7
star
34

babel-plugin-react-native-stylename-to-style

Transform styleName property to style property in react-native.
JavaScript
7
star
35

react-native-dynamic-style-processor

React Native dynamic style processor combines multiple runtime style transformations together.
JavaScript
6
star
36

placekeeper

Placekeeper is a HTML5 placeholder attribute polyfill for old browsers that lack support for it.
JavaScript
6
star
37

react-native-svg-expo-example

JavaScript
6
star
38

stylelint-config-react-native-css-modules

Shareable stylelint config for React Native CSS modules
JavaScript
5
star
39

bdd-using

DRY your Jasmine or Mocha tests using the data provider pattern
JavaScript
5
star
40

react-native-css-modules-stylename-example

A simple example app that shows how you can use CSS modules + styleName syntax with React Native and React (for browser)
JavaScript
4
star
41

observable-redux-json-api-typescript-example

A minimal example to show how to use observable-redux-json-api library using React, Typescript and Express.js.
JavaScript
3
star
42

react-native-orientation-change-provider

Simple React Native Provider component that forces a re-render when orientation changes.
JavaScript
3
star
43

css-viewport-units-transform

Transform CSS viewport units of a Javascript style object to pixels based on window dimensions
JavaScript
3
star
44

react-native-known-styling-properties

List of React Native specific styling properties.
JavaScript
3
star
45

react-native-typed-less-transformer

Less transformer with Typescript support for React Native
JavaScript
3
star
46

react-native-typed-postcss-transformer

PostCSS transformer with Typescript support for React Native
JavaScript
2
star
47

css-mod-stylus-setup

Objective-C
2
star
48

react-native-typed-css-transformer

CSS transformer with Typescript support for React Native
JavaScript
2
star
49

css-calc-proof-of-concept

An app for doing a proof of concept for using CSS calc() function in React Native
Java
2
star
50

react-native-0.59-svg-example

An example of using react-native-svg-transformer in React Native 0.59.x
Objective-C
2
star
51

rn-svg-rendering-issues

Objective-C
1
star
52

react-native-ios-13-view-or-image-inside-text-bug

Objective-C
1
star
53

react-native-text-transform-issue

A demo app to reproduce a bug with textTransform on Android
Objective-C
1
star
54

react-native-css-modules-with-viewport-units-example

An example app to show how CSS Viewport Units work in React Native.
Objective-C
1
star
55

adventures-in-webkit-land

JavaScript
1
star
56

react-native-typed-stylus-transformer

Stylus transformer with Typescript support for React Native
JavaScript
1
star
57

Test-iOS-Travis-CI

Test iOS Travis CI
Objective-C
1
star
58

package-updater

JavaScript
1
star
59

rn-svg-crash

Objective-C
1
star
60

pinar-example-app

TypeScript
1
star
61

expo-scss-test

Objective-C
1
star
62

react-native-css-modules-dss-example

Example app to show how to use DSS (https://dss-lang.com) and React Native CSS modules together
JavaScript
1
star
63

react-native-svg-debug-0.59.5

Objective-C
1
star
64

helsinkijs-may-2013-talk

JavaScript
1
star
65

react-native-geolocation-service

React native geolocation service for iOS and android
Java
1
star
66

observable-redux-json-api-example

A minimal example to show how to use observable-redux-json-api library using React and Express.js.
JavaScript
1
star