Back in 2015, I was in the midst of learning my first front-end framework -- AngularJS. The way I thought of it was that I was building my own HTML...
For further actions, you may consider blocking this person and/or reporting abuse
I did a hello world in web components a couple of years ago, but haven't touched them since. Thanks for the refresher!
I'd just make one suggestion about adding event listeners. This is not web component specific, but for adding event listeners in general. In the case of the
rainbow-text
component, the number of<span />
s increases for every additional letter in thetext
attribute, so the number of event listeners per instance of the component is n letters * 2 (mouse over + animation end events).You can end up with a lot of events very quickly just for one instance of the component. What you can do is add an event listener for each event type on the parent
<div />
you create in aspittel/rainbow-word-webcomponent and then the power of event bubbling is your friend.e.g.
Ah thank you for spotting that!
Thanks for the intro to Web Components!
I'm curious how the browser knows what to render when it decides to "connect" your component. It seems like, from the naming,
connectedCallback
is called after the Dom expects something to exist, so there's a period of time while your function runs that there is nothing (not even a container) rendered? Or is that less acomponentDidMount
and more of acomponentWillMount
?Also, is there any sort of poly fill or transplanting to get this in older browsers...or even just some automatic graceful degradation (like in your try..catch block).
Thanks again! This was very clear and straightforward intro!
Awesome! Polymer is the best way to create web components that are cross-browser compatible. The API is somewhat different, but they are much more usable!
connectedCallback
is called when the custom element is first connected to the document's DOM (according to the MDN documentation). So the container mounts to the DOM, thenconnectedCallback
is triggered.connectedCallback
is called every time, when an element is attached to document, so also when you move an element from one parent to another. MDN definition:(developer.mozilla.org/en-US/docs/W...)
WebReflection on GitHub has a cross browser compatible polyfill of document.registerElement
Hi, thanks for this clear post.
Note that when running (Chrome 70), it says
[Deprecation] Element.createShadowRoot is deprecated
and will be removed in M73, around March 2019.
Please use Element.attachShadow instead.
Perhpas you may update the code (I couldnt ...)
works with
I love your articles and your website is cool.
Very nice example of creating web components using only browser APIs :) If I may, in your full code example is few missing things:
this.createShadowRoot()
is not defined, and it should be resistant to multiple calls (connectedCallback
can be called multiple times)this.render()
method. It appends newdiv
, so every time it will be called (and it is called inconnectedCallback
), new div is appended to theshadowRoot
. I assume, you should clearshadowRoot
firstly (you can callthis.shadowRoot.innerHTML = ''
).attributeChangedCallback
, but the code example is not using it, so changing attributes does not re-render component (Also then you should use static computed propertystatic get observedAttributes()
- someone already wrote about it in a comment here)Using raw APIs is really cool because we don't need any external tools for creating web components. However, then you and only you are responsible for matching standards, which ensure your custom element will work and will be usable :)
Did you consider using a 6kb library with an absolutely simple and unique API for building web components?
More or less, your example could be written like this (using hybrids library):
I know, that your whole idea was to not use libraries, but after all, they give you a solid abstraction on top of web APIs and ensures, that components work as expected.
Hi, I wrote and article about the real scope of web components, which is extend the HTML.
You can read more about this point of view, that is also shared by Google Web Fundamentals
dev.to/clabuxd/web-componentsthe-r...
This can help with writing DOM components in Vanilla JS:
github.com/vitaly-t/excellent
It simply turns all your DOM code into components, for better reusability, isolation, and instantiation patterns.
Safari already has web components
webkit.org/blog/4096/introducing-s...
webkit.org/blog/7027/introducing-c...
(I didn't try Shadow DOM yet, but at least native custom elements works fine to me)
Oh cool! I was just going off of the MDN docs -- I don't really use Safari! Thanks for the heads up!
Have you tried any of the other callbacks? I can't seem to get the attributeChangedCallback to fire.
For performance reasons, the browsers require you to define the list of attributes you want to listen to. You can do that by defining a static method called observedAttributes, which returns an array:
That'll cause attributeChangedCallback to fire for changes to either of the listed attributes.
Thanks a lot. I did get it to work with a setter but i still wanted to know how the callback works. I'll try it out when I can.
I assume that we will need to define the getStyle method, right?
OK, I found it in the complete example...
You can use a CSS .header span instead of a .letter class for each span.
Awesome!! This is the clearest writeup I've read of what web components actually do - thanks for the excellent run-through :D
This is such a great article! It serves as a very gentle intro to web components which I actually wanted to try for a while. Great job, Ali!