gregberge / svgr

Transform SVGs into React components 🦁
https://react-svgr.com
MIT License
10.59k stars 422 forks source link

Preserving viewBox #18

Closed troch closed 7 years ago

troch commented 7 years ago

Hi,

First of all, thank you for this amazing library, it is great!

With version 1.0.0, viewBox is dropped. In my case I have some illustrations that I inline, they are not icons but I still need viewBox to scale them.

I would be happy to contribute and help, but in order to understand where you come from: what is the rationale behind removing viewBox for "non-icons"?

gregberge commented 7 years ago

Hi, after #17 I think adding an option « viewBox » and defaulting it to true is the best thing. I think viewBox doesn’t add a lot of data and give ability to resize, it is pretty common with SVG. @troch feel free to make a PR, else I will do it as soon as I have a moment.

edorivai commented 6 years ago

For future googlers (like me). It seems the information in this issue is outdated, viewbox settings are now delegated to the svgoConfig. See https://github.com/smooth-code/svgr/issues/142

Webpack snippet:

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

@edorivai yes you are right, viewBox is svgo job!

panayotoff commented 4 years ago

Note, that in order to work now { removeViewBox: false } should be passed at array:

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

Hi,

How to handle this in @svgr/rollup, I mean how to write this config in rollup

tomolenet commented 3 years ago

Hi,

How to handle this in @svgr/rollup, I mean how to write this config in rollup

For me this one works:

svgr({
  svgoConfig: {
    plugins: {
      removeViewBox: false
    }
  }
}),
baotlake commented 2 years ago

https://github.com/webpack-contrib/image-minimizer-webpack-plugin/issues/190

{
    plugins: [
      {
        name: 'removeViewBox',
        active: false
      },
    ]
  }
gregberge commented 2 years ago

Give a look to: https://react-svgr.com/docs/migrate/#svgo

MDSmpacini commented 8 months ago

rollup plugin tune

svgr({
          svgoConfig: {
              plugins: [
                  {
                      name: 'preset-default',
                      params: {
                          overrides: {
                              removeViewBox: false,
                          },
                      },
                  }
              ]
          }
      })
thewanionly commented 7 months ago

Note, that in order to work now { removeViewBox: false } should be passed at array:

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

New update on this one. If you encounter this error: Error: Plugin name should be specified, you need to update to the ff:

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

Source: https://stackoverflow.com/a/73089859/9608615