Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/goopscoop/svg-to-react-cli

A command line utility that takes a svg image file and outputs a fully formatted stateless functional React component file that can be used to render your image.
https://github.com/goopscoop/svg-to-react-cli

Last synced: about 2 months ago
JSON representation

A command line utility that takes a svg image file and outputs a fully formatted stateless functional React component file that can be used to render your image.

Awesome Lists containing this project

README

        

# svg-to-react-cli
A command line utility that takes a svg image file and outputs a fully formatted stateless functional React component with `height` and `width` for props. With flags to toggle formatting and remove style attributes.

## To Use
`npm install -g svg-to-react-cli`

### One File

`svgtoreact `

**NOTE**: image file must be in current working directory. Do not add the extention. If file is `image.svg`, then just enter `image` as the first argument. ComponentName will be the name of the sfc and filename with `.js` appended.

### Multi File

`svgtoreact dir`

or for all files in directory (will name all components in CamelCase based on image name. If image is `image.svg` then new component will be `Image` and file will be `Image.js`):

## Flags

Or use flags: `svgtoreact --output ./components/svgComponents/ --no-format --rm-style`

**Optional Flags:**

`-o, --output ` - the output path. Do not include the filename.

`--no-format` - will skip line breaks and indentation to svg. If your svg is already formatted, use this flag.

`--rm-style` - removes all style attributes from svg tags.

`--help` - Prints out this readme.

`--example` - Prints an example of the i/o of this util.

## Example

Takes this:
```html

```
And creates a new file with this:

```javascript
import React from 'react';

export default function NewThing(props) {
return (















);
}
```

## Width / height order of precedence
The order of precedence of how width/height is set on a generated component is as follows:

- 1 - Passed in width and height props are always priority one. This gives run time control to the container.
- 2 - If a svg has its width/height set, this will be the first fallback if width/height props are not provided.
- 3 - If the svg has not provided width/height attributes, the svg's viewbox's width/height segments are used as the fallback if width/height props are not provided.
- 4 - Finally, if the svg has no width/height attributes, and the svg also doesn't have a viewbox - a fallback of 50px is applied to the width and height