-
Notifications
You must be signed in to change notification settings - Fork 679
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
[selectors] New selector to facilitate utility classes ([name~^=value]
? .prefix-*
?)
#10001
Comments
If I understand you are saying that libraries cant do |
I think that can be confusing because
I would go this way. I imagine a combination of |
I am strongly in favor of more powerful tools for attribute selectors but I do wonder if the use case is valid. Encoding a lot of key/value pairs in class names seems like the wrong approach. Maybe because attributes are supposed to have a
Styling the commonalities then becomes trivial with |
There is #1010, but that is about regular expressions and element selectors, not simpler wildcard class selectors, which were also not covered in #354. For what it’s worth, I believe utility classes for atomic design tokens – i.e. usually with a name that roughly matches its single style rule like That is partially why I suggested, in #3714, to introduce something like this: $fg-green {color: var(--my-green);}
$bg-white {background: white;} which could then be applied verbosely in HTML as <a style="@include $fg-green $bg-white">dev</a> and if that is found to establish a common pattern, it would be moved back to CSS: .pattern {@include $fg-green $bg-white;} and the HTML could become more readable and semantic again. <a class="pattern">prod</a> Applied to one of the initial examples: $bi {/*commonalities*/}
.bi-iconname {
@include $bi;
--icon: url(icons.svg#name);
} instead of the now suggested .bi-* {/*commonalities*/}
.bi-iconname {
--icon: url(icons.svg#name);
} |
@Crissov This looks like custom mixins (#9350) So IIUC wildcard class selectors and custom mixins are basically solving the same problem. I'm also wondering if a selector that targets a group of class names / attribute names will make implementations significantly more complicated. For example, Blink maintains maps keyed by class names for efficient style matching and invalidation, and it will be much more complicated if we have to eg put a trie there for prefix matching. Mixins solve the same use cases without affecting style matching and invalidation. So +1 to mixins over wildcard selectors. |
I cannot speak on behalf of every author, but I suspect the reasons are multi-fold:
The fact that authors are willing to go through the pain of currently using this pattern instead of using That said, being able to target attributes based on a prefix of their name is also needed, though less frequently. @Crissov |
@xiaochengh, like I said there, it’s mostly a syntax choice (disregarding benefits for JS). To really improve the icon use case, you’d probably want something like this instead: .iconname {
--icon: src(concat("icons.svg#", selector(classname)));
} |
@Crissov having a function to get the part after the class name and string concatenation are both very useful in their own right, but without a way to target elements with a given class prefix where would you apply this? In your code example you use |
I’d expect a very long list of selectors. Also, Anyway, that’s sidetracking from the original issue, so I should probably raise a new one. |
That's a good argument. I take my opposition back. This is like an inheritance vs. composition debate. Normally I prefer composition, but in CSS verbosity is a much bigger concern, as more code means more network usage. |
In terms of syntax, I just had a discussion with @fantasai about how we actually need to be able to target prefixes across several types of selectors, not just classes:
Therefore, I think using a wildcard, despite its unfortunate similarity with the universal selector, is the way to go here. |
I think we need to consider other options than the wildcard for elements, classes, I am concerned about a couple of things:
I think that having the absolute shortest notation for the selector isn't that critical here. If possible without too much friction I think we should pursue this. |
I'm not convinced.
Perfectly covered by
We could add something for this, e.g. :attr(^= foo-) /* Matches if there is an attribute whose name starts with foo- */
:attr(^= foo-, ^= bar-) /* Matches if there is an attribute whose name starts with foo-
and whose value starts with bar- */ By using |
Would reverse spread syntax work? .foo-... {bar: baz;} |
Seçicilerde eksik yanlar var iken böyle bir değişiklik ertelenmelidir. Örneğin aşağıdaki gibi değerleri CSS'nin şimdiki seçicilerini kullanarak daha az kod ile çözemiyoruz. Translated with Google TranslateSuch a change should be postponed while there are shortcomings in the selectors. For example, we cannot solve values like the ones below with less code using CSS's current selectors. <div class="ml-10px mr-15px"></div> [class*="ml-1"]{
--ml-xx = 1;
}
[class*="0px"]{
--ml-x = 0;
}
[class*="5px"]{
--ml-x = 5;
}
[class*="ml-"]{
margin-left: calc( var(--ml-xx) * 10 + var(--ml-x) )px;
} Olmalı (Veya daha iyisi olmalı)Translated with Google TranslateShould be (Or better yet should be)<div class="ml-10px mr-15px"></div> [class*="ml-1*"]{
--ml-xx = 1;
}
[class*="ml-*0*"]{
--ml-x = 0;
}
[class*="ml-*5*"]{
--ml-x = 5;
}
[class*="ml-**px"]{
margin-left: calc( var(--ml-xx) * 10 + var(--ml-x) )px;
} Not: yukarıdaki örnekte 0-99 arası değerler yaklaşık 20 satır kod ile işletilebilir. Bununla ilgili bir yenilik gelmese de kendimce çözümlerim var. Translated with Google TranslateNote: in the example above, values between 0-99 can be processed with approximately 20 lines of code. Even though there is no innovation regarding this, I have my own solutions. Like ="ml10ml:px"... |
@volkantash Apologies, but the CSSWG conducts its work in English, and I don't want to trust auto translation to understand your contribution. |
Searching through code as plain text will always yield all sorts of things unless you get creative with regexes. It's up to IDEs to provide contextual lookup relevant to the language and help developers navigate their code.
What's the source of this? Maths seems to be doing fine for hundreds of years now with
I'm really tired of seeing this "people new to CSS will have hard time understanding things" argument. It's really not that difficult to teach someone that there's a difference between
There were a lot of tools that assumed that |
hidden
I never said such thing. Please be careful to not attribute hostile wording to others. I said that the proposed syntax would have traits that make it harder to learn. I never made any statement about persons learning CSS. The aspects I listed are not some veto against the proposed syntax or feature. These aspects are also minor or short term pains, but still worth mentioning. |
I've quoted your exact words intact, I have no idea what attribution of hostility you're referring to. Of course it's not a veto. I don't think anyone is seeing it as such, no need to worry. It's a public discussion which we're all contributing to. |
@lazarljubenovic I agree with @romainmenke here. You are the one who introduced not smart enough to this discussion, and it was not accurate or necessary. Please take more care in paraphrasing other people’s contributions. |
Thanks for the feedback. I've changed it to avoid further derailing the topic. (I don't understand the difference between "not smart enough", "has hard time understanding", or "has difficulties acquiring knowledge" - it's all just different ways to express the exact same thing. Would be great if we could focus on any reason why we believe or don't believe that's the case.) If I understand that shorter doesn't necessarily mean easier to understand. I don't understand why this particular case is harder to understand. I understand that too many special characters in one spot can get hard to understand. I don't understand why adding a star is harder to understand. Are you aware of any studies conducted that could translate to this case? Do you have any anecdotal evidence? I've been teaching people CSS for years now and people generally don't have difficulties to grasp the syntax of selectors one bit, until |
The difference is in the subject. Saying that something is harder to learn is only a statement about the thing itself. It isn't a statement about any individual or any group of people. I definitely didn't want to say or imply anything negative about people who are learning CSS :) I might be looking at the proposed A wildcard selector as it exists today selects and represents an actual element that can be of any type. Whereas This reminds me a bit of pseudo element selectors which should have had a proper combinator, but don't. |
Lots of conjecture about what is hard for beginners being thrown around 🙃 From a usability pov, it's not the density of symbols that hinders understanding, it's the recognizability. The same applies to programming languages: ASCII characters are their "icons". That’s why Often this is intuitively understood by language designers, but the specifics of what exactly is widely recognized by the target audience is the source of many a debates (e.g. for my previous example, I'm sure many would argue On to this particular case, I would argue using a wildcard to mean "any string of symbols" is a very widespread convention. It extends beyond programming languages as well, as it's a common convention in many searching GUIs. On the contrary, strings of symbols like Also, whether beginners can understand the difference between |
Yeah, I think way too much weight is being assigned to some of the items I listed here : #10001 (comment) :) In particular That statement assumes that there is a perceived difference for CSS authors. My concern here was that there would be a set of rules and behaviors for universal selectors and a different set for wildcards. If they align, then this is moot. As I finished that comment, I am actually in favor of figuring out how to make
Yup, easily testable :) As you also say, |
Herhangi bir seçicide daha iyi seçiş yapabilmek için düşündüğüm yöntem şöyle: (The method I think of to make a better selection in any selector is as follows:) cssdiv[class^="abc" + * + "z"] {
background: #ff0000;
}
div[class$="abc" + * + "z"] {
background: #aa0000;
}
div[class*="abc" + ** + "z"] {
background: #bbbb00;
}
div[class*="abc" + 2* + "z"] {
background: #ccc333;
}
div[class*="abc" + 2** + "z"] {
background: #6789ab;
} html<div class="abcde"></div>
<div class="abcdexyz"></div>
<div class="abcdz"></div>
<div class="abcdez"></div>
<div class="abcdefghz"></div> Kodları okuyarak anlayamıyorsanız ayrıntılı olarak anlatabilirim. (If you cannot understand by reading the codes, I can explain in detail.) |
Just remembered that even the HTML spec recommends using this pattern, to specify the language of |
(I’d be surprised if this has not been proposed before, but I was not able to find anything)
Pain point
It is very very common to use classes with a shared prefix as to specify key-value pairs. For example:
<code>
elements (and it has been a huge pain to handle in Prism).pt-6
orspace-y-4
to specify their stylesfa-*
/bi-*
respectively to specify their icons (in this case the prefix does double duty: it serves as both the key and a namespace).These utility classes have commonalities shared across all instances, as well as declarations that are specific to the value used. Usually, these are implemented in one of three ways:
bi
class and abi-iconname
class.[class^="prefix-"], [class*=" prefix-"]
None of these are ideal. It seems pretty straightforward to provide the capability these use cases really need, so this seems like a a quick win.
While the vast majority of use cases are around class names, I could see something like this being useful for all attributes that take a space-separated list of values.
Potential solutions
I see two options:
.foo-*
is actually not currently valid, so it seems like a natural choice, especially as it extends nicely to other types of wildcard selectors such as element names, attribute names, ids, etc.~=
and^=
. Options for the operator could be~^=
,^~=
, or even^~
if we want to keep it to a max of 2 characters. This is quicker to implement, but very specific to attribute selectors, and far less intuitive.The text was updated successfully, but these errors were encountered: