Options
SVGR ships with a handful of customizable options, usable in both the CLI and API.
Config file
Specify a custom config file.
Default | CLI Override | API Override |
---|---|---|
null | --config-file | configFile: string |
Runtime config
Disable runtime config (.svgrrc
, .svgo.yml
, .prettierrc
).
Default | CLI Override | API Override |
---|---|---|
true | --no-runtime-config | runtimeConfig: boolean |
JSX runtime
Specify a JSX runtime to use.
- "classic": adds
import * as React from 'react'
on the top of file - "automatic": do not add anything
- "classic-preact": adds
import { h } from 'preact'
on the top of file
Default | CLI Override | API Override |
---|---|---|
classic | --jsx-runtime | jsxRuntime: 'classic' | 'automatic' | 'classic-preact' |
JSX runtime import source
Specify a custom JSX runtime source to use. Allows to customize the import added at the top of generated file.
Example: jsxRuntimeImport: { source: 'preact', specifiers: ['h'] }
for "classic-preact" equivalent.
To use the default import instead of a list of names, you can use defaultSpecifier
, for example to use svgr with hyperapp-jsx-pragma
, you can specify jsxRuntimeImport: { source: 'hyperapp-jsx-pragma', defaultSpecifier: 'h' }
to get an end result of import h from "hyperapp-jsx-pragma";
Default | CLI Override | API Override |
---|---|---|
null | - | jsxRuntimeImport: { source: string, specifiers: string[], defaultSpecifier: string } |
Icon
Replace SVG width
and height
by a custom value. If value is omitted, it uses 1em
in order to make SVG size inherits from text size.
Default | CLI Override | API Override |
---|---|---|
false | --icon [value] | icon: boolean | string | number |
If you use
--icon
without any argument, be careful of separating files from arguments using--
, ex:svgr --icon -- assets/svg
Native
Modify all SVG nodes with uppercase and use a specific template with
react-native-svg
imports. All unsupported nodes will be removed.
Default | CLI Override | API Override |
---|---|---|
false | --native | native: boolean |
TypeScript
Generates .tsx
files with TypeScript typings.
Default | CLI Override | API Override |
---|---|---|
false | --typescript | typescript: boolean |
Dimensions
Keep width
and height
attributes from the root SVG tag.
Removal is guaranteed if dimensions: false
, unlike the removeDimensions: true
SVGO plugin option which also generates a viewBox
from the dimensions if no viewBox
is present.
NOTE:
- Any SVGO processing will occur prior to this removal.
- When removing dimensions, SVGO will be configured not to remove the
viewBox
if one is present. You can override this behaviour via your own config.
Default | CLI Override | API Override |
---|---|---|
true | --no-dimensions | dimensions: boolean |
Expand props
All properties given to component will be forwarded on SVG tag. Possible values: "start"
, "end"
or false
("none"
in CLI).
Default | CLI Override | API Override |
---|---|---|
end | --expand-props | expandProps: string |
Prettier
Use Prettier to format JavaScript code output.
Default | CLI Override | API Override |
---|---|---|
true | --no-prettier | prettier: boolean |
Prettier config
Specify Prettier config. See Prettier options.
Default | CLI Override | API Override |
---|---|---|
null | --prettier-config | prettierConfig: PrettierConfig |
SVGO
Use SVGO to optimize SVG code before transforming it into a component.
NOTE: SVGR implicitly enables the prefixIds
SVGO plugin which adds a prefix to values of id
and class
attributes to reduce the chance of collision issues.
Default | CLI Override | API Override |
---|---|---|
true | --no-svgo | svgo: boolean |
SVGO config
Specify SVGO config. See SVGO options.
Default | CLI Override | API Override |
---|---|---|
null | --svgo-config | svgoConfig: SVGOConfig |
Ref
Setting this to true
will forward ref to the root SVG tag.
Default | CLI Override | API Override |
---|---|---|
false | --ref | ref: boolean |
Memo
Setting this to true
will wrap the exported component in React.memo
.
Default | CLI Override | API Override |
---|---|---|
false | --memo | memo: boolean |
Replace attribute value
Replace an attribute value by an other. The main usage of this option is to change an icon color to "currentColor" in order to inherit from text color.
Default | CLI Override | API Override |
---|---|---|
[] | --replace-attr-values <old=new> | replaceAttrValues: { [key: string]: string } |
You can specify dynamic property using curly braces:
{ '#000': "{props.color}" }
or--replace-attr-values #000={props.color}
. It is particularly useful with a custom template.
SVG props
Add props to the root SVG tag.
Default | CLI Override | API Override |
---|---|---|
[] | --svg-props <name=value> | svgProps: { [key: string]: string } |
You can specify dynamic property using curly braces:
{ focusable: "{true}" }
or--svg-props focusable={true}
. It is particularly useful with a custom template.
Accessibility
SVGs by default have an implicit role="graphics-document"
. To make SVG content accessible you may want to override the default behavior which can be done via the svgProps
override.
For web:
Default | CLI Override | API Override |
---|---|---|
[] | --svg-props role=img | svgProps: { role: 'img' } |
For native:
Default | CLI Override | API Override |
---|---|---|
[] | --svg-props accessibilityRole=image | svgProps: { accessibilityRole: 'image' } |
Typically for role=img you will want to add either alt
text or aria
label/description properties to describe the image when rendering the SVG in your application.
NOTE: On the web once you add
role: 'img'
some screen-readers will automatically announce anytitle
/text
/desc
nodes within your SVG.SVGO can be configured to remove those nodes via the built-in
removeDesc
andremoveTitle
plugins if you only want to usearia
properties during consumption of the SVGs.
Title
Add title tag via title property. If titleProp is set to true and no title is provided (title={undefined}
) at render time, this will fallback to an existing title element in the svg if exists.
Default | CLI Override | API Override |
---|---|---|
false | --title-prop | titleProp: boolean |
Description
Add desc tag via desc property. If descProp is set to true and no description is provided (desc={undefined}
) at render time, this will fallback to an existing desc element in the svg if exists.
Default | CLI Override | API Override |
---|---|---|
false | --desc-prop | descProp: boolean |
Template
Specify a template file (CLI) or a template function (API) to use. For an example of template, see the default one.
Default | CLI Override | API Override |
---|---|---|
basic template | --template | template: <func> |
Output Directory
Output files into a directory.
Default | CLI Override | API Override |
---|---|---|
undefined | --out-dir <dirname> | outDir: string |
index.js template
Specify a template function (API) to change default index.js output (when --out-dir is used).
Default | CLI Override | API Override |
---|---|---|
basic template | --index-template | not available |
index.js file
Disable index.js file generation
Default | CLI Override | API Override |
---|---|---|
false | --no-index | index: boolean |
Ignore existing
When used with --out-dir
, it ignores already existing files.
Default | CLI Override | API Override |
---|---|---|
false | --ignore-existing | not available |
Filename case
Specify a custom filename case. Possible values: pascal
for PascalCase, kebab
for kebab-case, camel
for camelCase, or snake
for snake_case.
Default | CLI Override | API Override |
---|---|---|
pascal | --filename-case | filenameCase: string |
File extension
Specify a custom extension for generated files.
Default | CLI Override | API Override |
---|---|---|
"js" | --ext | ext: string |