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

Simplify usage with var(--color) #72

Open
henryruhs opened this issue Jun 21, 2020 · 14 comments
Open

Simplify usage with var(--color) #72

henryruhs opened this issue Jun 21, 2020 · 14 comments

Comments

@henryruhs
Copy link

henryruhs commented Jun 21, 2020

Hey, I use this awesome plugin after processing variables with postcss-custom-properties ... to make it work together something like this is needed:

@svg-load icon-search url('@mdi/svg/svg/magnify.svg')
{
	fill: var(--color);
}

&:after
{
	background: svg-inline(rs-icon-search) center no-repeat;
}

What I totally would love is a syntax like:

&:after
{
	background: svg-load('@mdi/svg/svg/magnify.svg', fill = var(--color)) center no-repeat;
}

@TrySound Is there a way to rewrite this plugin or my postcss chain to make the easy syntax possible?

require('postcss-custom-properties')(
{
	preserve: false
}),
require('postcss-inline-svg')(
{
	paths:
	[
		'node_modules'
	]
}),

Thanks

@henryruhs henryruhs changed the title Simplify usage with var(--color) function Simplify usage with var(--color) Jun 21, 2020
@djmtype
Copy link

djmtype commented Jul 9, 2020

In this day and age we need to use custom property values

@henryruhs
Copy link
Author

@djmtype I don't get it... could you explain what you are talking about?

@djmtype
Copy link

djmtype commented Jul 9, 2020

@redaxmedia I was supporting you.

We need a simpler method like your example. Right now, the a custom property, var(--primary-color), for fill won't get passed. It will be ignored.

.example {
    background: svg-load('img/icon-arrow.svg', fill=var(--primary-color));
}

@hudochenkov
Copy link
Collaborator

CSS doesn't support url interpolation. This CSS is not valid:

.up {
    background: url("data:image/svg+xml;charset=utf-8,%3Csvg fill=var(--primary-color) %3E...%3C/svg%3E");
}

@henryruhs
Copy link
Author

I was not asking for keeping the vars inside the url...

@hudochenkov
Copy link
Collaborator

Are you using them as preprocessor variables (like $var) and not custom properties? Then run PostCSS plugin, which replaces them to actual values, before postcss-inline-svg. There is no way postcss-inline-svg will know what variable values, and it will not replace it.

@henryruhs
Copy link
Author

henryruhs commented Oct 29, 2020

Why on earth should something be impossible that can be done with 2 lines of code by switching to 1 line of code? This plugin should just accept another syntax and that's all I am looking for. I don't mind about CSS support or whatever, I want the hex values hard coded like this plugin does anyway.

@conor-darcy
Copy link

Yes, this would be a great feature.
--color: #ABCDEF;
background: svg-load('@mdi/svg/svg/magnify.svg', fill=var(--color)) center no-repeat;

would become:
background: url("data:image/svg+xml;charset=utf-8,%3Csvg fill=#ABCDEF %3E...%3C/svg%3E");

@mdmoreau
Copy link

@conor-darcy for what it's worth I created a small plugin to handle that: https://www.npmjs.com/package/postcss-root-var

@nevolgograd
Copy link

nevolgograd commented Nov 25, 2021

Did anyone manage to make it work with css-vars?
@redaxmedia i've tried your approach and it didn’t work for me, still see var(--color) in my inline svg
@mdmoreau same for your tool

guys, any advice?

Config:

require('postcss-custom-properties')({
  preserve: true
}),
require('postcss-inline-svg')({
  paths: ['src'],
  removeFill: true,
  removeStroke: true
})

Css:

@svg-load icon url(assets/icons/download.svg) {
  path {
    fill: var(--color-W0);
    stroke: var(--color-W0);
  }
}

background-image: svg-inline(icon);

Result:

background-image: url("data:image/svg+xml;charset=utf-8*****fill=%27var%28--color-W0%29%27 stroke=%27var%28--color-W0%29%27/%3E %3C/svg%3E")

@henryruhs
Copy link
Author

@nevolgograd well, it is preserve: false and not preserve: true within the postcss-custom-properties plugin.

@nevolgograd
Copy link

@redaxmedia thx, but unfortunately result is the same

@henryruhs
Copy link
Author

henryruhs commented Jan 19, 2022

I found this "black magic" that lets you change the color of SVG without touching the fill attribute. I started using CSS filter to invert black SVG to white and later on found this generator to apply hex colors:

See: https://codepen.io/sosuke/pen/Pjoqqp

Red colored SVG would look like:

.svg-on-fire {
    filter: invert(99%) sepia(100%) saturate(18%) hue-rotate(315deg) brightness(104%) contrast(100%);
}

@WebMechanic
Copy link

sad to see there appears to be fix from the plugin...

@henryruhs filter affects the whole element, not just the background-image, so this "magic" is not useful for elements with other contents.

the question is: how often in one page and one stylesheet do you need to recolour the background image? and how many are even just monochrome?

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

No branches or pull requests

7 participants