DEV Community

𒎏Wii 🏳️‍⚧️
𒎏Wii 🏳️‍⚧️

Posted on

CSS needs a name prefix selector

I love working with custom elements for all kinds of things, but sometimes I just want to hide stuff until it is loaded or do something else with it in CSS.

A simple solution looks like this:

framework-button:not(:defined) {
   display: none
}
Enter fullscreen mode Exit fullscreen mode

Put that in a <style> tag in the HTML and the button will just appear once it's loaded.

But with larger frameworks, this becomes really annoying. Sometimes you can get away with simply selecting :not(:defined), but that's not always viable.

An obvious (at least to me) fix: CSS needs a name prefix selector, so you can just do this

framework-*:not(:defined) {
   display: none
}
Enter fullscreen mode Exit fullscreen mode

It's not like this would be an entirely new thing. We can already kind of do this with attribute selectors.

And I'm sure allowing splits only at - in an element's name would make it reasonably easy to implement this effectively in browsers too.

What do y'all think? Would this be useful? Are there easier solutions that already work?

Top comments (8)

Collapse
 
dannyengelman profile image
Danny Engelman • Edited

The is attribute is the safest attribute to do this after your Web Component sets it with this.is = this.localName (or set it yourself when you need it with :defined)
Because is is reserved for Customized Built-In Elements, which Apple/Safari will never support

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Yes, there's plenty of ways to hack this in; one could also just use a helper class like <framework-button class="hide-until-defined"></framework-button>, but the point is to achieve this in CSS.

Collapse
 
dannyengelman profile image
Danny Engelman

If there was a need it would have been implemented a long time ago.
Attribute selections are possible since IE7; they are a performance hit.

And yes, they are on my (MSCW) Would-Have list too,

so I can do <queen-of-hearts> instead of <playing-card is="queen-of-hearts">

But the latter works just fine, even more now the Custom Element can set the is value

Collapse
 
link2twenty profile image
Andrew Bone • Edited

I like it but I think something like

[tag^="framework-"]:not(:defined) {
  display: none;
}

// OR

:tag("^framework-"):not(:defined) {
  display: none;
}

Enter fullscreen mode Exit fullscreen mode

would be more consistent with what we already have.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

I don't particularly like [tag^="framework-"] because that's already a selector that checks a tag= attribute.

The :tag() rule looks a lot better imho. I'm not sure if it would be better to have a dedicated prefix selector that maybe browsers could optimise for, or just a generic tag name matching mechanism where you can do prefix, suffix, infix, or whatever you need for your project.

I honestly haven't put an awful amount of thought into this; it's really just a vague "I wish this was a thing" more than any serious proposal 🤭

In defense of framework-* as a selector:

  1. it looks like a node name selector, which it kind of is
  2. the * at the end is similar to the actual * selector

So it combines two concepts that people already know into something that, I hope, makes it easy to piece together what it's doing :)

Collapse
 
moopet profile image
Ben Sinclair

I wouldn't find it useful myself because I can't imagine using a framework that way.
That said, since you can select based on partial attributes already, it makes sense to me to allow selecting by partial tag names as well, if only for consistency.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I'm using "framework" very loosely there; if I wanted to be pedantic, "component library" would have probably been a more precise word to use ;)

Collapse
 
seanolad profile image
Sean

This is a cool idea.