svgr icon indicating copy to clipboard operation
svgr copied to clipboard

Online transformer keeps `className` and removes `viewBox` for React Native components

Open LoyalPotato opened this issue 2 years ago • 7 comments

🐛 Bug Report

When converting svgs to React Native components, the prop className is kept and viewBox is removed.

To Reproduce

Steps to reproduce the behavior:

  • Convert svg with a class prop to React Native type

Expected behavior

Remove the className and keep viewBox.

Screenshot

image

LoyalPotato avatar Aug 18 '22 15:08 LoyalPotato

viewBox can be saved with

svgr({
      svgoConfig: {
        plugins: [
          { name: 'removeViewBox', active: false },
        ],
      },
    })

headfire94 avatar Sep 03 '22 06:09 headfire94

class can be removed with

  {
            name: 'removeAttrs',
            params: { attrs: 'class' },
          },

headfire94 avatar Sep 03 '22 07:09 headfire94

@headfire94 But shouldn't it keep those values when transforming by default? It keeps the width, fill and even xmlns but not class and viewbox?

LoyalPotato avatar Sep 05 '22 16:09 LoyalPotato

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 13 '22 00:11 stale[bot]

Can we add this to the documentation as well as how to trigger this via the CLI? this caught us by surprise and wondering why our SVGs wouldn't scale.

uhrohraggy avatar Jan 06 '23 17:01 uhrohraggy

For any that come across this, the viewbox gets removed as part of SVGO (https://github.com/svg/svgo). You can disable SVGO via --no-svgo option i.e. "icons": "npx @svgr/cli --jsx-runtime automatic --typescript --out-dir app/components/icons --no-svgo -- app/icons",

If you still want to opt into SVGO but not have it remove viewbox, you can pass your JSON in via --svgo-config directly as JSON or via a file ref. I preferred setting up svgo.config.js at the root

//svgo.config.js
module.exports = {
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          removeViewBox: false,
        },
      },
    },
  ],
};

Huge thanks @headfire94 for your comment, amazing help!

uhrohraggy avatar Jan 06 '23 18:01 uhrohraggy

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 21 '23 19:05 stale[bot]