Comments (7)
Hey @bene-starzengruber.
Use the options.onComplete
callback to access a list of all CSS custom properties.
- Set
options.onlyLegacy
tofalse
tell the ponyfill to do this in both modern and legacy browsers, - Set updateDOM
options.updateDOM
tofalse
to do a read-only (no changes to the DOM will be made).
cssVars({
onlyLegacy: false, // Execute in modern and legacy browsers
updateDOM : false, // Don't do anything
onComplete: function(cssText, styleNode, cssVariables) {
// Do stuff with cssVariables
console.log(cssVariables);
}
});
Use options.variables
to add or modify CSS custom properties. See the Examples section for details.
cssVars({
variables: {
"--my-color": "red"
}
});
This will either add the --my-color
CSS custom property or update its value if it already exists. All other CSS custom properties will remain unchanged.
You can call cssVars()
as many times as you like, passing only the custom properties you want to add or update to options.variables
. For performance reasons, it's obviously better to call the ponyfill one time with many variables as opposed to multiple times with a single variable each time.
If you want to 1) read a custom property value and then 2) change it based on its value, combine the two methods:
// 1st call: Get the CSS custom property values
cssVars({
onlyLegacy: false,
updateDOM : false,
onComplete: function(cssText, styleNode, cssVariables) {
if (cssVariables["--my-color"] === "red") {
// 2nd call: Edit a single custom property
cssVars({
variables: {
"--my-color": "blue"
}
});
}
}
});
from css-vars-ponyfill.
You bet, @bene-starzengruber.
FWIW, I created this quick demo to test options.variables
and overwriting values:
The test exposed an issue where the cssVariables
argument is empty when using the first block of code I provided:
cssVars({
onlyLegacy: false, // Execute in modern and legacy browsers
updateDOM : false, // Don't do anything
onComplete: function(cssText, styleNode, cssVariables) {
// Do stuff with cssVariables
console.log(cssVariables);
}
});
You can see this in the demo. Changing updateDOM
to true
(or comment it out since true
is the default) solves the issue, but this is a bug. You shouldn't have to make changes to the DOM just to read CSS custom property values.
Look for a fix soon. I wanted to let you know about this in case it is related to the issues you're having.
from css-vars-ponyfill.
Fixed in 1.12.2.
Sorry about that. 1.12.1 fixed a separate but related bug. The bug you ran into was a regression introduced in 1.11.0. This has been fixed, and values passed to options.variables
should now persist properly.
New demo available here:
from css-vars-ponyfill.
Thanks!
I still have the problem that the options.variables is overriding my other variables as well but now with your hints, I just get all variables, change some and set them all again. I know that this is probably not as performant but enough for my use case.
from css-vars-ponyfill.
Fixed in 1.12.1.
I've left the demo on 1.12.0 to you can see the bug. If you update the dependency to 1.12.1, you will see the bug has been fixed.
from css-vars-ponyfill.
I just tried it out with the new version and am still running into the same problem
https://stackblitz.com/edit/angular-u7687e?file=src%2Fapp%2Fapp.component.ts
- Change Background color - Background will change but the css-variable is not set on the :root element
- Change TextColor - Text color is changed but background is resetted to the initial value
Have a look at the console output - I made logs every time I call the cssVars function
from css-vars-ponyfill.
Yes, everything is working now!
Thanks, your work is highly appreciated :)
from css-vars-ponyfill.
Related Issues (20)
- works in IE dev mode, but not prod mode HOT 2
- CSS XHR error HOT 2
- Not working IE11 in Prod Mode HOT 3
- How to switch themes without browser reload using css variables? HOT 1
- v2.4.4 no longer transpiles to pure ECMAScript 5 HOT 1
- `CSS.supports()` doesn't support in IE11, but css-vars-ponyfill insert it into build artifact. HOT 1
- Option to skip formatting CSS HOT 3
- Does this library works for dynamically added CSS? HOT 1
- Am i missing something for use in Angular 10+? HOT 1
- the page's efficiency HOT 1
- Doesn't work with styled-components createGlobalStyle HOT 3
- Can I use it in Nuxtjs? HOT 2
- run onComplete callback too slow HOT 8
- [bug] link set setAttribute disabled not work HOT 1
- HasVarChange has a calculation problem HOT 1
- `fixKeyframes` throws error when using shadow DOM + legacy browsers HOT 1
- IE11 js runs too slowly. Can you use this when packaging webpacks? HOT 1
- map file HOT 1
- cssVars():: CSS parse error: missing '}' HOT 1
- ie 9 10 Use without effect HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from css-vars-ponyfill.