Socket
Socket
Sign inDemoInstall

react-simple-svg

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-simple-svg

Inline SVGs in React made easy


Version published
Weekly downloads
16
decreased by-23.81%
Maintainers
1
Weekly downloads
 
Created
Source

react-simple-svg

Inline SVGs in React made easy

Why another SVG component library for React?

  • Supports three types of SVG sources:

    • import statements
    • SVG strings
    • URLs
  • Makes it easy to change strokes and fills without CSS

  • Accessibility support

Usage

Install the package using NPM:

npm install react-simple-svg

Add the component to your React application in one of three ways:

1. Using import

If you are using Webpack, install svg-inline-loader from NPM. For other module bundlers, you need an equivalent tool that converts imported SVG files to strings.

npm install -D svg-inline-loader

Extend your Webpack config:

module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        loader: 'svg-inline-loader'
      }
    ]
  }
};

Finally, add the SVG file to your React component:

import SimpleSvg from 'react-simple-svg';
import icon from './assets/icon.svg';

// Somewhere in your code:
<SimpleSvg
  src={icon}
  height={100}
  width={100}
/>

2. Using an SVG string

import SimpleSvg from 'react-simple-svg';

const icon = '<svg xmlns="..." viewBox="...">...</svg>';

// Somewhere in your code:
<SimpleSvg
  src={icon}
  height={100}
  width={100}
/>

3. Using a URL

import SimpleSvg from 'react-simple-svg';

// File from a public folder:
const iconUrl = '../public/icon.svg#someId'; // ID is required!

// Somewhere in your code:
<SimpleSvg
  src={iconUrl}
  height={100}
  width={100}
/>

Important: Internally, react-simple-svg uses SVG <use> elements for URL sources. Please make sure of the following:

  • SVGs must contain an id attribute (e.g. <svg id="someId">...</svg>), which must then be used to reference the target element in the URL (see code above)
  • SVGs are not allowed to contain stroke/fill attributes if you want to override them using <SimpleSvg> props (they always have precedence, so the props will not work)

Props

PropTypeDefaultDescription
src (required)StringSVG string or URL
height (required)NumberSVG height
width (required)NumberSVG width
classNameString""Additional class names for the <svg> tag
titleStringContent for the SVG <title> tag (recommended for accessibility)
descriptionStringContent for the SVG <desc> tag (recommended for accessibility)
roleString"img"Role of the SVG element (e.g. "img, "presentation")
fillStringSVG fill color
fillOpacityNumberSVG fill opacity
strokeNumberSVG stroke color
strokeOpacityNumberSVG stroke opacity
strokeWidthNumberSVG stroke width
svgStyleNumber{}Additional styles to apply to the <svg> tag

Development

  • git clone
  • yarn install
  • yarn start to generate the library bundle using Rollup
  • Open localhost:3000 to see the component in action using Storybook

Credits

Example SVG By Fuzzypeg (Wikimedia Commons)

Keywords

FAQs

Package last updated on 29 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc