A JavaScript-free custom element implementation (#note)

Some people love custom elements, and it seems that even more people are entirely against them. And while I’m leaning towards using them, today I learned a fascinating custom elements take.
I’ve been reading one of Adam Argyle’s exc…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Some people love custom elements, and it seems that even more people are entirely against them. And while I'm leaning towards using them, today I learned a fascinating custom elements take.

I've been reading one of Adam Argyle's excellent component walk-throughs, and Adam shared a tool-tip custom element that works without the usual customElements.define JavaScript instruction.

To quote Adam:

You could think of a custom element like a classname with less specificity.

That's an interesting take because it changes the mental model around custom elements! Custom elements are usually used to encapsulate functionality as web components with JavaScript, but one could also use them as another style hook with lower specificity than class selectors. Neat!

Use Polypane's specificity calculator if you want to learn more about CSS specificity.

And to make the custom element accessible, Adam's example tool-tip component receives its semantic meaning via ARIA...

<tool-tip role="tooltip">A tooltip</tool-tip>

... and the show/hide functionality is then added via CSS has(). 😲

/* Only show the custom element when its parent is hovered, focused, or activated */
:has(> tool-tip):is(:hover, :focus-visible, :active) > tool-tip {
 opacity: 1;
 transition-delay: 200ms;
}

Very smart!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-10-28T22:00:00+00:00) A JavaScript-free custom element implementation (#note). Retrieved from https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/

MLA
" » A JavaScript-free custom element implementation (#note)." Stefan Judis | Sciencx - Friday October 28, 2022, https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/
HARVARD
Stefan Judis | Sciencx Friday October 28, 2022 » A JavaScript-free custom element implementation (#note)., viewed ,<https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/>
VANCOUVER
Stefan Judis | Sciencx - » A JavaScript-free custom element implementation (#note). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/
CHICAGO
" » A JavaScript-free custom element implementation (#note)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/
IEEE
" » A JavaScript-free custom element implementation (#note)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/. [Accessed: ]
rf:citation
» A JavaScript-free custom element implementation (#note) | Stefan Judis | Sciencx | https://www.scien.cx/2022/10/28/a-javascript-free-custom-element-implementation-note/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.