UNPKG

10.2 kBMarkdownView Raw
1# react-svg
2
3[![npm version](https://img.shields.io/npm/v/react-svg.svg?style=flat-square)](https://www.npmjs.com/package/react-svg)
4[![build status](https://img.shields.io/github/actions/workflow/status/tanem/react-svg/ci.yml?style=flat-square)](https://github.com/tanem/react-svg/actions?query=workflow%3ACI)
5[![coverage status](https://img.shields.io/codecov/c/github/tanem/react-svg.svg?style=flat-square)](https://codecov.io/gh/tanem/react-svg)
6[![npm downloads](https://img.shields.io/npm/dm/react-svg.svg?style=flat-square)](https://www.npmjs.com/package/react-svg)
7[![minzipped size](https://img.shields.io/bundlephobia/minzip/react-svg?style=flat-square)](https://bundlephobia.com/result?p=react-svg)
8
9> A React component that injects SVG into the DOM.
10
11[Background](#background) | [Basic Usage](#basic-usage) | [Live Examples](#live-examples) | [API](#api) | [Installation](#installation) | [FAQ](#faq) | [License](#license)
12
13## Background
14
15Let's say you have an SVG available at some URL, and you'd like to inject it into the DOM [for various reasons](https://github.com/tanem/svg-injector#background). This module does the heavy lifting for you by delegating the process to [@tanem/svg-injector](https://github.com/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.
16
17## Basic Usage
18
19```jsx
20import { createRoot } from 'react-dom/client'
21import { ReactSVG } from 'react-svg'
22
23const container = document.getElementById('root')
24const root = createRoot(container)
25root.render(<ReactSVG src="svg.svg" />)
26```
27
28## Live Examples
29
30- Accessibility: [Source](https://github.com/tanem/react-svg/tree/master/examples/accessibility) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/accessibility)
31- API Usage: [Source](https://github.com/tanem/react-svg/tree/master/examples/api-usage) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/api-usage)
32- Basic Usage: [Source](https://github.com/tanem/react-svg/tree/master/examples/basic-usage) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/basic-usage)
33- Before Injection: [Source](https://github.com/tanem/react-svg/tree/master/examples/before-injection) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/before-injection)
34- CSS Animation: [Source](https://github.com/tanem/react-svg/tree/master/examples/css-animation) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/css-animation)
35- CSS-in-JS: [Source](https://github.com/tanem/react-svg/tree/master/examples/css-in-js) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/css-in-js)
36- External Stylesheet: [Source](https://github.com/tanem/react-svg/tree/master/examples/external-stylesheet) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/external-stylesheet)
37- Fallbacks: [Source](https://github.com/tanem/react-svg/tree/master/examples/fallbacks) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/fallbacks)
38- Iframe: [Source](https://github.com/tanem/react-svg/tree/master/examples/iframe) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/iframe)
39- Loading: [Source](https://github.com/tanem/react-svg/tree/master/examples/loading) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/loading)
40- No Extension: [Source](https://github.com/tanem/react-svg/tree/master/examples/no-extension) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/no-extension)
41- SSR: [Source](https://github.com/tanem/react-svg/tree/master/examples/ssr) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/ssr)
42- Styled Components: [Source](https://github.com/tanem/react-svg/tree/master/examples/styled-components) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/styled-components)
43- SVG Wrapper: [Source](https://github.com/tanem/react-svg/tree/master/examples/svg-wrapper) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/svg-wrapper)
44- Typescript: [Source](https://github.com/tanem/react-svg/tree/master/examples/typescript) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/typescript)
45- UMD Build (Development): [Source](https://github.com/tanem/react-svg/tree/master/examples/umd-dev) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/umd-dev)
46- UMD Build (Production): [Source](https://github.com/tanem/react-svg/tree/master/examples/umd-prod) | [Sandbox](https://codesandbox.io/s/github/tanem/react-svg/tree/master/examples/umd-prod)
47
48## API
49
50**Props**
51
52- `src` - The SVG URL.
53- `afterInjection(svg)` - _Optional_ Function to call after the SVG is injected. `svg` is the injected SVG DOM element. If an error occurs during execution it will be routed to the `onError` callback, and if a `fallback` is specified it will be rendered. Defaults to `() => {}`.
54- `beforeInjection(svg)` - _Optional_ Function to call just before the SVG is injected. `svg` is the SVG DOM element which is about to be injected. If an error occurs during execution it will be routed to the `onError` callback, and if a `fallback` is specified it will be rendered. Defaults to `() => {}`.
55- `desc` - _Optional_ String used for SVG `<desc>` element content. If a `<desc>` exists it will be replaced, otherwise a new `<desc>` is created. Defaults to `''`, which is a noop.
56- `evalScripts` - _Optional_ Run any script blocks found in the SVG. One of `'always'`, `'once'`, or `'never'`. Defaults to `'never'`.
57- `fallback` - _Optional_ Fallback to use if an error occurs during injection, or if errors are thrown from the `beforeInjection` or `afterInjection` functions. Can be a string, class component, or function component. Defaults to `null`.
58- `httpRequestWithCredentials` - _Optional_ Boolean indicating if cross-site Access-Control requests for the SVG should be made using credentials. Defaults to `false`.
59- `loading` - _Optional_ Component to use during loading. Can be a string, class component, or function component. Defaults to `null`.
60- `onError(error)` - _Optional_ Function to call if an error occurs during injection, or if errors are thrown from the `beforeInjection` or `afterInjection` functions. `error` is an `unknown` object. Defaults to `() => {}`.
61- `renumerateIRIElements` - _Optional_ Boolean indicating if SVG IRI addressable elements should be renumerated. Defaults to `true`.
62- `title` - _Optional_ String used for SVG `<title>` element content. If a `<title>` exists it will be replaced, otherwise a new `<title>` is created. Defaults to `''`, which is a noop.
63- `useRequestCache` - _Optional_ Use SVG request cache. Defaults to `true`.
64- `wrapper` - _Optional_ Wrapper element types. One of `'div'`, `'span'` or `'svg'`. Defaults to `'div'`.
65
66Other non-documented properties are applied to the outermost wrapper element.
67
68**Example**
69
70```jsx
71<ReactSVG
72 afterInjection={(svg) => {
73 console.log(svg)
74 }}
75 beforeInjection={(svg) => {
76 svg.classList.add('svg-class-name')
77 svg.setAttribute('style', 'width: 200px')
78 }}
79 className="wrapper-class-name"
80 desc="Description"
81 evalScripts="always"
82 fallback={() => <span>Error!</span>}
83 httpRequestWithCredentials={true}
84 loading={() => <span>Loading</span>}
85 onClick={() => {
86 console.log('wrapper onClick')
87 }}
88 onError={(error) => {
89 console.error(error)
90 }}
91 renumerateIRIElements={false}
92 src="svg.svg"
93 title="Title"
94 useRequestCache={false}
95 wrapper="span"
96/>
97```
98
99## Installation
100
101> ⚠️This library depends on [@tanem/svg-injector](https://github.com/tanem/svg-injector), which uses [`Array.from()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from). If you're targeting [browsers that don't support that method](https://kangax.github.io/compat-table/es6/#test-Array_static_methods), you'll need to ensure an appropriate polyfill is included manually. See [this issue comment](https://github.com/tanem/svg-injector/issues/97#issuecomment-483365473) for further detail.
102
103```
104$ npm install react-svg
105```
106
107UMD builds are also available for use with pre-React 19 via [unpkg](https://unpkg.com/):
108
109- https://unpkg.com/react-svg/dist/react-svg.umd.development.js
110- https://unpkg.com/react-svg/dist/react-svg.umd.production.js
111
112For the non-minified development version, make sure you have already included:
113
114- [`React`](https://unpkg.com/react@18/umd/react.development.js)
115- [`ReactDOM`](https://unpkg.com/react-dom@18/umd/react-dom.development.js)
116- [`PropTypes`](https://unpkg.com/prop-types/prop-types.js)
117
118For the minified production version, make sure you have already included:
119
120- [`React`](https://unpkg.com/react@18/umd/react.production.min.js)
121- [`ReactDOM`](https://unpkg.com/react-dom@18/umd/react-dom.production.min.js)
122
123## FAQ
124
125<details>
126
127<summary>
128Why are there two wrapping elements?
129</summary>
130
131This module delegates it's core behaviour to [@tanem/svg-injector](https://github.com/tanem/svg-injector), which requires the presence of a parent node when swapping in the SVG element. The swapping in occurs outside of React flow, so we don't want React updates to conflict with the DOM nodes `@tanem/svg-injector` is managing.
132
133Example output, assuming a `div` wrapper:
134
135```html
136<div> <!-- The wrapper, managed by React -->
137 <div> <!-- The parent node, managed by @tanem/svg-injector -->
138 <svg>...</svg> <!-- The swapped-in SVG, managed by @tanem/svg-injector -->
139 </div>
140</div>
141```
142
143See:
144
145- [Integrating with Other Libraries](https://reactjs.org/docs/integrating-with-other-libraries.html).
146
147Related issues and PRs:
148
149- [#24](https://github.com/tanem/react-svg/issues/24).
150- [#30](https://github.com/tanem/react-svg/issues/30).
151- [#36](https://github.com/tanem/react-svg/pull/36).
152- [#48](https://github.com/tanem/react-svg/issues/48).
153
154</details>
155
156## License
157
158MIT