HTML Web 组件
HTML Web Components

原始链接: https://blog.jim-nielsen.com/2023/html-web-components/

在 Jim Nielsen 的博客文章“构建自己的自定义元素时遇到摩擦”中,他讨论了他在 Web 组件领域构建个性化元素的经验。 Nielsen 描述了传统 React 组件由于各自设计的差异,其功能通常与 Web 组件有很大不同。 他解释了 Web 组件设计的主要特征如何优先考虑增强而不是完全替换,鼓励通过使用附加功能扩展其 HTML 来组合预定内容,同时还强调通常应避免添加或替换完整部分。 尼尔森声称,通过采用增强和增强而不是完全替代,这种方法更符合互联网的整体技术轨迹,从而实现更长的寿命和兼容性。 最终,尼尔森建议那些在构建 Web 组件方面寻求持久成功的人将从遵守这一原则中受益匪浅。

根据所提供的文本材料回答您的问题,根据给定的上下文,Lit 允许 Web 组件在 JavaScript 之前渲染的说法是正确的。 然而,它指出它存在潜在的问题,例如需要数十个新的网络标准来解决自己造成的伤害,并且与传统的 React 组件功能相比工作方式有所不同。 尽管依赖程度比 Vue 少,Lit 仍然在 Angular 的轻量级版本中运行。 此外,本文讨论了 HTML 和 JavaScript Web 组件之间的差异,并指出“HTML Web 组件”并不真正存在,而仅仅是 HTML。 尽管如此,“Web 组件还是很有用的”,特别是在组件除了包含核心 HTML 内容之外还需要通过附加功能来增强的情况下。 文中承认 Web 组件带来了挑战,例如迫使设计人员采用面向对象的编程原则以及添加 TS 使问题进一步复杂化。 最后,文本建议向客户端发送预渲染的 HTML 片段可以提供服务器端渲染组件的外观,类似于 Web 组件。 然而,服务器渲染组件的能力目前并不完全可行。 根据有关声明性 Shadow dom 的参考文献,目前主流浏览器并未广泛支持它。
相关文章

原文

I think the word “component” in “web components” confused a lot of people — at least it did me.

“Web components” sounded like the web platform’s equivalent to “React components”. JSX had and now the web had .

But when you try building web components the same way you build React components, it’s easy to get frustrated and give up because web components don’t work like React components — I know I gave up a few times.

The grain of a React component is not the grain of a web component. Their design prioritize different functionality and forms of use. If you try to use one like the other, you’ll fight the direction of their natural grain.

Web components have their own grain and it favors enhancement over replacement. What do I mean by this?

A typical React component might look like this[1]:

UserAvatar
  src="https://example.com/path/to/img.jpg"
  alt="..."
/>

You could write a web component this same way, e.g.

user-avatar
  src="https://example.com/path/to/img.jpg"
  alt="..."
>user-avatar>

But the unique power of web components (in the browser) is that they can render before JavaScript. React components cannot do this — full stop.

This feature of web components encourages a design of composability. Rather than an empty “shell component” that takes data and (using JavaScript exclusively) renders the entirety of its contents, web components encourage an approach of composing core content with HTML and then wrapping it in a custom element that enhances its contents with additional functionality.

user-avatar>
  img src="https://example.com/path/to/img.jpg" alt="..." />
user-avatar>

This specific flavor of componentization is what Jeremy calls “HTML web components”:

If your custom element is empty, it’s not an HTML web component. But if you’re using a custom element to extend existing markup, that’s an HTML web component.

React encouraged a mindset of replacement: “forgot what browsers can do; do everything in a React component instead, even if you’re reinventing the wheel.”

HTML web components encourage a mindset of augmentation instead.

I like that term “HTML web component”. It stands in contrast to a “JavaScript web components” which would be an empty element whose functionality and contents rely exclusively on JavaScript.

Per my earlier example, this would be a JavaScript web component:

user-avatar
  src="https://example.com/path/to/img.jpg"
  alt="..."
>user-avatar>

It relies exclusively on the presence of JavaScript and is meaningless to the end user without it.

Whereas this would be an HTML web component:

user-avatar>
  img src="https://example.com/path/to/img.jpg" alt="..." />
user-avatar>

It has meaning and content without JavaScript — then is enhanced by its presence.

This idea of augmentation/enhancement over replacement is intriguing.

Augmentative approaches work best on the web because 1) the web’s grain encourages enhancement to improve resilience, and 2) that’s really the best way to iteratively change something as big as the web.

Eventually all the best ideas of web-adjacent frameworks are subsumed into the platform to work in ways that augment the existing technology rather than replace it wholesale.

XHTML wanted to replace HTML4, but HTML5 wanted to augment it. HTML5 won.

Networking libraries wanted to replace XMLHttpRequest and their best ideas were eventually ported into the fetch standard — which exists in more places than just the browser these days!

The best ideas of Sass and jQuery were ported to the browser.

Typescript’s best ideas are going to the browser, but in a way that works to enhance not replace what exists.

With web components, you might even say React’s component model is being ported to the browser. But it’s being done in a way that works to enhance how the web already works, not replace it.

My takeaway is: if you’re looking for longevity, opt for a technical approach of augmentation and enhancement over replacement. The web’s grain is arranged in that direction.


联系我们 contact @ memedata.com