question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Appends `prefix__` to `className` while converting to React

See original GitHub issue

I tried converting:

<svg class="w-32 h-32 text-blue-500" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg">
    <defs>
      <linearGradient x1="50%" y1="92.034%" x2="50%" y2="7.2%" id="a">
        <stop offset="0%" stop-color="currentColor" />
        <stop stop-opacity="0" offset="100%" stop-color="white" />
      </linearGradient>
    </defs>
    <circle stroke="currentColor" fill="url(#a)" cx="8.5" cy="8.5" r="6" fill-rule="evenodd" fill-opacity=".8" />
</svg>

It gave me:

import * as React from "react"

function SvgComponent() {
  return (
    <svg
      className="prefix__w-32 prefix__h-32 prefix__text-blue-500"
      viewBox="0 0 17 17"
      xmlns="http://www.w3.org/2000/svg"
    >
      <defs>
        <linearGradient x1="50%" y1="92.034%" x2="50%" y2="7.2%" id="prefix__a">
          <stop offset="0%" stopColor="currentColor" />
          <stop stopOpacity={0} offset="100%" stopColor="#fff" />
        </linearGradient>
      </defs>
      <circle
        stroke="currentColor"
        fill="url(#prefix__a)"
        cx={8.5}
        cy={8.5}
        r={6}
        fillRule="evenodd"
        fillOpacity={0.8}
      />
    </svg>
  )
}

export default SvgComponent

Take a look at the prefix__. I don’t see any option to remove it as well. Is this a bug?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
polarathenecommented, Apr 9, 2021

You would think that since there’s no mention of it being enabled in the SVGR documentation, while SVGO docs explicitly mention it as disabled by default…

This is what SVGR is doing internally with their SVGO plugin:

https://github.com/gregberge/svgr/blob/fbb4ee5014549737d4c637bfac0f7c73193edea7/packages/plugin-svgo/src/config.js#L7-L15

Which is then merged with two other possible configs from the user, so you need to keep those two plugins in mind if you want to override them:

https://github.com/gregberge/svgr/blob/fbb4ee5014549737d4c637bfac0f7c73193edea7/packages/plugin-svgo/src/index.js#L87-L95

Note that removeViewBox: false says not to remove the viewBox attribute if the config for removing dimensions is set to false… however removeDimensions (that would remove the width and height attributes is not enabled by default AFAIK, and for that to work you must also disable the default removeViewBox option. I haven’t yet verified that with svgr, but that appears to be a bug.


This base config option for prefixIds was added in Nov 2018 for resolving this issue regarding styling concerns.

The prefix feature was added to SVGO in 2017. There’s a related plugin cleanupIDs which is enabled by default that produces the minified ID which was the main concern from the SVGR PR to enable the prefixIds plugin. That can be configured to instead provide a more unique ID that’s not as long as a prefix may be, by having webpack generate a content hash.

That said, if your concern is with filesize, do look at the compressed size (eg gzip), as that concern may be optimized away in practice.

0reactions
deadcoder0904commented, Apr 9, 2021

I think I resolved this one simply by removing prefix__.

It wasn’t much of an issue tbh. Just a tiny nitpick.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically add a class to manual class names?
You can either do this, normal JavaScript: className={'wrapper searchDiv ' + this.state.something}. or the string template version, ...
Read more >
DOM Elements - React
className. To specify a CSS class, use the className attribute. This applies to all regular DOM and SVG elements like <div> , <a>...
Read more >
Inline Styling with React - Pluralsight
In most cases, className should be used to reference classes defined in an external CSS stylesheet. style is most often used in React...
Read more >
How To Style React Components | DigitalOcean
In this tutorial, you'll learn three different ways to style React ... in this tutorial you will prefix the wrapper class name with...
Read more >
react-classname-prefix-loader - npm
A Webpack loader that prefixes classes with custom prefix in React components. Latest version: 1.0.8, last published: 5 years ago.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found