question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatic CSS Variable Scoping

See original GitHub issue

Overview

Currently, CSS variables are treated just like any other property in blocks. This means that conflicting variable names are expected to be explicitly resolved by the developer, but we can do better than that!

Problem

CSS variable can be given special treatment in blocks. Consider two blocks a and b:

/* a.block.css */
:scope {
  --my-var: red;
}
.class {
  color: var(--my-var);
}
/* b.block.css */
:scope {
  --my-var: blue;
}
.class {
  color: var(--my-var);
}

If both blocks are applied to the same element, their --my-var definitions will conflict, causing unexpected behavior and requiring explicit resolution.

Now consider two blocks base and extended:

/* base.block.css */
:scope {
  --theme-color: red;
}
.class {
  color: var(--theme-color);
}
/* extended.block.css */
@block-reference base from "base.block.css";
:scope {
  extends: base;
  --theme-color: blue;
  --local-var: red;
}
.bar {
  color: var(--local-var);
}

Here, the exception is elements with block extended applied will use the re-defined --my-var class for all inherited styles.

Proposal

Compiled blocks should rewrite variable names to unique identifiers. This is easily done by prefixing all vars with their uid:

/* a.block.css */
.a {
  --a-my-var: red;
}
.a__class {
  color: var(--a-my-var);
}
/* b.block.css */
.b {
  --b-my-var: blue;
}
.b__class {
  color: var(--b-my-var);
}

In blocks that extend a base block, conflicting css var names should inherit the name of their base block, while locally defined names should be prefixed with the local uid. So our extended block example becomes:

/* base.block.css */
.base {
  --base-theme-color: red;
}
.base__class {
  color: var(--base-theme-color);
}
/* extended.block.css */
.extended {
  --base-theme-color: blue;
  --extended-local-var: red;
}
.extended__bar {
  color: var(--extended-local-var);
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
chriseppsteincommented, Nov 13, 2017

It’s really not clear to me that css custom properties should be scoped to the block.

The concept of a block-scoped css custom property is interesting, but I’m not sure that it should be the default behavior.

0reactions
brandonkalcommented, May 8, 2019

I believe css variables should not be scoped to the block. They provide a nice way to theme components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS variables: Scoping - LogRocket Blog
This article shows you how to apply scoping in CSS variables to streamline theming and enable clean, modular design systems.
Read more >
Understanding the Scope in CSS Variables - Part 2 - Webkul
Scope determines the range of the accessibility of the variable. Scope helps to store separate values, which comes into play only when they...
Read more >
The Power (and Fun) of Scope with CSS Custom Properties
Let's look at a couple of instances where CSS variables can be used to do some pretty cool things that we may not...
Read more >
Using CSS custom properties (variables) - MDN Web Docs
However, this doesn't always have to be the case: you maybe have a good reason for limiting the scope of your custom properties....
Read more >
A Complete Guide To CSS Variables [With Examples]
Scope In CSS Variables. In the implementation shown earlier, the root pseudo class (in which the variable is declared) and its significance in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found