Svgr: Disable stripping viewBox

Created on 20 Jul 2018  路  12Comments  路  Source: gregberge/svgr

I must be looking in all the wrong places; sorry if so.

How can I disable stripping the viewBox attribute from SVGs?

Relatedly, is there a way to add a viewBox if it doesn't already exist? (i.e. turn width="w" height="h" into width="w" height="h" viewBox="0 0 w h")

Most helpful comment

okay, if anyone runs into the same thing. i've managed to make it work with webpack config like so:

js { test: /(icons|images)\/.*?.svg$/, use: [{ loader: '@svgr/webpack', options: { svgoConfig: { plugins: { removeViewBox: false } } } }, 'file-loader'] }

All 12 comments

  • Removing viewBox is relative to SVGO, so please refer to it and tricks config
  • Adding viewBox is not possible, you have to write a h2x plugin to do it
// in .svgrrc.js
module.exports = {
  svgoConfig: {
    plugins: {
      removeViewBox: false
    }
  }
};

Thanks, that got me on the right lines. That didn't work for me (I may have made a typo) but I saw in the options I can do a YAML file instead. I ended up writing a file .svgo.yml with the following contents:

plugins:
  - removeViewBox: false

is there a way to specify this option with @svgr/webpack loader?

okay, if anyone runs into the same thing. i've managed to make it work with webpack config like so:

js { test: /(icons|images)\/.*?.svg$/, use: [{ loader: '@svgr/webpack', options: { svgoConfig: { plugins: { removeViewBox: false } } } }, 'file-loader'] }

Any idea if there's a way to do that in a require statement, i.e. with no
special Webpack configuration?

If you're configuring/disabling several svgo plugins, remember that they each have to be in a separate object:

svgoConfig: {
      multipass: true,
      plugins: [
        {
          cleanupIDs: {
            prefix: `svg-${componentName.toLowerCase()}-`,
          },
        },
        {
          removeViewBox: false,
        }
      ],
    },

Any idea if there's a way to do that in a require statement, i.e. with no special Webpack configuration?

Keeping the viewBox attribute really should be the default..

@vlinder you should submit a PR to SVGO.

The SVGO option in .svgrrc didn't work for me.
However, SVGR CLI has an option to preserve the viewBox attribute by passing in --icon flag.

Example

npx @svgr/cli --icon -d src/icons svg-icons --ext tsx

Thanks, that got me on the right lines. That didn't work for me (I may have made a typo) but I saw in the options I can do a YAML file instead. I ended up writing a file .svgo.yml with the following contents:

plugins:
  - removeViewBox: false

Saved my day. .svgrrc didn't work for me since in my case the issue was in the rollup bundle plugin. Adding a .svgorc config fixed it.

if use rollup.config, you can create .svgarc file
inside indicate:
plugins: {
removeViewBox: false
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VincentCATILLON picture VincentCATILLON  路  5Comments

Kosai106 picture Kosai106  路  4Comments

rosenfeld picture rosenfeld  路  6Comments

lucasjs picture lucasjs  路  3Comments

1saf picture 1saf  路  3Comments