用于营销网站的 CSS Web 组件
CSS Web Components for marketing sites (2024)

原始链接: https://hawkticehurst.com/2024/11/css-web-components-for-marketing-sites/

## 重新思考营销网站的 Web 组件 作者认为,传统的 Web 组件依赖 JavaScript 和 Shadow DOM,通常对于营销网站设计系统来说过于复杂。这些组件经常代表简单的 UI 元素,如卡片或横幅,不*需要* JavaScript。 他们提出一个演进过程:首先是 **HTML Web 组件**,它使用自定义元素来封装标准的 HTML,允许使用最少的 JavaScript “水合作用”进行渐进增强。这种方法提供了 SSR 兼容性和更易于访问性。 然而,核心思想是 **CSS Web 组件**——*完全*使用 CSS 和自定义 HTML 元素构建组件,完全消除 JavaScript。功能和变体通过 CSS 属性选择器来实现,允许根据元素属性进行动态样式设置(例如,``)。 这种方法利用了 CSS 在响应式设计和组件变体方面的强大功能,避免了与标准 HTML 属性的命名冲突,并且非常适合优先考虑性能和可访问性的营销网站,从而最大限度地减少对 JavaScript 的依赖。作者认为这种方法可以有效地处理大多数常见的营销网站组件。

一篇 Hacker News 的讨论围绕着“CSS Web Components 用于营销网站”的文章。作者展示了使用自定义元素标签名(如 ``) 的代码,但评论者争论这是否真正符合 Web Components 的定义。 一个主要的争论点是 Web Component 的定义——有些人认为它*必须*是自包含的,拥有自己的依赖项(JavaScript、CSS、模板),而这种实现严重依赖外部 CSS。一位评论员建议使用标准类名 (``) 是一种更轻量级和更优化的方法,避免不必要的 HTML 冗余。 其他评论简要涉及广告固有的破坏性,并提出了 XSLT 作为一种潜在的替代解决方案。最终,讨论的问题是,使用自定义元素相对于传统的 CSS 类名进行样式设置,是否真的有额外的优势。
相关文章

原文

November 4, 2024 – @hawkticehurst

Hot take: I think “regular” web components (the ones with Shadow DOM and friends) are a terrible solution for marketing website design systems.

It has always left a bad taste in my mouth when I run across a web component for a swimlane, banner, card, and so on. Why? Because these are components that (unless you’re doing something mighty fancy) should never require JavaScript as a dependency.

But, in the world of web components you are locked into JavaScript from the very start. To even register a web component with the browser you need JavaScript.

But what if… we didn’t do that?

HTML Web Components

I’ve spent a good chunk of the last year focused on marketing site design systems at work. A regular topic of discussion is the need to build marketing sites that are accessible to folks with lower powered devices and poor internet connections. How do you achieve that? In short, use less JavaScript and ideally build UI with progressive enhancement in mind.

There are many ways to achieve these goals, but the method I’ve been focused on is how an HTML Web Component archictecture might be applied to implement a marketing site design system.

As a quick reminder/intro, HTML Web Components is a method of building web components where you write HTML as you would normally and then wrap the parts you want to be interactive using a custom element.

For example, if you wanted to create a counter button it would look like this:

<counter-button>
  <button>Count is <span>0</span></button>
</counter-button>

<style>
  counter-button button {
    /* Counter button styles */
  }
</style>

<script>
  class Counter extends HTMLElement {
    // Counter button behavior
  }
  customElements.define("counter-button", Counter);
</script>

The markup in an HTML web component is parsed, rendered, and styled as normal HTML. That HTML will then be seamlessly hydrated once the JavaScript associated with the custom element tag is executed.

In contrast, the markup of a "regular" web component (that uses Shadow DOM) is dynamically generated at runtime using JavaScript -- kind of like an SPA.

This component architecture is a really strong candidate for a marketing design system (and, as a bonus, avoids some of the big gotchas that come with regular web components).

  • It is a perfect implementation of progressively enhanced UI
  • It uses minimal and self-contained JavaScript — HTML Web Components can be thought of as islands
  • You still get the power of custom element APIs to implement stuff like design system component variants
  • The component markup is fully SSR-able
  • The component markup can be styled like regular HTML
  • Common accessibility practices can be applied without issue

But for all these benefits we’re still left with the original problem. HTML Web Components require JavaScript.

CSS Web Components

So here’s the question: What would happen if we took the ideas of HTML Web Components and skipped all the JavaScript?

You get CSS Web Components.

Note: I've never seen anyone talk about or name this concept before, so I'm using "CSS Web Components" to describe the idea. But please let me know if someone has already written about and named this!

How do they work? The exact same as HTML Web Components but you just take advantage of the powers of CSS to implement key functionality.

As an example let’s implement that swimlane component:

<swim-lane>
  <section>
    <h2>Creativity unleashed</h2>
    <p>A brand new way of illustrating for the web.</p>
    <a href="/product">Learn more</a>
  </section>
  <img src="product.jpg" alt="Product image" />
</swim-lane>

<style>
  swim-lane {
    display: flex;
    align-items: center;
    gap: 2rem;
    color: white;
    background: black;
    padding: 1rem;
    border-radius: 16px;
  }
  swim-lane h2 {
    /* Swimlane title styles */
  }
  swim-lane p {
    /* Swimlane description styles */
  }
  swim-lane a {
    /* Swimlane link styles */
  }
  @media (max-width: 650px) {
    /* Mobile responsive styles */
  }
</style>

Creativity unleashed

A brand new way of illustrating for the web.

Learn more
Product image

Okay great, we styled some HTML nested inside a custom element. There’s nothing too novel about that. But what about adding some functionality? Say, a component variant that lets you reverse the layout of the swimlane?

It’s possible using only CSS! Specifically, CSS attribute selectors.

<swim-lane layout="reverse">
  <section>
    <h2>Creativity unleashed</h2>
    <p>A brand new way of illustrating for the web.</p>
    <a href="/product">Learn more</a>
  </section>
  <img src="product.jpg" alt="Product image" />
</swim-lane>

<style>
  /* Other swimlane styles */
  swim-lane[layout="reverse"] {
    flex-direction: row-reverse;
  }
  @media (max-width: 650px) {
    swim-lane[layout="reverse"] {
      flex-direction: column-reverse;
    }
  }
</style>

Creativity unleashed

A brand new way of illustrating for the web.

Learn more
Product image

Another really cool perk of this is that because you’re defining an attribute on a custom element you don’t have to worry about naming collisions with HTML attributes. No need to add data- to the beginning of attributes like you would/should on normal HTML elements.

How far does this go?

In theory, I believe this method of building design systems can go quite far. If you think about it, the vast majority of basic components you might need in a marketing design system are just vanilla HTML elements with specific style variations.

A marketing website button is just an anchor tag wrapped in a <link-button> custom element and styled using custom attribute selectors.

<link-button>
  <a href="">Learn more</a>
</link-button>
<link-button variant="secondary">
  <a href="">Learn more</a>
</link-button>
<link-button pill>
  <a href="">Learn more</a>
</link-button>
<link-button size="large">
  <a href="">Learn more</a>
</link-button>

<style>
  link-button a {
    /* Default link button styles */
  }
  link-button[variant="secondary"] a {
    background: transparent;
    color: white;
  }
  link-button[pill] a {
    border-radius: 50px;
  }
  link-button[size="large"] a {
    padding: 10px 20px;
    font-size: 1.25rem;
  }
</style>

From here, imagine incorporating all the other powers that CSS (and HTML) bring to the table:

The possibilities are quite large.

What do you think?

联系我们 contact @ memedata.com