Using SVGR in the command line
To use SVGR from the command line, install the CLI tool:
npm install @svgr/CLI
Then, we call SVGR in the command-line, passing in the .svg file and other flags to replace different attributes with our own values.
svgr --icon --replace-attr-values "#063855=currentColor" logo.svg
The above command will take our logo.svg
file and convert it to a React component.
We can then copy the output shown in our terminal and create a new Logo.jsx
component.
Publish converted components to Bit.dev
Now that we have our logo.svg
file as a React component Logo.jsx
, we can publish it to our collection on Bit. This way, we’ll make it available for our team to use in other projects.
Let’s install Bit globally on our machine:
$ npm install bit-bin --global
We’ll now initialize a Bit workspace in our project’s root directory:
$ cd svg-icons-project$ bit init
We can add multiple components at once, but for this small demo, we’ll specifically pick our Logo component:
$ bit add src/Logo.jsx
Now, since it is a React component, we’ll need to configure a compiler for it. > Otherwise, it would be coupled to our project’s build setup, and we don’t want that. We want our published components to move freely from one project to another.
The compiler is another component published on Bit. The --compiler
flag is our way of mentioning that it should be used as a compiler.
$ bit import bit.envs/compilers/react --compiler
We’ll now tag our component with a version number:
$ bit add --all 1.0.0
Login to our account (via our terminal):
$ bit login
Finally, publish (‘export’) our component to the collection we’ve created on Bit.dev:
$ bit export chidume.svg-components
I’ve published another two converted components. Feel free to visit my component collection.
My SVGs converted to React components and published to Bit.dev.