How to create a Web Component using Vanilla JS

Поделиться
HTML-код
  • Опубликовано: 3 янв 2025

Комментарии • 103

  • @bmehder
    @bmehder 4 года назад +45

    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 года назад +4

      Thanks Brad! glad it was of use

  • @yurr7408
    @yurr7408 4 года назад +17

    Great video.
    Many people over-complicate very quickly, but this was concise quick and easy to understand!

  • @leandrew2725
    @leandrew2725 Год назад +2

    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 🙂

  • @BaruchoSan
    @BaruchoSan 3 года назад +1

    This true teacher is a five star.
    Congratullations.

    • @Ashotofcode
      @Ashotofcode  3 года назад

      Thanks! Glad was useful Bernardo 😀

  • @theoligarchist1503
    @theoligarchist1503 4 года назад +4

    good pace, good voice , good accent , and most importantly Great font size,
    👍

  • @maksimsergeevich5939
    @maksimsergeevich5939 2 года назад +3

    Great! You have reinvented the ... framework/library.

  • @wheelbegood
    @wheelbegood 4 года назад +3

    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!

  • @jptech__
    @jptech__ 4 года назад +4

    Good job! A very precise and concise explanation, you rock!

  • @UCcdTp7XpCkVLkaRCsDcifFg
    @UCcdTp7XpCkVLkaRCsDcifFg 22 дня назад +1

    we need more video like this

  • @codelightsparkles2403
    @codelightsparkles2403 3 года назад +1

    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

  • @EricRohlfs
    @EricRohlfs 2 года назад +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

  • @alexanderwhillas6431
    @alexanderwhillas6431 2 года назад +2

    So what is a better way to handle the innerHTML thing?

  • @ravendark3258
    @ravendark3258 4 года назад +3

    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 года назад +2

      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 года назад +1

      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.

  • @HadarsGrasp
    @HadarsGrasp Год назад +1

    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.

  • @samuelhuang4324
    @samuelhuang4324 4 года назад +4

    You have been a real life saver!! Thank you so much. Subscribed!

  • @tr1f4ek
    @tr1f4ek 4 года назад +2

    The API is just INSANE!

  • @paulohsgoes1959
    @paulohsgoes1959 4 года назад

    Oh, man! You're the best! You've just gotten a new subscriber.

  • @sazaraki
    @sazaraki 3 года назад +1

    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 :-)

  • @aram5642
    @aram5642 3 года назад +1

    Thumb up. Although the missing part here is disconnectedCallback and removeEventListener :)

    • @Ashotofcode
      @Ashotofcode  3 года назад

      Thanks - yes those are important for sure :-)

  • @gideon8648
    @gideon8648 3 года назад +1

    Oh man, this has been really useful. Thank you for sharing!

    • @Ashotofcode
      @Ashotofcode  3 года назад

      Glad it was helpful Gideon :-)

  • @yan-fz8pl
    @yan-fz8pl 5 месяцев назад +1

    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  5 месяцев назад +1

      Good question! Mainly speed of development and not reinventing the wheel I would say are why I use LitElement. Cheers Mark

  • @livb4139
    @livb4139 3 года назад +1

    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

  • @dangerousInfo9
    @dangerousInfo9 4 года назад +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

  • @tedfitzpatrickyt
    @tedfitzpatrickyt 3 года назад +1

    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 :-)

  • @mithatakbulut6490
    @mithatakbulut6490 2 года назад +1

    Love it! Crystal clear

  • @sau002
    @sau002 4 года назад +1

    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 года назад +2

      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

  • @alexon2010
    @alexon2010 4 года назад +2

    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 года назад +1

      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.

  • @realraven2000
    @realraven2000 4 года назад +1

    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.

  • @fabinSilvas
    @fabinSilvas 4 года назад +3

    I really liked the video, could you make a video explaining a little more about shadow dom?

    • @Ashotofcode
      @Ashotofcode  4 года назад +2

      Hi Fernando, I'm looking to make one on the new declarative shadow dom for sure soon 😎

  • @lucasdevoliveira4943
    @lucasdevoliveira4943 3 года назад +1

    i liked your explained, congrat

  • @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.

  • @edgeeffect
    @edgeeffect 10 месяцев назад

    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`?

  • @vedant6460
    @vedant6460 2 года назад +1

    Thanks for the video

  • @mvg4398
    @mvg4398 4 года назад +3

    This is really awesome thank you so much!

  • @maartensegers5687
    @maartensegers5687 Год назад

    How would you publish this as a package to npmjs?

  • @nicholasbraveson1173
    @nicholasbraveson1173 3 года назад +1

    how do we apply styles to our component?

    • @Ashotofcode
      @Ashotofcode  3 года назад +1

      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

  • @lnuemelieboss7762
    @lnuemelieboss7762 3 года назад +1

    This was great!!

  • @DevMeloy
    @DevMeloy Год назад +1

    I love web components, wish they would catch on!

    • @Ashotofcode
      @Ashotofcode  Год назад

      Yeah one day maybe.. Cheers Mark

  • @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,

  • @camn-bv3vq
    @camn-bv3vq 4 года назад +1

    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

  • @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

  • @maungmaungtar9651
    @maungmaungtar9651 4 года назад +1

    Great video, Thanks.

  • @ThiagoLucioBittencourt
    @ThiagoLucioBittencourt 4 года назад +1

    Where the css? Great vídeo!

    • @Ashotofcode
      @Ashotofcode  4 года назад +1

      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

  • @adriannuske
    @adriannuske 2 года назад +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

  • @DioArsya
    @DioArsya 3 года назад +1

    wow thank you!

    • @Ashotofcode
      @Ashotofcode  3 года назад

      No problem Dio 😊Cheers Mark

  • @shubhamlatiyan7972
    @shubhamlatiyan7972 4 года назад +1

    Hai, very nice video can you link the code for this video

    • @Ashotofcode
      @Ashotofcode  4 года назад +1

      D'oh sorry think that code is gone now

  • @vrjb100
    @vrjb100 3 года назад +2

    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.

  • @alexon2010
    @alexon2010 3 года назад

    Até que atuliza, mas não faz o render novamente....

  • @binaryeverest
    @binaryeverest 3 года назад

    I didn't get anything.!!

  • @Mari_Selalu_Berbuat_Kebaikan
    @Mari_Selalu_Berbuat_Kebaikan 6 месяцев назад +1

    Let's always do alot of good

  • @NiyazKassenov
    @NiyazKassenov 8 месяцев назад +1

    gem

  • @lronSausage
    @lronSausage 4 года назад +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.

  • @TesterAnimal1
    @TesterAnimal1 11 месяцев назад +1

    As opposed to strawberry JS? 😂

  • @alexon2010
    @alexon2010 3 года назад

  • @MagnusAnand
    @MagnusAnand 3 года назад +1

    Much easier with AlpineJS 😜

  • @edwardmacnab354
    @edwardmacnab354 2 года назад +1

    another mess