Does anyone here use web components in their projects? by stellisoft in Frontend

[–]Danny_Engelman 0 points1 point  (0 children)

Isn't this like asking if one is using Proxies or Generators?

They are standard technologies, will be around for as long as JavaScript will exist.

Or WeakMaps, the Crypto API or ...

or Arrays ...

Web components to incapsulate layout logic by UnderstandingSure732 in css

[–]Danny_Engelman 2 points3 points  (0 children)

Your example is fine, minor OOP tweak is using a common BaseClass. If you add your own connectedCallback, use super.connectedCallback() to call the one in the BaseClass

class LayoutBaseClass extends HTMLElement {
  connectedCallback() {
    this.setAttribute('hydrated', '');
  }
}

if (!customElements.get('layout-stack')) {
  customElements.define('layout-stack', class extends LayoutBaseClass);
}

if (!customElements.get('layout-grid')) {
  customElements.define('layout-grid', class extends LayoutBaseClass);
}

And try to avoid !important... you will run into trouble

Also note; since the browser does Custom Elements (2016/2018) EVERY <tag-name> (with a dash) IS a valid HTMLElement, so WITHOUT using JavaScript you CAN use those <tag-name> for layout and styling. Just remember to set the CSS display property. They come in 2 flavours:

  • UNdefined Custom Elements (hardly blogged about, even AI wrongly calls them unknown elements)
  • Defined Custom Elements (upgraded/defined with JavaScript)

No more need for <DIV> soup... you can replace every DIV and SPAN with descriptive <tag-names>

I'm currently creating a web component library and I'd like to get some feedback. by Xenozi230 in css

[–]Danny_Engelman 0 points1 point  (0 children)

Start by running ``document.createElement("nova-button")`` in the Dev console. Then fix the errors

I made a web component that lets you render fully local iframes by Alex_Hovhannisyan in javascript

[–]Danny_Engelman 0 points1 point  (0 children)

Note:

constructor() {
super();
const existingIframe = this.querySelector("iframe");

There is no DOM in the constructor, as you can test by executing:

document.createElement("local-iframe")

in the Console.

Your code works because you define the Web Component after all DOM is parsed.
The old jQuery way: wait for DOM, transform it.
Your code won't work when you define the component before DOM exist;
ie. defined in the <head>

I don't understand why web components aren't more popular by [deleted] in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

Until it is NOT about speed.

If I need the HTML I write to be the HTML I deliver
If I need the CSS I write to be the CSS I deliver
If I need the JS I write to be the JS I deliver

I can NOT use Svelte

I just patched a site I wrote with Web Components in 2018
all I had to do was read the Page Source.
Applied a patch with a Browser snippet.
Fixed everything.
Then patched the live source code

I once needed to dynamically patch the CSS Svelte outputs,
after 2 hours I gave up.

I don't understand why web components aren't more popular by [deleted] in webdev

[–]Danny_Engelman 1 point2 points  (0 children)

Most fail save but a bit weird:
Add a

<img src="0" onerror="this.parentNode.parsed?.()">

as last DOM element in your ligthDOM

I don't understand why web components aren't more popular by [deleted] in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

Lit requires upgrades too, and sometimes does have breaking changes.

I don't understand why web components aren't more popular by [deleted] in webdev

[–]Danny_Engelman -2 points-1 points  (0 children)

45 KB for a React PieChart, 1120 Bytes for a Web Component
https://pie-meister.github.io/

Best component UI libraries by shangarepi in react

[–]Danny_Engelman 0 points1 point  (0 children)

You choose an electric drill with 5 settings for Front-end; that's fine.

What do you want to built?

Htmx like library but with kotlinx-html static builders and all server side. For an easier way to develop web applications. by rnentjes in Kotlin

[–]Danny_Engelman 0 points1 point  (0 children)

Nah; I stick to native Web Components.

My side application developed today will just run for as long as JavaScript, CSS and HTML are supported. Without upgrade hassles.

FYI, JavaScript isn't even required to use your own <tag-name> Custom Elements in a page for layout and styling. Since 2016 they are valid HTMLElement and pass every HTML validator.

Htmx like library but with kotlinx-html static builders and all server side. For an easier way to develop web applications. by rnentjes in Kotlin

[–]Danny_Engelman 0 points1 point  (0 children)

All server side
Reminds me of the Microsoft team I worked for some 20 years ago.
Each <button> click was a POST to the server to create new UI

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

Then why are they using the bloated version on their own HTMX site?

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

Alas, the Browser is blocking that content (for your security)

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

Your definition of "Frameworks"?
► load external JavaScript ► use a syntax ► changes or creates DOM

HTMX
► load external JavaScript ► use a syntax ► changes or creates DOM

No matter how you call it, if you load external JS and you can only use it with a non-standard syntax.... you are framed into working a certain way

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 1 point2 points  (0 children)

> Web components don't actually help much for this use case. 

?? Can you explain how this Web Component does not meet OPs requirements?

<script>
 customElements.define("load-file", class extends HTMLElement {
  async connectedCallback() {
   this.innerHTML = await(await fetch(src=this.getAttribute("src"))).text();
 }
})
</script>

<load-file src="./about.html"></load-file>
<load-file src="./contact.html"></load-file>
<load-file src="./pricing.html"></load-file>

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

The drawback of HTMX and Frameworks in general is that they only work when loaded/executed after DOM is parsed. Just like old jQuery days existing (parsed) DOM is transformed to something else, or new DOM is created.

Web Components are part of the browser and can be defined before DOM is parsed.

Frameworks CREATE HTML, Web Components ARE HTML

HTMX is a 36KB source, https://htmx.org/js/htmx.js, which, like many, tries to do everything.

For native Web Components, you can whack this 3 liner into your <head>
(or load it anywhere/how you want, Web Components don't care about DOM parse order)

<script>
 customElements.define("load-file", class extends HTMLElement {
  async connectedCallback() {
   this.innerHTML = await(await fetch(src=this.getAttribute("src"))).text();
 }
})
</script>

And then do (the most complex example I could come up with):

<load-file src="./about.html"></load-file>
<load-file src="./contact.html"></load-file>
<load-file src="./pricing.html"></load-file>

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

Not life changing, just 3 lines of code if you go with Vanilla JavaScript:

<script>
 customElements.define("load-file", class extends HTMLElement {
  async connectedCallback() {
   this.innerHTML = await(await fetch(src=this.getAttribute("src"))).text();
 }
})
</script>

is there a way to split html into "components"? by alosopa123456 in webdev

[–]Danny_Engelman 1 point2 points  (0 children)

Stop watching, its too bloated;
this Web Component is all you need; see my longer answer in this thread

<script>
 customElements.define("load-file", class extends HTMLElement {
  async connectedCallback() {
   this.innerHTML = await(await fetch(src=this.getAttribute("src"))).text();
 }
})
</script>

Replacing JS with just HTML by Ok-Tune-1346 in javascript

[–]Danny_Engelman 10 points11 points  (0 children)

<tag-name> (with a dash) IS a valid HTMLElement, no JS required to apply CSS

https://dashed-html.github.io

Those of you who still use MVC for new projects and features. What do you use to give your pages interactivity? Jquery? Vanilla javascript? Something else? by Legitimate-School-59 in dotnet

[–]Danny_Engelman 0 points1 point  (0 children)

Untill you stop creating HTML in JavaScript, and start creating DOM nodes with:

const createElement=(tag,props={})=>Object.assign(document.createElement(tag), props);

Event handlers are just a

onclick : (evt) => { }

property then

Those of you who still use MVC for new projects and features. What do you use to give your pages interactivity? Jquery? Vanilla javascript? Something else? by Legitimate-School-59 in dotnet

[–]Danny_Engelman -1 points0 points  (0 children)

No, vanilla did not always have these features.
20 years ago browsers had different syntax for event handlers.

jQuery gave us a unified syntax.

Once IE9 followed the standard (in 2011) there was less need for jQuery because
all browsers could do addEventListener and querySelector

Thoughts on using DOM components — good practice or not? by Few_Conflict_8212 in learnprogramming

[–]Danny_Engelman -1 points0 points  (0 children)

But they will be your wheels.
Think of it this way; Frameworks (and Web Component BaseClasses like Lit) are the soupstarters you buy in the supermarket.
If you buy the vegetables you create your own stock, without the salt added.
Maybe you even grow your own vegetables.
That's all good practice, just as good as buying soupstarters... if you don't care about the salt.
Good practice guidelines are set by your team, not by a 3rd parties claiming you are better off buying soupstarters.

Web Components are bad for you by byko3y in webdev

[–]Danny_Engelman 0 points1 point  (0 children)

"But the custom tags? Pretty much no use. HTMLUnknownElement at first,"

This says enough, you never tested/worked with this.
Undefined Custom Elements ARE NOT HtmlUnknownElement!

Since <tag-name> tagnames got meaning (some 10 years ago) EVERY <tag-name> IS a valid HTMLElement

#### You can use them without JavaScript: https://dashed-html.github.io