Repository navigation
Description
(This proposal has some overlap with #542, but that one is specifically about string concatenation. This proposal is about using concatenation to construct custom-idents and dashed-idents)
In many CSS features, authors need to give elements a certain name so that they can later refer to those targeted elements. Think of container-name, view-transition-name, view-timeline-name, scroll-timeline-name, etc. Depending on the property these names are of the type <custom-ident> or <dashed-ident>. These names need to be unique (within the feature that’s being used). In case of View Transitions and Scroll-Driven Animations this uniqueness can become burden for authors.
Take this Scroll-Driven Animations example, where the author is setting a unique timeline name per element, using :nth-child():
.parent {
timeline-scope: --tl-1, --tl-2, --tl-3;
}
nav {
li:nth-child(1) { animation-timeline: --tl-1; }
li:nth-child(2) { animation-timeline: --tl-2; }
li:nth-child(3) { animation-timeline: --tl-3; }
}
main {
div:nth-child(1) { view-timeline: --tl-1; }
div:nth-child(2) { view-timeline: --tl-2; }
div:nth-child(3) { view-timeline: --tl-3; }
}Same with View Transitions, where I’ve seen this code in the wild
&:nth-child(1) {
view-transition-name: opt-1;
& > label {
view-transition-name: opt-1-label;
}
& > input {
view-transition-name: opt-1-input;
}
}
&:nth-child(2) {
view-transition-name: opt-2;
& > label {
view-transition-name: opt-2-label;
}
& > input {
view-transition-name: opt-2-input;
}
}
&:nth-child(3) {
view-transition-name: opt-3;
& > label {
view-transition-name: opt-3-label;
}
& > input {
view-transition-name: opt-3-input;
}
}To make things easier for authors, it would be nice if they could dynamically construct <custom-ident> or <dashed-ident> values. I am thinking of two functions:
ident(…)to generate a<custom-ident>valuedashed-ident(…)to generate a<dashed-ident>value
The functions can accept an arbitrary number of arguments, but need at least 1. The arguments are of the type <string> or a function that generates string-like values (*).
With sibling-index() being accepted, the first example could be simplified as follows:
nav li {
animation-timeline: dashed-ident("tl-", sibling-index()); }
}
main div {
view-timeline: dashed-ident("tl-", sibling-index());
}Ideally, the output of calc() and attr() would also be allowed as arguments into the proposed functions.
(*) Maybe this last part needs an explicit cast to string, which is covered in #542. Or ff the casting of the arguments would be too difficult to do, maybe some value interpolation could be used? In that case the functions would accept only 1 argument, e.g. dashed-ident("tl-{sibling-index()}").
Activity
argyleink commentedon Aug 2, 2023
yes! i have many use cases for this 🙂
it would really DRY up my styles.
i asked about this at the end of presenting
sibling-index(), thank you so much for formalizing it and articulating the space so well 🙏🏻bramus commentedon Aug 16, 2023
While building https://codepen.io/bramus/pen/mdabWzr earlier today I came to the realisation that
sibling-index()won’t cut it in the case of View Transitions. If one, for example, inserts a node in a list at positionxthen all indexes after that position would get shifted, essentially breaking the View Transition for all those elements.Instead, something like
random()should be used, as that one is able to generate a value per element. However, it might lead to clashes in case it generates the same value twice, so it’s not 100% closing. Maybe we also need something likeunique()as that is guaranteed to not generate duplicate values.All this to say that my initial “Ideally, the output of
calc()andattr()would also be allowed as arguments into the proposed functions.” might be a requirement from the start.noamr commentedon Oct 10, 2023
I love this! especially for #8320, in conjunction with #8319.
A few nits:
dashed-identor simplyident()that joins whatever it gets, strings or attributescontente.g.:
ident("song" attr(id) "-" counter(foobar))would return e.g.song300-1vmpstr commentedon Oct 10, 2023
Might also consider something like an
iota()that would just be a single:root-level counter that increments after each use. I like leaning into counters here, since we need to be a bit careful about style containment.unique()works too thoughbramus commentedon Oct 11, 2023
I split out both because they return a different value. But I think I am misunderstanding what you are saying here.
Good suggestion!
Since
attr()comes with its own set of challenges, authors initially can work around it by setting a custom prop to some sort of identifier, and use that inident(). For example:LeaVerou commentedon Oct 12, 2023
I’m not sure if the best solution is a way to construct dynamic idents, or just better scoping for these features. E.g. what if nesting these inside other rules also scoped their idents?
With dynamic idents authors still need to think of a way to ensure uniqueness, which increases cognitive overhead, and is error prone.
noamr commentedon Oct 15, 2023
Can you please elaborate? I don't fully understand.
In light of #8319, the ident itself doesn't have to be unique (for view transitions).
I get the issues with this but not the alternative (yet).
Right now authors still need to ensure uniqueness but they have to do that with JS or on the server, this allows them to do this in css directly. Happy to discuss alternatives!
bramus commentedon Apr 9, 2024
I think @LeaVerou means that if names could be contained/scoped by whatever trigger, that would open up the way to safely allow duplicate names to exist alongside each other.
While that would work in some cases – i.e. where children look up the DOM tree to get access to a thing with a certain name – it does not work when all those named items are part of bigger whole.
For example: View Transitions with many cards still need a unique name on each card as they all participate in the same VT, or ScrollTimelines that all need to be hoisted up to a shared parent via
timeline-scope, same with anchoring, etc.They already need to think about uniqueness in many cases (names for view-transition elements, containers, timelines, anchors, custom properties, etc). The solution that is pursued here would allow authors to dedupe a lot of repetitive uniqueness.
E.g. things like this:
noamr commentedon Apr 9, 2024
@bramus what does the
ident()function add here? Why not use strings, and turn it into an ident automatically when the property expect an ident (e.g.view-transition-name)bramus commentedon Apr 9, 2024
Parsing purposes. Think of shorthands, such as
scroll-timeline.With
ident():scroll-timeline: inline ident("tl-" var(--id))scroll-timeline: ident("tl-" var(--id)) inlineWithout
ident():scroll-timeline: inline "tl-" var(--id)scroll-timeline: "tl-" var(--id) inlinenoamr commentedon Apr 9, 2024
Sure, though perhaps these can work without the
identfunction when it's not in a shorthand.noamr commentedon Apr 11, 2024
So summarizing the discussion here and in #8320 into a proposal:
id()anddata(foo)functions that generate idents from attributes. (Alternativelyattr()with an allow-list)These can be used directly in
identfunctions, in properties that accept idents, or in custom propertiesident("literal-string" some-ident-from-attr --some-ident-from-var)to generate an ident that can be used anywhere that accepts an identidentfunction can be implied (likeurl()in certain cases)So this for example would work:
This still means that developers would need to figure out how unique IDs are created, but I don't see a way around it yet.
Loirooriol commentedon Apr 11, 2024
If
data()reads arbitrarydata-*attributes, I don't think it should produce an ident, at least by default.This seems like a huge forwards-compatibility problem.
noamr commentedon Apr 11, 2024
What should it be? A string? In what properties would this string be used?
Perhaps it can have a special type, something like attribute-value that can be used inside
ident()and perhaps later on insideurl()?Fair enough. We can decide this on a case-by-case basis or always require
ident().24 remaining items
LeaVerou commentedon Oct 25, 2024
@tabatkins Does that mean
var()inident()would work? Is the plan to change this so that it also allowsvar(var())andvar(attr())?tabatkins commentedon Nov 1, 2024
Yeah,
var(var())probably should work, it just requires changes to the processing model. I need to open a top-level issue for it.kizu commentedon Nov 1, 2024
I remember trying
var(var())back in 2016 when I was first playing with custom properties, and being sad that we couldn't do it. If this will be eventually possible (especially with an ability to construct idents dynamically), that will be great!romainmenke commentedon Dec 11, 2024
Should it be possible to assign values to custom properties with constructed
dashed-ident's?keithamus commentedon Dec 11, 2024
This seems tricky because of arbitrary peek-ahead (what makes
ident(...):pink;the declaration different toident(...):pink {}the rule - other than the{N tokens to the right). Unless we explicitly disallow theident(function token in a style rule's prelude, at which point that may gate off future expressivity for selectors - but maybe that's okay?bramus commentedon Dec 11, 2024
This is food for a follow-up issue, outside of the scope of the original ask.
bramus commentedon Dec 11, 2024
To summarize: I am proposing the
ident()function to dynamically construct<ident>s. Authors would use this when they need to set idents for a bunch of elements in one go, instead of having to set all those idents individually.The syntax for
ident()looks like this:EDIT: I added
<integer>to this. See next comment.Example usage:
Simple:
More advanced:
FAQ:
Q: Why do we need the function? Can’t one directly glue things together, e.g.
view-transition-name: var(--id) "title";?A: Parsing Purposes.
Q: Why not just
attr(foo type(<custom-ident>))?A: With
ident()you can glue things together, e.g.ident("view-" attr(data-type) "-" attr(data-sequence)). The pieces that need to be glued can come from other elements as well (see example with.cardabove)Q: What about constructing dashed idents?
A: Put
"--"at the start, e.g.ident("--item-tl-" attr(data-itemnum))Q: Doesn’t the
attr()function needtype(<custom-ident>)added to it (which now makes your examples look shorter)?A: No, when no
<attr-type>is given, the<attr-name>gets parsed to a CSS string (see spec). Becauseident()is designed to accept strings, it works just fine.Q: Why not rely on something like
…-name: auto?A: Sometimes you want to apply this generated ident to a certain element, and then apply it to a different one. See an aspirational demo like https://codepen.io/bramus/pen/PogVZwb in which the
title-item-1generated name is conditionally applied to two different elements.A: In many cases you also need to use that generated name somewhere else (e.g.
view-timeline-name->animation-timeline)That should be it. Everything else mentioned in this issue but not in this comment falls outside of the scope of what is proposed.
bramus commentedon Dec 11, 2024
One thing I realized after typing up the above: we should also allow
<integer>in the function, to allow authors to, for example, usesibling-index().LeaVerou commentedon Dec 15, 2024
Heh, just came here to post exactly this:
Or even things like color tints.
And we need to make sure multiple levels of this work. E.g.:
css-meeting-bot commentedon Dec 18, 2024
The CSS Working Group just discussed
[css-values] A way to dynamically construct custom-ident and dashed-ident values, and agreed to the following:RESOLVED: Adopt ident() function into css-values-5The full IRC log of that discussion
<noamr> bramus: this is about dynamically constructing dynamic idents/dashed-idents. for when authors need to come up for idents in multiple elements in one go<bramus> https://github.com//issues/9141#issuecomment-2536648501
<noamr> bramus: e.g. view-timeline-name. The ident() function together with attr(), can have those created in one go
<noamr> bramus: see examples
<noamr> bramus: e.g. combining the data-* attribute with other strings
<noamr> bramus: another example where view-transition-name is dynamically constructed from the enclosing element
<noamr> bramus: proposing an ident() function with space-separated items, can be string/integer/ident
<noamr> bramus: can you just use attr?
<noamr> bramus: sometimes you need to glue pieces together, some of which are attr and some not, e.g. strings
<noamr> bramus: sometimes you need to refer to that name, you can't always put it as an attribute-specific value like "view-transition-name: auto"
<kizu> +1, wanted this in 2016
<noamr> astearns: a bit concerned that stitching together ident() really depends on the context in which the function is being used
<noamr> astearns: I can see myself being confused about why my ident() is not matching because of not thinking of the context
<noamr> bramus: dev tools can definitely help with that, to find the generated values
<miriam> q+
<astearns> ack miriam
<noamr> bramus: like see a mapping of references to names in view-timeline etc
<noamr> miriam: to clarify, when creating idents like this in CSS, it generally means we have to refer to them somewhere else
<noamr> miriam: in most of these pieces it makes sense that you've created the name, and then you need to reference them
<noamr> q+
<noamr> bramus: in view-timeline, set on non-parent elements, each photo might have its view-timeline, but the dots might not be descendants and still refer to the images by name
<noamr> bramus: so e.g. the sibling-index() can be used with ident() to rebuild the ident()
<astearns> ack noamr
<bramus> noamr: one issue of this is css custom props wher eyou put on an an ancestor and then refer to that var
<bramus> … sometimes the attr you want is somewhere above and the participing element is somwheer down below
<noamr> bramus: similar to how the attr() behaves, they resolve before inherited
<noamr> bramus: one of the examples is where the cart stores an attribute into --id custom property, and down the line an ident() function uses that value
<bramus> s/how the attr() behaves/how the attr() behaves when used in a custom prop
<noamr> astearns: PROPOSED RESOLUTION that we resolve the feature
<noamr> fantasai: proposal should be to adopt to the css-values-5
<noamr> PROPOSED RESOLUTION: work on this issue in css-values-5 draft
<fantasai> PROPOSED: Adopt ident() function into css-values-5
<noamr> astearns: objection?
<fantasai> RESOLVED: Adopt ident() function into css-values-5