-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (24 loc) · 833 Bytes
/
index.js
File metadata and controls
30 lines (24 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import React from "react";
export default class SVG extends React.PureComponent {
render() {
if (!this.props.src) throw new Error("Required prop 'src' missing");
// create a svg node
const div = document.createElement("div");
div.innerHTML = this.props.src;
const svg = div.firstChild;
// create attributes, set innerHTML
const attrs = {dangerouslySetInnerHTML: {__html: svg.innerHTML}};
// copy attributes from the svg source
if (svg.hasAttributes()) {
[].slice.call(svg.attributes).forEach(attr => {
attrs[attr.name] = attr.value;
});
}
// copy attributes from the react component
Object.keys(this.props).forEach(prop => {
if (prop === "src") return;
attrs[prop] = this.props[prop];
});
return React.createElement("svg", attrs);
}
}