How to create a Web Component using Vanilla JS

  Рет қаралды 67,533

A shot of code

A shot of code

Күн бұрын

Пікірлер: 102
@bmehder
@bmehder 4 жыл бұрын
I've been viewing several web component crash courses lately, and I find this video to be the most clear and easy to understand. Thank you for doing this!
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Thanks Brad! glad it was of use
@yurr7408
@yurr7408 4 жыл бұрын
Great video. Many people over-complicate very quickly, but this was concise quick and easy to understand!
@theoligarchist1503
@theoligarchist1503 3 жыл бұрын
good pace, good voice , good accent , and most importantly Great font size, 👍
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thank you! 😃
@BaruchoSan
@BaruchoSan 2 жыл бұрын
This true teacher is a five star. Congratullations.
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Thanks! Glad was useful Bernardo 😀
@maksimsergeevich5939
@maksimsergeevich5939 2 жыл бұрын
Great! You have reinvented the ... framework/library.
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Indeed! 😁
@EricRohlfs
@EricRohlfs 2 жыл бұрын
Nice video, very succinct into to the topic. I especially liked using getters and setters for working with attributes. The one one assertion you make that I would challenge is performance. We have found that our custom elements are much faster than our previous vue.js and react implementations. I 100% agree they are more verbose and as a team there are more decisions to be made such as unbundled or bundled.
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Fair point thanks Eric - as long as your render is performant then very true I think. Cheers Mark
@jptech__
@jptech__ 4 жыл бұрын
Good job! A very precise and concise explanation, you rock!
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Thank you kindly!
@wheelbegood
@wheelbegood 4 жыл бұрын
BIG CONGRATS - that was a very informative and interesting video!! I just begin to learn JS and it makes me even more want to learn it from the ground up!
@samuelhuang4324
@samuelhuang4324 4 жыл бұрын
You have been a real life saver!! Thank you so much. Subscribed!
@tr1f4ek
@tr1f4ek 4 жыл бұрын
The API is just INSANE!
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Indeed!
@sazaraki
@sazaraki 3 жыл бұрын
Excellent info to get a foundation. I understand how they work inside, now I just need a how to use them outside 😁
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it was helpful Sazaraki! Cheers Mark :-)
@gideon8648
@gideon8648 3 жыл бұрын
Oh man, this has been really useful. Thank you for sharing!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it was helpful Gideon :-)
@realraven2000
@realraven2000 3 жыл бұрын
this may come in handy. i am converting a legacy Add-on (FiltaQuilla) for Thunderbird 78 and need to remove remaining xbl bindings - it ain't easy. Add-ons usually don't use frameworks so i need to keep it pure JS (unless Mozilla has a framework) moving away from XUL to HTML is hard as we loose a lot of good functionality that needs to be reimplemented.
@paulohsgoes1959
@paulohsgoes1959 3 жыл бұрын
Oh, man! You're the best! You've just gotten a new subscriber.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks Paulo :-)
@mithatakbulut6490
@mithatakbulut6490 2 жыл бұрын
Love it! Crystal clear
@Ashotofcode
@Ashotofcode Жыл бұрын
Thanks Mithat🙂
@alexanderwhillas6431
@alexanderwhillas6431 Жыл бұрын
So what is a better way to handle the innerHTML thing?
@Ashotofcode
@Ashotofcode Жыл бұрын
Lit 🙂
@maungmaungtar9651
@maungmaungtar9651 4 жыл бұрын
Great video, Thanks.
@sau002
@sau002 4 жыл бұрын
Nice video. at 3:25 you are writing the body of the render method. The Counter is not encapsulated in double quotes. Why is that?
@Ashotofcode
@Ashotofcode 4 жыл бұрын
hiya - this is all enclosed within backticks (you can see on the line above it ends with a backtick). So this allows us to have a multiline string and also use interpolation (the part with ${this.count) )for variables compared to using double quotes where we have to use a plus to add them in and then reopen the quotes again. Cheers Mark
@yan-fz8pl
@yan-fz8pl 2 ай бұрын
great video! I'm wondering, why would be better to use some framework rather than sticking with the vanillajs and web components? i thought that actually the vanilla way would present better performance as it has less levels of abstraction (and dependencies). kinda more "low level"
@Ashotofcode
@Ashotofcode 2 ай бұрын
Good question! Mainly speed of development and not reinventing the wheel I would say are why I use LitElement. Cheers Mark
@alexon2010
@alexon2010 4 жыл бұрын
So, I was a bit disappointed with react ssr, I decided to investigate how I could make a PWA 100% Javascript Vanilla with WebComponent and Shadow dom, so you would have any suggestions where I could start to organize and structure a 100% Javascript Vanilla application for a PWA?
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Alexon, it's a tricky one for sure, so far I've stuck with the frameworks and only looked at vanilla conceptually, so can't give much help here afraid. Cheers Mark
@GottZ
@GottZ 4 жыл бұрын
I'd suggest wrapping document.createElement with a function that allows you to do stuff like: const scrollTop = ce("div.scrollTop", document.body, { style: {position: "fixed", bottom: 0, left: 0, willChange: transform}, click(e) { window.scrollTop = 0; } }) i made it so.. if you don't know how, I'll show you the.. dunno.. 30 lines i think. this would allow you to create html elements without relying on innerHTML and would ensure you stay in control of your elements. if you construct a dom through that, you don't even need to use stuff like querySelector.
@maartensegers5687
@maartensegers5687 8 ай бұрын
How would you publish this as a package to npmjs?
@tedfitzpatrickyt
@tedfitzpatrickyt 3 жыл бұрын
gotta look more into shadow dom, are we able to blacklist most css but selectively pass specific rules (like font)?
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Hi Ted, by default everything is blacklisted and can't penetrate into DOM. We can whitelist though by using CSS variables, and Shadow Parts are 2 options. I've a couple of videos on these :-)
@lnuemelieboss7762
@lnuemelieboss7762 2 жыл бұрын
This was great!!
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Thanks :-)
@alexon2010
@alexon2010 3 жыл бұрын
onde eu posso pegar o seu condigo de exemplo.... para ver se funciona, se é algum erro de digitação... faz todo processo e não atualiza na tela,
@mimosveta
@mimosveta 2 жыл бұрын
not familiar with HTMLelement, so this might be a dumb question, but is this doable functionally? I have snobbish disdain for oop, I know, I'm the worst
@vedant6460
@vedant6460 Жыл бұрын
Thanks for the video
@Ashotofcode
@Ashotofcode Жыл бұрын
No problem! Cheers Mark
@nicholasbraveson1173
@nicholasbraveson1173 3 жыл бұрын
how do we apply styles to our component?
@Ashotofcode
@Ashotofcode 3 жыл бұрын
We can add to the style header within the component is one way as the component author. To apply a theme from outside the component, e.g. as a user of the component then we are looking at things like custom properties ( as these can pierce the ShadowDom ) or CSS Shadow Parts can do this for us also - but not standard across browsers yet I think. Cheers Mark
@Mari_Selalu_Berbuat_Kebaikan
@Mari_Selalu_Berbuat_Kebaikan 3 ай бұрын
Let's always do alot of good
@Ashotofcode
@Ashotofcode 2 ай бұрын
Cheers!
@DioArsya
@DioArsya 3 жыл бұрын
wow thank you!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
No problem Dio 😊Cheers Mark
@NiyazKassenov
@NiyazKassenov 5 ай бұрын
gem
@Ashotofcode
@Ashotofcode 5 ай бұрын
Thanks :-)
@shubhamlatiyan7972
@shubhamlatiyan7972 4 жыл бұрын
Hai, very nice video can you link the code for this video
@Ashotofcode
@Ashotofcode 4 жыл бұрын
D'oh sorry think that code is gone now
@alexon2010
@alexon2010 3 жыл бұрын
@TesterAnimal1
@TesterAnimal1 8 ай бұрын
As opposed to strawberry JS? 😂
@edwardmacnab354
@edwardmacnab354 Жыл бұрын
another mess
@Ashotofcode
@Ashotofcode Жыл бұрын
Could be!
@dangerousInfo9
@dangerousInfo9 4 жыл бұрын
Thanks I was lucky to run into this (it's rare to see things like this.-it's always about framework marketing that put people on the shoulders of giants and make them move from ground up again, when the giant falls). For the eventListner, maybe creating another function and calling it inside this.render, just after inserting the html would have been better. What you've just explained seem to be the core concept behind these frameworks.
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Darlington, thanks, glad it was helpful, the render function yes that might be cool :-) Cheers Mark
@adriannuske
@adriannuske 2 жыл бұрын
with due respect, I don't understand the hustle. This is how you code in a formal language. It's not gonna be slow. It's not gonna be difficult to maintain if you architect it properly in a modular way. It's way easier to understand what is happening, for nothing is hidden, and as you build your app you'll end up writing less and less code for every new component. Yes, It'll look like a "framework" of your own, but it's gonna be simpler because it's gonna be tailored to your needs and nothing will be there "just in case you need it". I've written big apps in vanilla js starting before jqery ever existed, and no problem whatsoever. The thing with the frameworks and libraries (which I use as well, following the industry common uses) is to have people writing code over a pattern not really having to know much. Great video BTW! cheers.
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Good points well made thanks Adrian 😀👍 Cheers Mark
@HadarsGrasp
@HadarsGrasp Жыл бұрын
What do you recommend reading about and learning instead of innerHTML?
@Ashotofcode
@Ashotofcode Жыл бұрын
hiya - my go to is Lit (lit.dev/) when it comes to web components. They use Tagged Template Literals to ensure good performance. Worth checking out for sure.
@ravendark3258
@ravendark3258 4 жыл бұрын
At 2:45 you are talking about problems caused by using innerHTML, and it should not be used in complex applications. So how would it be implemented in the much more efficient way? What would you recommend?
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Raven, that takes us down the path towards something like a Virtual DOM which React and Vue use, or Tagged template literals which LitElement uses if we're looking for the best implementation. We basically start to reinvent these if looking to improve on innerHTML. These allow us to update just the parts that have changed rather than refreshing the whole DOM of our component.
@flexitheelectron3724
@flexitheelectron3724 4 жыл бұрын
@Raven Dark Lit-html is very efficient and can be easily integrated into vanilla JS
@webosm6494
@webosm6494 4 жыл бұрын
Actually it is pretty easy. You put the count inside a or any other suitable element. Get a reference to it. Then instead of rendering the innerHTML of the component you just change the innerHTML of the . Normally you use the this.shadow.innerHTML only one time. Then attach listeners to the elements you need and references to the elements you want to change. Have a look at codepen.io/webosm/pen/QWNeVyg for something similar and usefull as a little template.
@lronSausage
@lronSausage 4 жыл бұрын
While i don't disagree libraries can make things more efficient by taking care of the micromanagement for you (that's what libraries/frameworks do after all), I'm not sure if you are intentionally writing this super long just to get your point across. 1. Why do you need shadow root for this example? 2. If you need to access count and intend to use the attribute as your source of truth, why go thru so many hoops with so many life cycle methods and getters and setters? just just use this.getAttribute('count') every time you need it, no need to observe it and then set it to another variable. This is way over complicated. 3. And the click listener.... a. instead of just querySelector('#btn').addEventListener... you had to make it 2 lines by putting it to a variable and then var.addEventlistener. b. The worst thing you did, calling it after this.render() and then repeat it again in attributeChanges. Dude, just call it once INSIDE the render function after you set the innerHTML. c. You don't have to bind it if you just call it inside an anonymous arrow function. React class components have the same issue, this has nothing to do with web components. You can write the same functionality with a lot less code even without any library. This is exaggerated. You made this harder than it needs to be.
@vrjb100
@vrjb100 3 жыл бұрын
Web components only exists in plain javascript. That's the main advantage. No need for any immature framework, that evolves too quickly, with code breaking changes every 6 months. Browsers won't support typescript.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Fair point thanks 😀
@kristofs8893
@kristofs8893 18 күн бұрын
I tried to do two-way databindig and use it with my own Image Resizer API, that's where I decided to abandon the project. 😂 You can't just sit down and write something like that. You need to plan out how and where the state will be updated and kept. On top of that maintaining listeners becomes a nightmare very quickly if you have wrapper objects around DOM like I did. From that point on syncing state is one thing then you have to maintain events and listeners. 😂 On top of that doing OOP in JS is such a hassle. I spent most of my time d*ck*ng around with setter and getter properties. Absolute nightmare, but I learnt what I shouldn't spend time on. 😅
@edgeeffect
@edgeeffect 7 ай бұрын
Could you not have created the button as a single element in `render` and create a property for `button` at the same time - which means there's no need for `querySelector`?
@fabinSilvas
@fabinSilvas 4 жыл бұрын
I really liked the video, could you make a video explaining a little more about shadow dom?
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Fernando, I'm looking to make one on the new declarative shadow dom for sure soon 😎
@mvg4398
@mvg4398 4 жыл бұрын
This is really awesome thank you so much!
@lone_wolf2453
@lone_wolf2453 Жыл бұрын
please pardon me if this sounds very basic however I failed to understand why was an attribute used to hold the variable instead of an html tag ? I was left wondering if this was a limitation of web components or if this is the correct way to do this and why ? Also can these custom components hold value or text as they very much looks like normal html tag. So do they behave like normal html tags.?.. Once again pardon if these are basic questions as I just started learning js. I appreciate the content provided and simplicity through which the concept was explained since I was able grasp most it despite being a beginner.
@leandrew2725
@leandrew2725 Жыл бұрын
I've been looking for the definition of Shadow Root DOM and examples to use it. But this video is one of the most clear and easy to understand. Thank you for doing this!
@Ashotofcode
@Ashotofcode Жыл бұрын
Awesome, thank you! Cheers Mark 🙂
@livb4139
@livb4139 3 жыл бұрын
Thanks for the video. I got a question if you don't mind. 13:37 Why is it that when it's inner HTML you can't add event handler directly on the html?
@Ashotofcode
@Ashotofcode 3 жыл бұрын
hiya - I don't think innerHTML gets parsed the same as on page load and the events are not noticed as such. Cheers Mark
@aram5642
@aram5642 3 жыл бұрын
Thumb up. Although the missing part here is disconnectedCallback and removeEventListener :)
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks - yes those are important for sure :-)
@DevMeloy
@DevMeloy Жыл бұрын
I love web components, wish they would catch on!
@Ashotofcode
@Ashotofcode Жыл бұрын
Yeah one day maybe.. Cheers Mark
@codelightsparkles2403
@codelightsparkles2403 3 жыл бұрын
amazing video and really easy to follow, I am surprised you don't have more subscribers
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad you liked it! Cheers Mark
@camn-bv3vq
@camn-bv3vq 4 жыл бұрын
Can I create web component using TypeScript? (no frameworks), just like with Vanilla as you have done
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Carlos, for sure yes, the typescript will simply compile into javascript and then you are good to go with no framework in sight :-)
@camn-bv3vq
@camn-bv3vq 4 жыл бұрын
@@Ashotofcode thanks, I'll try it out again. The last time it didn't work. I thought elementRef was not working on typescript as works in vanilla. I have thought to use something like polymer or stencil
@MagnusAnand
@MagnusAnand 3 жыл бұрын
Much easier with AlpineJS 😜
@Ashotofcode
@Ashotofcode 3 жыл бұрын
For sure :-)
@lucasdevoliveira4943
@lucasdevoliveira4943 3 жыл бұрын
i liked your explained, congrat
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks Lucas! 😃
@alexon2010
@alexon2010 3 жыл бұрын
Até que atuliza, mas não faz o render novamente....
@ThiagoLucioBittencourt
@ThiagoLucioBittencourt 4 жыл бұрын
Where the css? Great vídeo!
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Thiago, no css in this one to keep it simple, but if we added it would slot into the innerHtml with a style tag is one option. Cheers Mark
@techwithshudarsan559
@techwithshudarsan559 3 жыл бұрын
I didn't get anything.!!
How to create a WebComponent in ANY Framework
18:44
A shot of code
Рет қаралды 2,3 М.
Can you build a web app with vanilla Web Components in 2024?!?
30:16
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 9 МЛН
Running With Bigger And Bigger Lunchlys
00:18
MrBeast
Рет қаралды 104 МЛН
Man Mocks Wife's Exercise Routine, Faces Embarrassment at Work #shorts
00:32
Fabiosa Best Lifehacks
Рет қаралды 5 МЛН
Building a REAL Web Component from Scratch! - Tutorial
35:51
DesignCourse
Рет қаралды 53 М.
You don't need a frontend framework
15:45
Andrew Schmelyun
Рет қаралды 126 М.
Web Components Crash Course
28:54
Traversy Media
Рет қаралды 255 М.
Custom Web Component Behaviours and Events
23:25
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 8 М.
Modern Lit tutorial
27:05
Jad Joubran
Рет қаралды 13 М.
Custom Events Dispatched from Web Components
20:32
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 2 М.
Vanilla JS Single Page Application Routes | # or URL  - #88
33:38
JavaScript Pro Tips - Code This, NOT That
12:37
Fireship
Рет қаралды 2,5 МЛН
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 9 МЛН