News for hackers. Stuff that matters.
Re:Web Components Eliminate JavaScript Framework Lock-In by itslennysfault on Monday November 27, 2023 @01:50PM (#38436458)
I like the idea of web components, but really dislike html and js being intermingled (the reason I don't like React/JSX). In web components its even worse because its HTML as a string which means it has no validation and isn't syntax aware. I much prefer the angular approach where you have separate css,html,ts files, and would love a web component framework that could work similarly.

Searching for this I found some kinda hacky solutions on Stack Overflow which use fetch to load html/css at runtime.

Re:Web Components Eliminate JavaScript Framework Lock-In by djrenren on Monday November 27, 2023 @02:07PM (#38436713)
Web Components are really just the minimal APIs required to allow the implementation of new HTML elements. This includes things like:

- Responding to being attached to the DOM

- Encapsulating DOM nodes (the Shadow DOM)

- Scoped styling (Shadow DOM + User defined stylesheets + CSS parts)

It is definitively not a templating engine. It doesn't provide any new APIs for creating DOM nodes, or mutating them a la React or handlebars or lit-html. Templating is basically the "next step up the stack". Using shadowDom.innerHTML = `...`; is basically a stand-in for having an actual templating engine.

There is work going on, developing a native templating system in the browser which may interest you. It's called the DOM Parts proposal and you can find info on it here: https://github.com/WICG/webcomponents/blob/gh-pages/proposal...

Re:Web Components Eliminate JavaScript Framework Lock-In by jacobsimon on Monday November 27, 2023 @02:14PM (#38436804)
I like React but I actually agree with you re: JSX, it helps me to remember that JSX is 'just javascript' - it's a syntactical shorthand for creating a component tree, which then happens to be rendered into HTML.
Re:Web Components Eliminate JavaScript Framework Lock-In by acheong08 on Monday November 27, 2023 @03:38PM (#38437910)
Rather than putting the HTML in JS, you can fetch HTML in the JS.

E.g. https://github.com/g-utils/WebComponentFactory

Re:Web Components Eliminate JavaScript Framework Lock-In by yohannparis on Monday November 27, 2023 @02:04PM (#38436675)
You can use `<template>` for that purpose and keep your separation of concerns okay.
Re:Web Components Eliminate JavaScript Framework Lock-In by MrDresden on Monday November 27, 2023 @02:09PM (#38436747)
As a non web dev I had to look this up and yeah, this looks like a very sane and clean way to separate concerns and get proper handling from IDEs:

https://developer.mozilla.org/en-US/docs/Web/API/Web_compone...

Re:Web Components Eliminate JavaScript Framework Lock-In by gitaarik on Tuesday November 28, 2023 @09:48AM (#38446297)
Indeed, the Web Components APIs don't demand that you put the HTML and JS together. You can use it and structure it to your own likes.
Re:Web Components Eliminate JavaScript Framework Lock-In by fuzzy2 on Monday November 27, 2023 @02:04PM (#38436670)
Then… use Angular Elements?

Using innerHTML isn’t specific to Web Components in any way. It was and is a quick & dirty way to dynamically insert HTML.

Re:Web Components Eliminate JavaScript Framework Lock-In by claytongulick on Monday November 27, 2023 @01:54PM (#38436530)
If you use a good rendering library, like lit-html then (at least in vscode) you get very nice syntax highlighting, completion and validation:

    html`
    <this is all syntax highlighted>
    `
Re:Web Components Eliminate JavaScript Framework Lock-In by itslennysfault on Monday November 27, 2023 @01:59PM (#38436609)
I was actually just looking at that after posting this (lit is new to me). That's a pretty cool solution. I'd still prefer an option to have the html in its own file, but that is just my taste. I know a lot of people feel exactly the opposite.
Re:Web Components Eliminate JavaScript Framework Lock-In by claytongulick on Monday November 27, 2023 @04:06PM (#38438277)
Maybe a bit pedantic - but I prefer using lit-html, not lit.

Lit is a bit too "frameworky" for my taste, I generally just use vanilla classes that extend HTMLElement and have a render function that renders the lit-element template to the light dom.

Something like:

    import {html, render} from 'lit-html';

    class AppComponent extends HTMLElement {
      connectedCallback() {
        this.template = () => html`<markup here>`;
        this.render();
      }
    
      render() {
        if(this.template)
          render(this.template(), this)
      }
    }

    customElements.define('app-component', AppComponent);
Re:Web Components Eliminate JavaScript Framework Lock-In by itslennysfault on Monday November 27, 2023 @06:35PM (#38440117)
Perfect, and perfect timing. I was recently tasked with creating a small standalone web app and everything feels like overkill to me. This might be the perfect fit. Thanks for the explanation.
Re:Web Components Eliminate JavaScript Framework Lock-In by paulddraper on Monday November 27, 2023 @01:53PM (#38436507)
> its HTML as a string

Can you explain what you mean by that?

Re:Web Components Eliminate JavaScript Framework Lock-In by itslennysfault on Monday November 27, 2023 @01:58PM (#38436590)
This is the example from the article, and pretty common practice for web components. As far as an IDE is concerned this is just a string of text (it doesn't know/care that it is html). This makes it more error prone as the IDE can't catch syntax errors in the html.

  connectedCallback() {
    this.shadow.innerHTML = `
      <p>Hello from a web component!</p>
      <style>
        p {
          color: pink;
          font-weight: bold;
          padding: 1rem;
          border: 4px solid pink;
        }
      </style>
    `;
  }
Re:Web Components Eliminate JavaScript Framework Lock-In by paulddraper on Monday November 27, 2023 @02:05PM (#38436680)
Well, you can use `innerHTML` and a string, but you don't have to.

But there several alternatives:

1. Use DOM APIs (createElement).

2. Fetch the HTML as a separate resource.

3. Have the HTML in a separate file and bundle it with a bundler.

4. Use React/JSX.

5. Use a preferred templating library of your choice.

You can do whichever of these you like the most

Re:Web Components Eliminate JavaScript Framework Lock-In by thfuran on Monday November 27, 2023 @01:54PM (#38436537)
Html as a string literal in code rather than the html being in a separate .html file.
Re:Web Components Eliminate JavaScript Framework Lock-In by IshKebab on Monday November 27, 2023 @01:54PM (#38436526)
React uses JSX/TSX which is much better because it is syntax checked and even type checked in the case of TSX.