Skip to content

Commit 072875e

Browse files
committed
Forward ref to svg element [publish]
1 parent 1260302 commit 072875e

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.1
4+
5+
Forward ref to svg element
6+
37
## 0.3.0
48

59
Added `useInnerHTML` option to use `dangerouslySetInnerHTML` for SVG contents which improve bundle size.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-fast-react-svg",
33
"description": "Turn SVG into React components, without Babel",
4-
"version": "0.3.0",
4+
"version": "0.3.1",
55
"license": "MIT",
66
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
77
"main": "dist/index.js",

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export const svgToJSX = (svg: string, useInnerHTML?: boolean) => {
3737
.replace(/\s+/g, " ");
3838
jsx = `${updatePropsCase(
3939
svg.slice(0, index)
40-
)} {...props} dangerouslySetInnerHTML={{ __html: '${content}' }} />`;
40+
)} ref={ref} {...props} dangerouslySetInnerHTML={{ __html: '${content}' }} />`;
4141
} else {
42-
jsx = updatePropsCase(svg).replace(">", " {...props}>");
42+
jsx = updatePropsCase(svg).replace(">", " ref={ref} {...props}>");
4343
}
44-
return `import React from "react";const ReactComponent = (props) => (${jsx});export default ReactComponent;`;
44+
return `import React from "react";const ReactComponent = React.forwardRef((props, ref) => (${jsx}));export default ReactComponent;`;
4545
};
4646

4747
const updatePropsCase = (svg: string) =>

0 commit comments

Comments
 (0)