Git Product home page Git Product logo

Comments (7)

jhildenbiddle avatar jhildenbiddle commented on July 23, 2024 2

Hey @bene-starzengruber.

Use the options.onComplete callback to access a list of all CSS custom properties.

  • Set options.onlyLegacy to false tell the ponyfill to do this in both modern and legacy browsers,
  • Set updateDOM options.updateDOM to false 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.

jhildenbiddle avatar jhildenbiddle commented on July 23, 2024 1

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.

jhildenbiddle avatar jhildenbiddle commented on July 23, 2024 1

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.

bene-starzengruber avatar bene-starzengruber commented on July 23, 2024

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.

jhildenbiddle avatar jhildenbiddle commented on July 23, 2024

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.

bene-starzengruber avatar bene-starzengruber commented on July 23, 2024

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.

bene-starzengruber avatar bene-starzengruber commented on July 23, 2024

Yes, everything is working now!

Thanks, your work is highly appreciated :)

from css-vars-ponyfill.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.