Skip to content

[css-values] Key-Value and Index Maps for attr() #4484

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

Closed
Crissov opened this issue Nov 5, 2019 · 3 comments
Closed

[css-values] Key-Value and Index Maps for attr() #4484

Crissov opened this issue Nov 5, 2019 · 3 comments

Comments

@Crissov
Copy link
Contributor

Crissov commented Nov 5, 2019

Note: […] The attr() expression cannot return everything, […]. This is intentional, as the intent of the attr() expression is not to make it possible to describe a presentational language’s formatting using CSS, but to enable CSS to take semantic data into account.

To achieve this goal, I think attr() would benefit from a way to map attribute values to CSS values.

  • Key-value pairs: attr(party map("Dem" blue, "Rep" red, "Grn" green) color, gray)
  • Numeric index:
    • 1-based: attr(rank map(gold, silver, orange) color, gray)
    • 0-based: attr(rank map(black, gold, silver, orange) color, gray)
    • explicit: attr(rank map(1 gold, 2 silver, 3 orange) color, gray)
  • mixed: attr(rank map(gold, silver, orange, "DQ" red) color, gray)

Similar results can already be achieved with attribute selectors, though:

[party] {color: gray}
[party="Dem"] {color: blue}
[party="Rep"] {color: red}
[party="Grn"] {color: green}
[rank] {color: gray}
[rank=1] {color: gold}
[rank=2] {color: silver}
[rank=3] {color: orange}
[rank=NQ] {color: red}
@Crissov
Copy link
Contributor Author

Crissov commented Jan 21, 2025

Rediscovering this suggestion of mine, I think with recent changes and additions to CSS, it probably makes more sense to do such mappings in a way that is also usable with var() etc., i.e. move the attr() inside the map():

map(attr(party <string>),
"Dem" blue,
"Rep" red,
"Grn" green,
gray)

I thought @LeaVerou had raised a similar issue, but neither #9992 nor #11543 seems to be it.

@tabatkins
Copy link
Member

This is now doable with the if() function:

--foo: attr(party);
background-color: if(
  style(--foo: "Dem"): blue;
  style(--foo: "Rep"): red;
  style(--foo: "Grn"): green;
  else: gray);

So, closing as addressed. ^_^

@Crissov
Copy link
Contributor Author

Crissov commented Mar 8, 2025

I did not think of it like that yet, so I gladly give you ”doable“, but I won’t give you pretty.

background-color: map(attr(party),
  "Dem": blue, 
  "Rep": red,
  "Grn": green,
  else: gray
);

background-color: switch(attr(party),
  "Dem": blue, 
  "Rep": red,
  "Grn": green,
  gray
);

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

3 participants