Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-values?] What is the MVP for inline conditionals on custom properties? #10064

Open
LeaVerou opened this issue Mar 12, 2024 · 1 comment
Open

Comments

@LeaVerou
Copy link
Member

LeaVerou commented Mar 12, 2024

There are several issues across this repo proposing some kind of inline conditional function. Here is a sample:

Yet, this is another case where progress has stalled because we’re trying to flesh out a much more general and powerful feature, which involves a significant amount of design & implementation effort. Meanwhile, this major author pain point remains unsolved, and authors have to resort to HTML attributes instead (as explained in #5624).

The current workarounds are:

  • Using a 0/1 switch and linear interpolation: calc(var(--test) * var(--if-true) + (1 - var(--test)) * var(--if-false))
  • "Space toggle": Using presence/absence and var(--test, var(--if-false))
  • Style container queries: these are great, but only work on descendants.

However, there is no workaround for transforming arbitrary keywords to arbitrary values, even simple values. E.g. custom properties like these are impossible to implement (examples from Shoelace, one of the most popular web component libraries, but similar use cases can be found in almost any design system and/or WC library):

What if we could come up with an MVP that could be implemented fast and extended later?

We could scope it down quite a lot and still have something that addresses the most pressing author pain points.
Some example restrictions we could start with:

  • Only style( <style-query> ) conditionals
  • Only custom properties
  • If it helps, the value space could be restricted too, even as much as single tokens
  • If it helps, it could even be something that’s only allowed in custom property values

Syntax could be either functional (probably easier):

<if()> = if (style( <style-query> ), <true-value>, <false-value>? )

or keyword-based (fewer parens, no conflict with preprocessors):

<if-inline> = if style( <style-query> ) then <true-value> [ else <false-value> ]

Implementors, would this make it tractable? If not, what would?

@LeaVerou LeaVerou changed the title What is the MVP for inline conditionals on custom properties? [css-values?] What is the MVP for inline conditionals on custom properties? Mar 12, 2024
@jjenzz
Copy link

jjenzz commented Apr 13, 2024

Style container queries: these are great, but only work on descendants.

when i first discovered style queries, i admit i had intuitively tried to do something like this:

@container style(--variant: large) {
  & {
    /* style the container itself */
  }
}

it seems a way to select the container that @container style() matches would be a nice stepping stone but i have nowhere near enough context/history to know if that is doable.

or maybe enabling style queries as part of the @scope api?

@scope style(--variant: large) {
  :scope {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants