Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use the svg in css #67

Closed
samuelpaulc opened this issue Nov 7, 2016 · 13 comments
Closed

How can I use the svg in css #67

samuelpaulc opened this issue Nov 7, 2016 · 13 comments
Labels

Comments

@samuelpaulc
Copy link

In our react project, we came across a situation when we had to use a third party library and we needed to apply our svg icons on their UI.

One way is to fork and fix it but I prefer if we could do it css whilst still using the sprited svgs.

Is this possible? If not what other option do I have apart from using the unsprited svg as is(which would be a separate request)

@kisenka
Copy link
Contributor

kisenka commented Nov 8, 2016

Could you provide an example?

@samuelpaulc
Copy link
Author

We are using the sprited svgs we get from this loader in the below way which is what the example was saying and it works very well.

<svg
style={style}
dangerouslySetInnerHTML={{ __html: '' }}
/>

But we are using a third party library 'rc-tree' and we want to apply our own icons for the tree.
They have a className provided their api where we can put out icons and style it.

I was wondering if we can reuse the sprited svg somehow?

@kisenka
Copy link
Contributor

kisenka commented Nov 8, 2016

@samuelpaulc in this case - no. You should should customize rc-tree classes with following manner:

.override-rc-class {background-image: url('image.svg')} // svg should inlined via url-loader

@samuelpaulc
Copy link
Author

Oh so I need to use https://github.com/bhovhannes/svg-url-loader right?

@kisenka
Copy link
Contributor

kisenka commented Nov 8, 2016

@samuelpaulc right

@samuelpaulc
Copy link
Author

Cool 👍 Thanks Stas

@samuelpaulc
Copy link
Author

@kisenka But when we use the svg-url-loader, a separate request is made, can it not work together with svg-sprite-loader where it gets sprited?

@kisenka
Copy link
Contributor

kisenka commented Nov 8, 2016

What you mean saying "a separate request is made"? svg-url-loader inline SVG contents into CSS.

@samuelpaulc
Copy link
Author

@kisenka
We are also using svg-sprite-loader for our svg images which are used directly in JS as their documentation says and it works well.

Now we are trying to make the svg that we load from within a css class to be also sprited

I am trying this and it does not work.

Using only svg-sprite-loader:
Works well for svgs included in js but if a svg in css is used I get the below error
Error: attribute y: Expected length, "%5C'6%5C'".

Using only svg-url-loader
Works

Using both together
Module build failed: ReferenceError: document is not defined

Order of loaders also dosent help;
Webpack config:
{
test: /.svg$/,
include: 'path/to/svg',
loaders: [
'svg-sprite?' + JSON.stringify({
name: '[name]',
prefixize: true
}),
'svg-url-loader'
]
}

Error I get for this config is
Module build failed: TypeError: Must be an object

Please suggest where I am going wrong and how to get both of these loaders to work together

@kisenka
Copy link
Contributor

kisenka commented Nov 16, 2016

@samuelpaulc

  1. You cannot use both loaders for single file.
  2. svg-sprite-loader can be used only in JS, not in CSS.

Use svg-url-loader in CSS and svg-sprite-loader in JS.

@lysoff
Copy link

lysoff commented Jan 25, 2017

@kisenka

Use svg-url-loader in CSS and svg-sprite-loader in JS.

could you provide webpack config example for this?

@kisenka
Copy link
Contributor

kisenka commented Feb 10, 2017

@lysoff if you want to import SVGs from CSS with svg-url-loader (inline as data-uri) and from JS with svg-sprite-loader I must warn you that this is potential source of collisions of these two loaders. For workaround this you can define svg-url-loader as default loader for SVGs, and exclude some folders which should be processed by svg-sprite-loader:

const svgSpriteFolder = path.resolve(__dirname, 'folder-with-svgs-for-svg-sprite-loader');

...

loaders: [
  // default loader for SVG
  {
    test: /\.svg$/,
    loader: 'svg-url-loader',
    exclude: svgSpriteFolder
  },

  {
    test: /\.svg$/,
    loader: 'svg-sprite-loader',
    include: svgSpriteFolder
  }
]

@constverum
Copy link
Contributor

One more way:

.icon1 {
  /* SVG will be inlined in CSS */
  background-image: url('./foo.svg?inline&fill=red');
}

.icon2 {
  /* default way - build a sprite and use SVG fragment identifiers */
  background-image: url('./foo.svg&fill=red');
}

Webpack config:

{
  module: {
    rules: [
      ...
      {
        test: /.svg$/,
        oneOf: [
          {
            resourceQuery: /inline/, 
            use: [
              'svg-url-loader',
              'svg-fill-loader',
            ],
          },
          {
            use: [
              {
                loader: 'svg-sprite-loader',
                options: {
                  esModule: false,
                  extract: true,
                },
              },
              'svg-fill-loader',
            ],
          },
        ],
      },
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants