Gathering detailed insights and metrics for react-native-svg-transformer
Gathering detailed insights and metrics for react-native-svg-transformer
Gathering detailed insights and metrics for react-native-svg-transformer
Gathering detailed insights and metrics for react-native-svg-transformer
Import SVG files in your React Native project the same way that you would in a Web application.
npm install react-native-svg-transformer
Typescript
Module System
47.5
Supply Chain
47.5
Quality
70
Maintenance
50
Vulnerability
93.2
License
JavaScript (100%)
Total Downloads
53,714,771
Last Day
32,653
Last Week
316,438
Last Month
1,751,059
Last Year
19,403,985
1,619 Stars
295 Commits
116 Forks
8 Watching
3 Branches
21 Contributors
Latest Version
1.5.0
Package Id
react-native-svg-transformer@1.5.0
Unpacked Size
14.89 kB
Size
5.43 kB
File Count
10
Publised On
09 Jul 2024
Cumulative downloads
Total Downloads
Last day
-61.2%
32,653
Compared to previous day
Last week
-24.8%
316,438
Compared to previous week
Last month
-1.3%
1,751,059
Compared to previous month
Last year
40.1%
19,403,985
Compared to previous year
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.
Import your .svg
file inside a React component:
1import Logo from "./logo.svg";
You can then use your image as a component:
1<Logo width={120} height={40} />
Make sure that you have installed the react-native-svg
library:
1npm install --save-dev react-native-svg-transformer
or
1yarn add --dev react-native-svg-transformer
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
:
1const { getDefaultConfig } = require("expo/metro-config"); 2 3module.exports = (() => { 4 const config = getDefaultConfig(__dirname); 5 6 const { transformer, resolver } = config; 7 8 config.transformer = { 9 ...transformer, 10 babelTransformerPath: require.resolve("react-native-svg-transformer/expo") 11 }; 12 config.resolver = { 13 ...resolver, 14 assetExts: resolver.assetExts.filter((ext) => ext !== "svg"), 15 sourceExts: [...resolver.sourceExts, "svg"] 16 }; 17 18 return config; 19})();
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
:
1const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config"); 2 3const defaultConfig = getDefaultConfig(__dirname); 4const { assetExts, sourceExts } = defaultConfig.resolver; 5 6/** 7 * Metro configuration 8 * https://reactnative.dev/docs/metro 9 * 10 * @type {import('metro-config').MetroConfig} 11 */ 12const config = { 13 transformer: { 14 babelTransformerPath: require.resolve( 15 "react-native-svg-transformer/react-native" 16 ) 17 }, 18 resolver: { 19 assetExts: assetExts.filter((ext) => ext !== "svg"), 20 sourceExts: [...sourceExts, "svg"] 21 } 22}; 23 24module.exports = mergeConfig(defaultConfig, config);
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
:
1const { getDefaultConfig } = require("metro-config"); 2 3module.exports = (async () => { 4 const { 5 resolver: { sourceExts, assetExts } 6 } = await getDefaultConfig(); 7 return { 8 transformer: { 9 babelTransformerPath: require.resolve( 10 "react-native-svg-transformer/react-native" 11 ) 12 }, 13 resolver: { 14 assetExts: assetExts.filter((ext) => ext !== "svg"), 15 sourceExts: [...sourceExts, "svg"] 16 } 17 }; 18})();
Some React Native projects are using Expo modules without using expo-cli.
In such projects Expo's transformer is selected by default, and can be overwritten to correctly use React Native's transformer by adding react-native
to the require path:
1-require.resolve("react-native-svg-transformer") 2+require.resolve("react-native-svg-transformer/react-native")
You can also force Expo's transformer to always be used:
1-require.resolve("react-native-svg-transformer") 2+require.resolve("react-native-svg-transformer/expo")
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):
1declare module "*.svg" { 2 import React from "react"; 3 import { SvgProps } from "react-native-svg"; 4 const content: React.FC<SvgProps>; 5 export default content; 6}
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)
1{ 2 "replaceAttrValues": { 3 "red": "currentColor" 4 } 5}
Edit your .svgrrc
file and include a line for replaceAttrValues
that matching a hex code to {props.fill}
1{ 2 "replaceAttrValues": { 3 "#000": "{props.fill}" 4 } 5}
And then make sure your path tag inside the SVG file fill
attribute is the hex code (in this case #000
).
1<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg"> 2 <path d="M2.965 6.0925C4.045 8.215 ..." fill="#000"/> 3</svg>
You can then use your image as a component:
1<Logo width={120} height={40} fill={"any color"} />
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
:
1module.exports = "SvgMock";
Then, depending on where you have your Jest configuration:
package.json
:
1{ 2 "jest": { 3 "moduleNameMapper": { 4 "\\.svg": "<rootDir>/__mocks__/svgMock.js" 5 } 6 } 7}
or
jest.config.js
:
1module.exports = { 2 moduleNameMapper: { 3 "\\.svg": "<rootDir>/__mocks__/svgMock.js" 4 } 5};
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.
In addition to React Native, this transformer depends on the following libraries:
No vulnerabilities found.
Reason
9 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More@hitbit/expo-svg-transformer
Use svgr to reactify .svg files
@ornikar/react-native-svg-transformer
SVG transformer for react-native - fork of kristerkari
@kode-frontend/svg-transformer
CLI to optimize and convert SVGs to React/React Native components
expo-svg-transformer
SVG transformer for react-native - fork of kristerkari