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.

Multiple defaults for CSS Custom Properties

See original GitHub issue

Consider this case

class MyElement extends HTMLElement {
  constructor() {
    super().attachShadow({ mode: 'open' }).innerHTML = `
      <style>
        :host { color: var(--my-color, red); }
        @media (prefers-color-scheme: dark) {
          :host { color: var(--my-color, blue); }
        }
      </style>
    `;
  }
}

Should I get

"cssProperties": [
  {
    "name": "--my-color",
    "default": "red"
  }
]

Or

"cssProperties": [
  {
    "name": "--my-color",
    "default": "blue"
  }
]

And if I get either, that’s not really correct is it?

We should add a schema facility for declaring multiple defaults and their conditions

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bennypowerscommented, Jul 19, 2021

Something to consider: if condition or conditions is meant to be machine-readable, then the values should be strictly identical

i.e. a tool like storybook or <api-viewer> might check a css property’s contexts like so:

propertyContext.condition === '@media(prefers-color-scheme: dark)'

Or, it might perform a regexp check that allows for whichever whitespace rules that condition’s language allows.

1reaction
castastrophecommented, Jul 16, 2021

I see these as variations or contexts. They’re still defaults for the same CSS property but they are dependent upon certain conditions. What about defining these contexts in connection to the primary definition like this:

"cssProperties": [{
    "name": "--my-color",
    "description": "Change the color of the button",
    "default": "blue",
    "contexts": [{
        "description": "Dark mode button color",
        "default": "red",
        "condition": "@media (prefers-color-scheme: dark)"
    }]
}]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using CSS custom properties (variables) - MDN Web Docs
Custom property fallback values​​ Using the var() function, you can define multiple fallback values when the given variable is not yet defined; ...
Read more >
Custom properties with defaults: 3+1 strategies - Lea Verou
We want to support a --color custom property for creating color variations by setting multiple things internally:
Read more >
A Complete Guide to Custom Properties | CSS-Tricks
Custom properties can make code easier to maintain because you can update one value and have it reflected in multiple places.
Read more >
A Strategy Guide To CSS Custom Properties
CSS Custom Properties are by default locally scoped to the specific selectors we apply them to. So they are kinda like local variables....
Read more >
CSS Custom Properties | 12 Days of Web
To define a custom property, use the name of your choice preceded by two dashes, for example: --my-property . Then you can use...
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