HTML Web Components
18:47
2 ай бұрын
Episode 141 - My obsidian setup
10:19
Episode 134: The craft of the web
1:56
Episode 133: Learning JavaScript
14:40
The problem with SPAs (they suck!)
4:17
Пікірлер
@frumbert
@frumbert 3 сағат бұрын
You could build a tic tac toe using html, no javascript. Lots of files though, one for each state the board could be in. Clicking a cell links to the hpertext file representing the new state. Inherent history/undo as well. That way it could even work on the Lynx browser.
@terryweb
@terryweb 9 сағат бұрын
Amazing tricks, except #13... 😀
@IainSimmons
@IainSimmons Күн бұрын
Great video! I'd love to see more of these types of videos, comparing JS framework code to regular light DOM HTML web components! One thing about your app logic... Can you not just use the fact that the next move alternates between X and O and whether the index of the history is odd or even to determine what it should be? And can you just remove the list items and history with an index higher than the clicked one, in handleJumpTo?
@kanaillaurent526
@kanaillaurent526 Күн бұрын
Thank you I like that. This is how I work too with webcomponents: TDD on the browser when the webcomponent is not very complicated. Also I used to store the state in the DOM too but I've changed my method because I've founded that it's more complicated in a complex application. Now i store the state of the whole application in a centralized object (a little bit like redux does). Unfortunately I don't have the observation (subscribe and dispatch) mecanism yet in place but I'm working on it.
@svivian
@svivian 2 күн бұрын
“I like to use const for everything” *immediately changes const to let* 😆
@chritical_ep
@chritical_ep 29 минут бұрын
He might have misspoke, I remember in one newsletter he said he used let for everything, so he might have got them switched up
@dovh49
@dovh49 3 күн бұрын
Van JS is a good alternative too. I was thinking for the history creating a square custom element and then storing all that information in there and then looping through the custom elements to update any history. Like `square.removeHistory(3)` Where 3 would be the history index. And when adding a value you can do `square.setValue('X', historyIndex)`. Something like that. Then you have a different class handling a lot of the things.
@terrencemoore8739
@terrencemoore8739 3 күн бұрын
Amazing video idea. I always wondered how different making custom web components are to react implementations
@terrencemoore8739
@terrencemoore8739 2 күн бұрын
Just tried making a web component without using the shadow dom, it worked great!
@SuicideDog
@SuicideDog 3 күн бұрын
im starting out in development. done a 2 year course on front-end where a lot of focus was on react. i feel like there are a lot of basic javascript knowledge that has flown over my head by transitioning over to react too quickly. I really like this type of content. because by comparing and practicing vanilla. It at least feels like i get a deeper understanding on how the language work, and how different programming logic and approaches can be applied. so super thankful:)
@cloudpuncher4615
@cloudpuncher4615 3 күн бұрын
How did you write the Custom Element innerHTML in the constructor? I get an error unless I attachShadow and write the shadowRoot innerHTML....
@dovh49
@dovh49 3 күн бұрын
The source code is in the description. Maybe you're doing something that is a little off?
@davidluhr
@davidluhr 4 күн бұрын
This is a great demonstration of the value of writing vanilla JavaScript: When you write framework code, you improve your skills in the framework. When you write vanilla JavaScript, you improve your skills in JavaScript as a whole. Sure, you use some vanilla JavaScript in React, but it's a narrower slice. Even if you create solutions that can React solve in its own way, your solution is tailored to your specific needs and context, and you gain more transferable knowledge along the way.
@k16e
@k16e 4 күн бұрын
JavaScript owes you, mentor. Then so does the community. Thanks for keeping me in check.
@bourge
@bourge 7 күн бұрын
I ran into problems when i tried to use a vanilla WC in a Next.js. Turns out SSR apps are not to friendly to WCs. Are there any thing we can do to solve this?
@gomakethings
@gomakethings 4 күн бұрын
I'd need more specifics about the errors you're seeing to answer that.
@bourge
@bourge 4 күн бұрын
@@gomakethings There are flash effects on every page loads. This is only for Nextjs apparently due to the way it handles hydration of the DOM.
@Alex-BSD
@Alex-BSD 8 күн бұрын
The CV says "senior" but the code has "document.getElementById"
@FlorianSchommertz
@FlorianSchommertz 8 күн бұрын
Am I correct that not using shadow DOM forces you to run the script after the DOM has loaded?
@gomakethings
@gomakethings 4 күн бұрын
No, that's just how DOM manipulation works in general. It's not specific to the shadow DOM (or lack thereof). If you target elements that don't exist yet, you'll get errors.
@tubeta028
@tubeta028 9 күн бұрын
I like this, good video thanks
@gomakethings
@gomakethings 4 күн бұрын
Thanks!
@YoungLink51423
@YoungLink51423 15 күн бұрын
Starting this series now
@ba8e
@ba8e 17 күн бұрын
Why shuffle the array when you can just get a random index? array[Math.floor(Math.random() * array.length)]
@gomakethings
@gomakethings 4 күн бұрын
Historically, the Fisher-Yates shuffle produced more random results than a simple Math.random(), which tends to skew towards the middle over time. MDN used to have details about this on their site, but they're not longer there.
@CristianKirk
@CristianKirk 19 күн бұрын
Thanks a lot. This series is being very useful and it's hard to believe there are no more channels teaching about web components..
@gomakethings
@gomakethings 4 күн бұрын
Thank you!
@pl4gueis
@pl4gueis 19 күн бұрын
"Developer ergonomics and ease of authoring code" holds true for the first 5 minutes of developing react. Once you add all the hooks, router, redux, component library, jss its all over. Adding abstractions to fix the horrible abstractions until you have a mess that is most react projects.
@gomakethings
@gomakethings 4 күн бұрын
Couldn't agree more!
@damvaz
@damvaz 19 күн бұрын
Great content!!! Ideas for new contents? What about "making a new simple design system" (framework agnostic) using web componentes and css best practices?
@gomakethings
@gomakethings 4 күн бұрын
That's a great suggestion, thanks!
@evilspymace209
@evilspymace209 19 күн бұрын
i'm guilty for using web components as an alternative to vue. right now i'm rewriting a SPA. i wanted to avoid the excessive tooling and ever changing apis. honestly it's so much more fun!
@pl4gueis
@pl4gueis 20 күн бұрын
Great series. How would I go about writing tests for these web components?
@gomakethings
@gomakethings 4 күн бұрын
Same way you'd test any DOM manipulation component. Jest is well suited to it, I think.
@BenHewart
@BenHewart 20 күн бұрын
I think its great that a youtube channel is finally doing native web components! I love it... and love web components but I think I disagree with the shadow dom being an anti pattern and styling is way easier being encapsulated... components are mean't to be self contained so they can be used through out any project without clashes with global code. if you want global styling the you simply create a global style sheet and use custom properties ( or css variables ) Global style sheet: :root { --bg-blue: ##4187f7; } component styles: <style> background-color: var(bg-blue); </style> Another issue mixing light and shadow dom is CLS obviously light dom will load first loading content first and then loads in shadom dom after this causing jumping, as far as I can see its better to stick to the shadom dom or the light not to mix them.... otherwise you will have to constantly reserve space for the shadow dom Open if anyone has any other solutions? Still learning all the awesomeness of web components thanks and keep up the great work!
@gomakethings
@gomakethings 4 күн бұрын
I think this is a symptom of thinking about Web Components as React components rather than their own, unique thing. React has poisoned the web well. gomakethings.com/html-web-components/
@BenHewart
@BenHewart 4 күн бұрын
Oh I totally agree that react has ruined the web.... I am massive on core web vitals and have built a web app using only native web components with html CSS and vanilla js.... That scores 100 across the board... The problem I found and if you have any suggestions is that if you mix the light Dom and the shadow dom it causes CLS... So the only way was to use web components that are self contained.
@gomakethings
@gomakethings 4 күн бұрын
@@BenHewart Forgive me, but what does CLS stand for?
@BenHewart
@BenHewart 4 күн бұрын
CLS (Cumulative Layout Shift) measures visual stability in Core Web Vitals. It quantifies how much elements on a page move unexpectedly as it loads. High CLS means a poor user experience, as content shifting can disrupt interactions. As the light Dom loads first and then native web components it can make elements jump about as the page loads hence I went for a full shadow dom component to fix this
@BenHewart
@BenHewart 4 күн бұрын
Happy to show you if your interested no pressure 😉
@rustyprogrammer
@rustyprogrammer 20 күн бұрын
Thanks, very clear :-). I wonder why, in the count component, you inherit from the HTMLElement and not directly from HTMLButtonElement?
@gomakethings
@gomakethings 20 күн бұрын
This comes up a lot! There are two reasons: 1. The Web Component itself isn't a button. One of it's child elements is. If I wanted the custom element itself to behave like a button, I'd also need to add stuff like a `role` attribute and additional event listeners. 2. That's also unfortunately not how Web Components work. You extend the HTMLElement, then layer in your custom behavior. You can't reliably extend other element types with customElements.define(). There was a spec in the works that would let you extend native elements with custom functionality using `is` attribute. Firefox and Chromium moved forward with it, and then the WebKit team straight up refused to implement it, effectively killing the spec. developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/is My understanding is that there's a replacement spec in the works that WebKit is allegedly onboard with, but I don't know much about it at this time as it's nowhere near production ready.
@etherweb6796
@etherweb6796 22 күн бұрын
The answer is yes
@coderlicious6565
@coderlicious6565 25 күн бұрын
Do you think this is a good way to go, using a cloudflare serverless function, if you're creating a chrome extension that calls the Notion api ( but notion doesn't allow you to make calls from the extension ). I need something somewhere on a paid server, that receives some text data, and then makes the call to notion api which makes notion happy. I was also looking at supabase for their serverless function capability.
@gomakethings
@gomakethings 20 күн бұрын
Could be! Could get expensive for a public extension, of course. The whole benefit of "serverless" is that you don't have to worry about maintaining the server, just the function (or functions) that run on it.
@kodejohan
@kodejohan 25 күн бұрын
Very interesting approach!
@gomakethings
@gomakethings 25 күн бұрын
Thanks!
@sonnyl2915
@sonnyl2915 26 күн бұрын
Luv it! Question: I've got troubled running into swapping elements issue. How do you have all the elements within the web-component readily in order for execution by the DOM? Sometimes in the console window said undefined or not found?
@gomakethings
@gomakethings 25 күн бұрын
I wrote a little bit about this a few weeks ago: gomakethings.com/the-different-ways-to-instantiate-a-web-component/ If you have a specific example in a code pen or GitHub repo you'd like me to look at, though, that works too!
@cmilne10
@cmilne10 26 күн бұрын
great series. Informative and simple
@gomakethings
@gomakethings 25 күн бұрын
Cheers!
@terhuneb
@terhuneb 26 күн бұрын
Thank you for mentioning an ES Module implementation too.
@gomakethings
@gomakethings 25 күн бұрын
You're welcome!
@dirtdart81
@dirtdart81 26 күн бұрын
Really enjoying this series! I'm not new to programming, but new to web/js and it is an interesting time to be learning. As flavor of the week frameworks go in and out, this kind of 'back-to-basics' feels timely. Thanks for the videos!
@gomakethings
@gomakethings 25 күн бұрын
You're welcome! If you have any questions along the way, feel free to ask!
@jonathanoden6854
@jonathanoden6854 28 күн бұрын
Hello, thank you very much for the work you are doing in sharing good practices on how the web could be done! Web components didn't clicked for me until I saw your daily tips. I had this bad feeling using shadow dom and kind of full javascript components. Now, with 'enhanced html elements' it feels really nice and deeply connected to html and css. I see here you are using attributes without 'data-' prefix, is there a reason for that? Again, thanks a lot!
@gomakethings
@gomakethings 27 күн бұрын
They're invalid, and I should probably stop doing it. In my mind, if I'm creating a custom element, I like to use custom attributes with it. An HTML validator will complain about them, but CSS and JavaScript will work with them just fine. For safety, I could also add a dash to the names to ensure no conflicts with future attribute names. I lean heavily on attributes in my web components, and having to write data-* constantly is fatiguing, IMO.
@NeilMerton
@NeilMerton Ай бұрын
I’m enjoying the series, thanks for putting these together 👍 Do you have any plans on showing option on how to encapsulate the web component code within the component itself and how that then gets consumed by the HTML? I hope this makes sense.
@gomakethings
@gomakethings Ай бұрын
Thanks! Can you clarify what you mean by this: "ow to encapsulate the web component code within the component itself and how that then gets consumed by the HTML?" I'd be delighted to make a vide about if, once I better understand what you're asking.
@NeilMerton
@NeilMerton Ай бұрын
Sure. What I see in these two videos is you’ve got the ‘pick-at-random.html’ file that contains ‘pick-at-random’ element and below that the JavaScript to make it work. How is this then reusable? For example, I’d like to use it on a different page, how would I go about doing that. Let me know if you’d like me to elaborate further.
@gomakethings
@gomakethings Ай бұрын
@@NeilMerton Yep, that helps a lot. Thanks Neil!
@netssrmrz
@netssrmrz Ай бұрын
The event management feels clumsy. Do you have or could you do a video covering it in more detail? As in pros and cons compared to other methods. I'm wondering if I should change my approach.
@gomakethings
@gomakethings Ай бұрын
That's a good suggestion, thanks!
@NicklasIsraelsson
@NicklasIsraelsson Ай бұрын
Thanks for sharing! Very nice. Looking forward to the next session. One thing I would like to see is the thing at the end you briefly mentioned. How would you "upgrade" this to use the dialog element instead of confirm?
@gomakethings
@gomakethings Ай бұрын
GREAT suggestion! Let me add that to the list!
@sanderlissenburg1608
@sanderlissenburg1608 Ай бұрын
It's better to use the connectedCallback instead of the constructor. I ran into an issue that the javascript was loaded before the dom was loaded/ready. This is because I load the javascript in the head instead of the bottom of the page. I know I know I should change that. Either way I think using the connectedCallback is still better.
@gomakethings
@gomakethings Ай бұрын
"Better" isn't the word you're looking for. "Can prevent issues in certain contexts." The problem you're describing can still happen even when using connectedCallback. There are a bunch of strategies to fix it. But in your case, DOM scripts should be loaded in the footer or use async/defer or be wrapped in a DOMContentLoaded event. What you're describing would happen if you were using plain old DOM manipulation methods, too. More on all of that here: gomakethings.com/the-different-ways-to-instantiate-a-web-component/
@sanderlissenburg1608
@sanderlissenburg1608 Ай бұрын
Thnx for the reply and extra info!
@aniltheblogger
@aniltheblogger Ай бұрын
what about lit framework?
@gomakethings
@gomakethings Ай бұрын
I don't like it, personally: gomakethings.com/what-about-lit-for-web-components/
@netssrmrz
@netssrmrz Ай бұрын
Good video. IMO a couple of mistakes though. One, the spec insists we do NOT add/remove elements in the constructor. Two, persistence (localstorage) should be handled externally to the component.
@gomakethings
@gomakethings Ай бұрын
Regarding the spec: it doesn't actually say that, though most people (including me, previously) interpreted it that way. "The specification recommends that, as far as possible, developers should implement custom element setup in this callback rather than the constructor." Does setup mean add/remove elements? Attach events? Something else? It's not clear. Hawk Ticehurst has a great article on this: hawkticehurst.com/writing/you-are-probably-using-connectedcallback-wrong/ What I do know is that setting up elements in connectedCallback is a bad idea, because it can lead to the same elements being rendered multiple times if an element is, for example, moved with the append() method. You can prevent it by using special .loaded properties or whatever, but just doing it once in the constructor() is, IMO, a lot more bulletproof and simple.
@gomakethings
@gomakethings Ай бұрын
Regarding localStorage, there are many ways to handle it. Outside the component (with some event to trigger the behavior) is one possible solution. Calling it a mistake is disingenous, though. What you really mean is "I would have approached this differently." That's fine, of course. The best and worst thing about JS is that there's always more than one way to do a thing. But the approach I used in this unscripted first pass isn't a mistake. It's just not what you would have done.
@netssrmrz
@netssrmrz Ай бұрын
@@gomakethings re constructor: Spec seems very clear to me. MDN: "In the class constructor... you should not add new attributes or children". HTML Standard 4.13.2: "When authoring custom element constructors... The element must not gain attributes or children." And you can prevent double loading by using methods like replaceChidren(). Hawk's post is about event listeners and not rendering child elements.
@netssrmrz
@netssrmrz Ай бұрын
@@gomakethings re localstorage: I'm not going to sugarcoat my take for a grown man. In my opinion, it is a mistake to couple this UI component to a persistence mechanism (you know why, I know why, everyone knows why). Granted your solution is passable as a first draft for educational purposes but it can be easily improved, and should be. In my opinion.
@gomakethings
@gomakethings Ай бұрын
@@netssrmrz I'm not asking you to "sugarcoat" anything. But let's be clear: your opinion is not the same thing as fact. You disliking something doesn't make it wrong. Senior developers constantly and arrogantly insisting something non-obvious is obvious is, frankly, why I have students. I'm asking you to engage in polite discussion. If you want to do the techbro reply guy thing, this isn't the right channel for you. This is the only time I'm going to say it. Next time, you get blocked.
@davidluhr
@davidluhr Ай бұрын
Completely agree on treating the DOM as the state of the application. It's so much simpler and the available DOM APIs are powerful. My personal favorite is using the `append` method. If the appended element is new, it'll add it to the DOM. If it already exists, it'll automatically move it into the new position with no additional work needed.
@gomakethings
@gomakethings Ай бұрын
I obviously had to keep going into the DOM a fair bit for this one, but case studies like this reinforce that it's the better approach for me: gomakethings.com/removing-react-dramatically-improves-performance-a-case-study/
@CristianKirk
@CristianKirk Ай бұрын
Great stuff, thanks a lot!
@gomakethings
@gomakethings Ай бұрын
Thank you for the kind words!
@sueaustin6261
@sueaustin6261 Ай бұрын
would adding a title to the button svg serve as accessibility? i've heard to not mess with aria unless you really know what you're doing.
@gomakethings
@gomakethings Ай бұрын
In theory, yes, that should be enough. In reality, it's not. 😞 Many screen reader/browser combinations will NOT announce the <title> unless you add an aria-labeledby attribute to the SVG, with a value that points to the ID of the <title>. In the end, since you're messing with ARIA anyways, aria-label is the least complicated way to do that.
@davidluhr
@davidluhr Ай бұрын
@@gomakethings Only downside of `aria-label` is it isn't translatable. For buttons and links, I stick with visually hidden text, which I'm fine appearing if CSS doesn't load. For landmark elements, I use `aria-labelledby` and point it to the corresponding section heading.
@gomakethings
@gomakethings Ай бұрын
@@davidluhr Agreed! I've had accessibility experts argue both approaches to me over the years. For this particular project, I went with the easiest one to implement, but probably could have discussed the tradeoffs a bit more.
@PDSRoxas
@PDSRoxas Ай бұрын
Thank you, thank you, thank you for including aria and accessibility stuff. So many tutorials omit this, and it’s frustrating. Accessibility stuff should be built into codes and projects from the beginning.
@gomakethings
@gomakethings Ай бұрын
Couldn't agree more! It's sad that this isn't the norm in dev education.
@tithos
@tithos Ай бұрын
I know you are The vanilla JS guy, but you teach me more about ARIA that anyone. THANKS
@gomakethings
@gomakethings Ай бұрын
I'm both delighted and saddened to hear that! Glad you appreciate, but a huge bummer that most tech educators never cover it.
@HeribertSchuetz
@HeribertSchuetz Ай бұрын
Are you planning to use the shuffled array for further functionality? Currently it feels like overkill. It should suffice to just use a random index to pick an item. The `shuffle` method could be removed and the body of the `onclick` method would just become let items = this.list.querySelectorAll('li'); let item = items[Math.floor(Math.random() * items.length)]; this.result.textContent = `You picked ${item.textContent}`;
@gomakethings
@gomakethings Ай бұрын
I could do that, yea. Historically, `Math.random()` has provided a less uniformly random shuffle than the Fisher-Yates algorithm. I'm not worried about the 473 extra bytes, personally.
@HeribertSchuetz
@HeribertSchuetz Ай бұрын
@@gomakethings I wasn't worried about the extra bytes, at least not primarily. Sometimes 20%+ extra code might well be worthwhile if the code becomes more readable. But my impression was that the added complexity makes the code harder to read for people who will have to maintain it or, in this particular case, for your audience. And regarding Fisher-Yates: It may well be true that Fisher-Yates is better than other shuffling methods. But here no shuffling is needed. (And BTW, the shuffle method uses Math.random() as well.) Finally, please don't get me wrong if I appear to be nit-picking. I like your videos and I just wanted to help improving. (Unfortunately youtube has no versioning support...)
@gomakethings
@gomakethings Ай бұрын
@@HeribertSchuetz I took this as a kindly discussion among peers, so no worries at all! I agree about making the code harder to read.
@patriciodelboca7898
@patriciodelboca7898 Ай бұрын
I'm extremely excited about Web Components! Thanks for the tutorial! It feels fresh, straight to the point and clear!
@gomakethings
@gomakethings Ай бұрын
Thank you so much for the kind words!
@muhaimincs
@muhaimincs Ай бұрын
How do you organize the code? How do you load the data? How to handle racing condition? How do you handle state of components without shadow dom? How about you just rephrase this YT video title?
@gomakethings
@gomakethings Ай бұрын
1. I addressed state directly in this video already. 2. As I mentioned IN THE VIDEO, there's a part two coming that will cover loading the data. 3. There's no race conditions to worry about in this simple app. 4. This isn't a video about organizing code. 5. How about I just leave the title as is.
@cb73
@cb73 Ай бұрын
I’m not sure how you can say the shadow dom is an anti-pattern - whatever that means. I would think the reason why most devs reach for web components, myself included, is to make our components usable on other peoples websites, the sites we have zero control over. I work for a company that does personalization and it’s critical that our components look and feel are not at all affected by the host site - and vice versa. I don’t think this is an edge case, it’s the promise web components offer.
@gomakethings
@gomakethings Ай бұрын
The Shadow DOM... 1. Adds additional complexity to the Web Component authoring experience. 2. Can introduce accessibility issues. 3. Makes components much harder to style. 4. Prevents the component from taking advantage of global styles. 5. Is often a solution in search of a problem. 6. Tries to let WC's do things "the React way," but React does those things better.
@netssrmrz
@netssrmrz Ай бұрын
I agree with GoMakeThings. I've worked on several WC projects at various companies including building component libraries and 90% of the time we've needed to deliver highly customisable components. From my experience Shadow DOM feels like an edge case. Anyway, once you have "open" components its trivial to wrap them in a closed Shadow DOM version.
@cb73
@cb73 Ай бұрын
The very first paragraph on the web components documentation page immediately highlights encapsulation as its primary feature. I think you’re just trying to sound controversial by calling it an anti-pattern. It’s fine if you don’t prefer to use it that way but please don’t tell your audience they are doing it wrong. Do they make things more complex? Maybe but compared to what? Not all complexity is bad. It depends on the context.
@gomakethings
@gomakethings Ай бұрын
@@cb73 Nope. I've built a LOT of Web Components. The benefits of the Shadow DOM, in my experience, are grossly outweighed by its many disadvantages. Isn't it strange how Web Components have been around for years, but only really started taking off once a bunch of folks started saying, "Hey, you don't actually need the Shadow DOM"?
@netssrmrz
@netssrmrz Ай бұрын
Nice video. 100% agree on shadow dom usage.
@pragmasoft
@pragmasoft Ай бұрын
Shadow dom is not all black or white. It very much depends on the functionality of your web components. Web components scope is not limited to only wrapping forms and inputs, even if in majority of cases it is.
@netssrmrz
@netssrmrz Ай бұрын
@@pragmasoft I've been using Web Components for several years (to build full apps ;) and I agree with "Go Make Things" that shadow DOM functionality is only required in edge cases, for the same reasons he stated. Unfortunately, most tutorials overcomplicate things by appearing to make shadow DOM mandatory.
@rupen42
@rupen42 Ай бұрын
I'd love to watch this but I can't look at a white screen for 30 minutes
@gomakethings
@gomakethings Ай бұрын
Sorry about that! I use a white background for my videos because it has better contrast and is thus a better choice for folks with certain vision issues. I do recognize the white can be intense, though.
@d.o.nmuzic3802
@d.o.nmuzic3802 Ай бұрын
😂 100%!
@uiuxaidesign
@uiuxaidesign Ай бұрын
that's so cool
@gomakethings
@gomakethings Ай бұрын
Thank you!