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
}
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
}
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)
The
is
attribute is the safest attribute to do this after your Web Component sets it withthis.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 supportYes, 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.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
valueI like it but I think something like
would be more consistent with what we already have.
I don't particularly like
[tag^="framework-"]
because that's already a selector that checks atag=
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:*
at the end is similar to the actual*
selectorSo it combines two concepts that people already know into something that, I hope, makes it easy to piece together what it's doing :)
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.
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 ;)
This is a cool idea.