PricingBlog

what is the scope of style variables?

  • benjamin.h-1453524356125692028

    Ben H

    7 days ago

    can someone clarify the expected scope of style variables?

    i declared some style variables at the root element of the page i'm working in. inside that root div, i can see them available in the style panel as expected. however, in children of this div, sometimes they seem available and sometimes not.

    for example, inside one child component, referencing --font-color works as expected.

    however, in a different child component, referencing --font-color-secondary is not working.

    are there rules regarding where these declared style variables can be used?
    1453524357912596715-image.png
    1453524357447155813-image.png
    1453524356616552704-image.png
  • what is the scope of style variables?
  • jacobkofoed-1453702103846617088

    Jacob Kofoed

    6 days ago

    We are working on a new version of CSS variables that will release very soon. In the new version you can declare variables globally, making them available everywhere.

    However, if not declared globally the variables are only available inside a component (this is how the current system works, always). The reason that you do not see the variable in sub-components is that the component has no way of knowing who their parent may be, as a component may be used by multiple parent elements. At compile-time, a component knows its children, but not parents, so we cannot show the inherited variables in the dropdown.

    In addition to defining variables globally, the new CSS variable system has other options to declare what variables a component should be able to use. We will write documentation on this before release 🙂
    🤶1
  • I cannot explain why it seems to sometimes be available and sometimes not in your examples though 🤔
    🎅1
  • benjamin.h-1453777165022662686

    Ben H

    6 days ago

    Thanks Jacob! That confirms my understanding - my CSS variables declared in the root div of my page should be available to all child components (just not the dropdown menus in the UI). Must be a bug in my code then 🙂 (edit: can confirm was me being dumb again)
  • benjamin.h-1453777726463803484

    Ben H

    6 days ago

    New global variables sound cool too but I think for my use case page-scoped variables are most appropriate