jpkleemans / vite-svg-loader

Vite plugin to load SVG files as Vue components
MIT License
554 stars 58 forks source link

Component import doesn't preserve `viewBox` attribute #112

Closed ExistentialAlex closed 11 months ago

ExistentialAlex commented 1 year ago

I have found through the use of this that using a component import doesn't preserve the viewBox attribute defined on the SVG.

The output in the DOM ends up being correct and has my custom classes on top, but the viewBox is not there.

I can set the viewBox attribute manually in the same way as the class attribute but it would be better to preserve the attribute instead as it would provide more flexibility with the components.

blesstosam commented 1 year ago

i found when viewBox width & height is same as width & height attribute , it will disppear! like below:

<svg viewBox="0 0 32 32" width="32" height="32">
Pryscy commented 11 months ago

I have the same problem, viewbox attribute disappears after the import.

leoolivier commented 11 months ago

This problem comes from the default configuration of svgo, which vite-svg-loader depends on. You can pass options to svgLoader() to disable the viewBox attribute removal.

export default defineConfig({
  plugins: [
    vue(),
    svgLoader({
      svgoConfig: {
        multipass: true,
        plugins: [
          {
            name: 'preset-default',
            params: {
              overrides: {
                // viewBox is required to resize SVGs with CSS.
                // @see https://github.com/svg/svgo/issues/1128
                removeViewBox: false,
              },
            },
          },
        ],
      },
    }),
  ],
});

svgo issues #1128

ExistentialAlex commented 11 months ago

Amazing! Thank you so much. I should have looked at the config first. 😄