All Projects → tanem → React Svg

tanem / React Svg

Licence: mit
🎨 A React component that injects SVG into the DOM.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to React Svg

Onthefly
🔗 Generate TinySVG, HTML and CSS on the fly
Stars: ✭ 37 (-93.1%)
Mutual labels:  svg, dom
Svgdom
Straightforward DOM implementation to make SVG.js run headless on Node.js
Stars: ✭ 154 (-71.27%)
Mutual labels:  svg, dom
Elementx
⚡️ Functionally create DOM elements and compose them to a tree quickly
Stars: ✭ 62 (-88.43%)
Mutual labels:  svg, dom
Html To Image
✂️ Generates an image from a DOM node using HTML5 canvas and SVG.
Stars: ✭ 595 (+11.01%)
Mutual labels:  svg, dom
Mega Doodles Pack
🔥 Big vector pack with hand-drawn doodles for presentations, social media, blog posts and so on
Stars: ✭ 258 (-51.87%)
Mutual labels:  svg, images
Dompurify
DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:
Stars: ✭ 8,177 (+1425.56%)
Mutual labels:  svg, dom
Elemental2
Type checked access to browser APIs for Java code.
Stars: ✭ 115 (-78.54%)
Mutual labels:  svg, dom
Svg
Fork of the ms svg library (http://svg.codeplex.com/)
Stars: ✭ 676 (+26.12%)
Mutual labels:  svg, dom
Lunasvg
A standalone c++ library to create, animate, manipulate and render SVG files.
Stars: ✭ 243 (-54.66%)
Mutual labels:  svg, dom
Vectorlogozone
3,000+ gorgeous SVG logos, perfect for your README or credits page
Stars: ✭ 239 (-55.41%)
Mutual labels:  svg, images
Cosha
Colorful shadows for your images. 🎨
Stars: ✭ 988 (+84.33%)
Mutual labels:  images, dom
Costumekit
Base types for theming an app.
Stars: ✭ 300 (-44.03%)
Mutual labels:  svg, images
Dom To Svg
Library to convert a given HTML DOM node into an accessible SVG "screenshot".
Stars: ✭ 67 (-87.5%)
Mutual labels:  svg, dom
Svg Credit Card Payment Icons
SVG Credit Card & Payment Icons: 6 Styles, 80 Icons
Stars: ✭ 227 (-57.65%)
Mutual labels:  svg, images
Sqip
"SQIP" (pronounced \skwɪb\ like the non-magical folk of magical descent) is a SVG-based LQIP technique.
Stars: ✭ 3,074 (+473.51%)
Mutual labels:  svg, images
Plain Draggable
The simple and high performance library to allow HTML/SVG element to be dragged.
Stars: ✭ 362 (-32.46%)
Mutual labels:  svg, dom
Miaou
A chat server with OAuth2 authentication, persistent and searchable history, video and audio, markdown formatting, private and public rooms, stars, votes, embedded games, and many other features
Stars: ✭ 486 (-9.33%)
Mutual labels:  svg
Inline svg
Embed SVG documents in your Rails views and style them with CSS
Stars: ✭ 510 (-4.85%)
Mutual labels:  svg
Nanogallery2
a modern photo / video gallery and lightbox [JS library]
Stars: ✭ 488 (-8.96%)
Mutual labels:  images
Datav
Vue数据可视化组件库(类似阿里DataV,大屏数据展示),提供SVG的边框及装饰、图表、水位图、飞线图等组件,简单易用,长期更新(React版已发布)
Stars: ✭ 5,916 (+1003.73%)
Mutual labels:  svg

react-svg

npm version build status coverage status npm downloads minzipped size

A React component that injects SVG into the DOM.

Background | Basic Usage | Live Examples | API | Installation | FAQ | License

Background

Let's say you have an SVG available at some URL, and you'd like to inject it into the DOM for various reasons. This module does the heavy lifting for you by delegating the process to @tanem/svg-injector, which makes an AJAX request for the SVG and then swaps in the SVG markup inline. The async loaded SVG is also cached, so multiple uses of an SVG only require a single server request.

Basic Usage

import React from 'react'
import { render } from 'react-dom'
import { ReactSVG } from 'react-svg'

render(<ReactSVG src="svg.svg" />, document.getElementById('root'))

Live Examples

API

Props

  • src - The SVG URL.
  • afterInjection(err, svg) - Optional Function to call after the SVG is injected. If an injection error occurs, err is an Error object. Otherwise, err is null and svg is the injected SVG DOM element. Defaults to () => {}.
  • beforeInjection(svg) - Optional Function to call just before the SVG is injected. svg is the SVG DOM element which is about to be injected. Defaults to () => {}.
  • evalScripts - Optional Run any script blocks found in the SVG. One of 'always', 'once', or 'never'. Defaults to 'never'.
  • fallback - Optional Fallback to use if an injection error occurs. Can be a string, class component, or function component. Defaults to null.
  • loading - Optional Component to use during loading. Can be a string, class component, or function component. Defaults to null.
  • renumerateIRIElements - Optional Boolean indicating if SVG IRI addressable elements should be renumerated. Defaults to true.
  • useRequestCache - Optional Use SVG request cache. Defaults to true.
  • wrapper - Optional Wrapper element types. One of 'div', 'span' or 'svg'. Defaults to 'div'.

Other non-documented properties are applied to the outermost wrapper element.

Example

<ReactSVG
  afterInjection={(error, svg) => {
    if (error) {
      console.error(error)
      return
    }
    console.log(svg)
  }}
  beforeInjection={(svg) => {
    svg.classList.add('svg-class-name')
    svg.setAttribute('style', 'width: 200px')
  }}
  className="wrapper-class-name"
  evalScripts="always"
  fallback={() => <span>Error!</span>}
  loading={() => <span>Loading</span>}
  onClick={() => {
    console.log('wrapper onClick')
  }}
  renumerateIRIElements={false}
  src="svg.svg"
  useRequestCache={false}
  wrapper="span"
/>

Installation

⚠️This library depends on @tanem/svg-injector, which uses Array.from(). If you're targeting browsers that don't support that method, you'll need to ensure an appropriate polyfill is included manually. See this issue comment for further detail.

$ npm install react-svg

There are also UMD builds available via unpkg:

For the non-minified development version, make sure you have already included:

For the minified production version, make sure you have already included:

FAQ

How can I improve the accessibility of the rendered output?

Let's assume we want to add role and aria-label attributes to the outermost wrapper element, plus title and desc elements to the SVG.

Since non-documented properties are applied to the outermost wrapper element, and the beforeInjection function allows us to modify the SVG DOM, we can do something like the following:

<ReactSVG
  aria-label="Description of the overall image"
  beforeInjection={(svg) => {
    const desc = document.createElementNS(
      'http://www.w3.org/2000/svg',
      'desc'
    )
    desc.innerHTML = 'A description'
    svg.prepend(desc)

    const title = document.createElementNS(
      'http://www.w3.org/2000/svg',
      'title'
    )
    title.innerHTML = 'A title'
    svg.prepend(title)
  }}
  role="img"
  src="svg.svg"
/>

A live example is available here.

Related issue:

License

MIT

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].