-
Notifications
You must be signed in to change notification settings - Fork 756
Open
Labels
Description
(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:
- The HTML spec recommends using this pattern, to specify the language of
<code>elements (and it has been a huge pain to handle in Prism). - Tailwind, Bootstrap and other frameworks use classes like
pt-6orspace-y-4to specify their styles - Icon families like FontAwesome and Bootstrap Icons use utility classes like
fa-*/bi-*respectively to specify their icons (in this case the prefix does double duty: it serves as both the key and a namespace). - Authors often create such utility classes of their own.
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:
- Enumeration of all the possibilities in the CSS, when they are finite. Manually (and painfully) when they are few, with build tools when they are dozens. This includes both CSS rules for the specific values of each utility class, as well as exceedingly long selector lists for the commonalities among them.
- Pushing the friction to authors by asking them to use one class for the commonalities and one for the specific values. E.g. to use a Bootstrap Icons icon, authors need to use both a
biclass and abi-iconnameclass. - This is not an option for libraries, but authors (who have more control of their markup) may do things like
[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:
- Extending class selectors to support wildcards. It appears that
.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. - Introducing a new attribute selector, that is basically a combination of
~=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.
castastrophe and omidantilong