DEV Community

Cover image for Lets Build Web Components! Part 1: The Standards

Lets Build Web Components! Part 1: The Standards

Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ on September 18, 2018

Component-based UI is all the rage these days. In fact it's so established that people have even started retconning old-school jQuery widgets as "j...
Collapse
ย 
jochemstoel profile image
Jochem Stoel โ€ข

๐Ÿ˜€ Thanks for reading! ๐Ÿ˜

Thanks for writing. Interesting read and very well written. Looking forward to read part two.

I notice that you too use customElements.define and not document.registerElement. I have been wondering if registerElement is (to be) deprecated but never found a satisfying unopioniated answer.
Do you have any insight?

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

Exactly right! document.registerElement is deprecated and shouldn't be used.

Collapse
ย 
jochemstoel profile image
Jochem Stoel โ€ข

@bennypowers I would like to show you document.defineElement/nativeEleme..., a polyfill for custom elements that does not require a hyphen (-) in the HTML tag. It has been a while now since I created that repository but today I added the nativeElements.define() method because you confirmed that registerElement is deprecated in 2018.

Obviously I am aware that this is generally a bad idea to be doing but perhaps you appreciate it nonetheless.

Thread Thread
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข
Bad ideas completely overwrite the element and break things make iframes or script tags stop working

๐Ÿคฃ

Collapse
ย 
lasalefamine profile image
Alessio Occhipinti โ€ข โ€ข Edited

One of the best article on Web Components both for content and clarity. Really great work @bennypowers !

And looking forward for the 2nd part, of course ๐Ÿ”ฅ

Collapse
ย 
gabuardi profile image
Josuรฉ Gabuardi โ€ข โ€ข Edited

Awesome!
totally awesome.. man, I think that this serie of articles are GOLD.

I have researching for web component and web component libraries, I first read the 3 first articles then I start do research in other sites.. after a time... the researching brought me here again. What I mean is that your articles right now are a high quality resource for web components info since what the people usually know about it is almost anything so is difficult to found something like this.

Thanks very much to share!

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

Thanks for the kind words ๐Ÿ˜

Collapse
ย 
kawaiidora profile image
ๅ“†ๅ•ฆ๏ผˆDora๏ผ‰ โ€ข

Nice article! Using template.innerHTML are quite hacky and cannot make use of IDE or Editor's features. Is there any way (e.g. load html file) to replace template.innerHTML method in your super-span.js after HTML-import are deprecated?

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

I think you'd like to check out lit-element. I have a whole article on it in part 5 of this series.

Collapse
ย 
teej profile image
TJ Fogarty โ€ข

Great post, and a great resource in general. I'll be referring to this a lot when I'm trying to learn more. Really looking forward to the next one :)

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

Thanks, TJ :D

Collapse
ย 
david_j_eddy profile image
David J Eddy โ€ข

This article has restored my faith in the Javascript ecosystem. Thank you Benny.

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

That's what we came here for :)

Glad you enjoyed it.

Collapse
ย 
kresnasatya profile image
Kresna Satya โ€ข

Excellent example you bring about web components. Awesome!

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

Thank you, Satya!
Please enjoy the other posts in the series.

Collapse
ย 
paramsinghvc profile image
Parminder Singh โ€ข

Here's an example of implementing config driven UI elements using custom components.
medium.com/@paramsingh_66174/gener...

Collapse
ย 
stefant123 profile image
StefanT123 โ€ข

One of the best posts I've read. Very well explained. It will be a nice future reference. Can't wait for the next one.

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

Thank you! ๐Ÿ˜

Collapse
ย 
crapkit profile image
Patrick Nemenz โ€ข โ€ข Edited

Thanks, Benny!

This post really opened my eyes on how far the standard has come since the old days of Polymer. Much like your write-up, the syntax is finally clear and concise.

Collapse
ย 
adityasridhar profile image
Aditya Sridhar โ€ข

Really great article. Thanks for taking the time to write this :)

Collapse
ย 
phnx311 profile image
Eric Chi โ€ข

Hi Benny I've never seen the element method .content from const

clone = template.content.cloneNode(true);

Is this new or just something thats always been available?

Collapse
ย 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ โ€ข

Hey Eric thanks for dropping by ๐Ÿป

content is a read-only property on HTMLTemplateElement that's not provided on other elements. It's purpose is to give you a handle on the template's contents as a DocumentFragment, so that you can clone it.