Skip to content

Commit

Permalink
Forward ref to svg element [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Oct 22, 2022
1 parent 1260302 commit 072875e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 0.3.1

Forward ref to svg element

## 0.3.0

Added `useInnerHTML` option to use `dangerouslySetInnerHTML` for SVG contents which improve bundle size.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "vite-plugin-fast-react-svg",
"description": "Turn SVG into React components, without Babel",
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
"main": "dist/index.js",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Expand Up @@ -37,11 +37,11 @@ export const svgToJSX = (svg: string, useInnerHTML?: boolean) => {
.replace(/\s+/g, " ");
jsx = `${updatePropsCase(
svg.slice(0, index)
)} {...props} dangerouslySetInnerHTML={{ __html: '${content}' }} />`;
)} ref={ref} {...props} dangerouslySetInnerHTML={{ __html: '${content}' }} />`;
} else {
jsx = updatePropsCase(svg).replace(">", " {...props}>");
jsx = updatePropsCase(svg).replace(">", " ref={ref} {...props}>");
}
return `import React from "react";const ReactComponent = (props) => (${jsx});export default ReactComponent;`;
return `import React from "react";const ReactComponent = React.forwardRef((props, ref) => (${jsx}));export default ReactComponent;`;
};

const updatePropsCase = (svg: string) =>
Expand Down

0 comments on commit 072875e

Please sign in to comment.