Skip to content

Lesson 3 of 9 · 21m

Designing Component Contracts That Don't Become Framework Glue

How to choose an element's public API, preserve progressive enhancement, avoid leaky dependencies, and make boundaries testable.

A web component isn't valuable because it uses shadow DOM. It is valuable because any host — static HTML, Rails, React, Vue, or a CMS template — can consume the same stable DOM contract. Design the outside first, and keep your build system out of the public API.

Start with the HTML consumers should write

<checkout-summary currency="USD">
  <span slot="title">Order summary</span>
  <summary-line item="Annual plan" amount="120"></summary-line>
  <summary-line item="Priority support" amount="40"></summary-line>
</checkout-summary>

This draft exposes the design: primitive configuration as attributes, rich content as children, composition through slots, and dynamic output through events. If consumers must call methods before anything renders, you have designed a widget factory rather than an element.

Progressive enhancement is a layout strategy

<details-disclosure>
  <button slot="summary">Advanced settings</button>
  <div slot="panel"><p>TLS options...</p></div>
</details-disclosure>

Before upgrade, the content is still present and readable. After upgrade, the component arranges it into interactive UI. This is not only an accessibility nicety; it limits deployment damage. If a script chunk fails, users still receive content instead of a blank box.

Publish the contract, hide the machinery

A component README should be able to list the whole public surface:

  • Attributes: open, placement, disabled
  • Properties: value, items
  • Events: dropdown-change, dropdown-close
  • Slots: default, trigger, empty
  • CSS hooks: --dropdown-max-height, ::part(trigger)

Everything else is private. Resist one-off requests to expose internal methods because one integration is convenient. Every accidental method becomes permanent compatibility debt.

Don't make the host enforce your invariants

Bad contract: assign items, then remember to call rebuild(). The element should react to property assignment itself, batching synchronous framework writes when needed:

_invalidate() {
  if (this._queued) return;
  this._queued = true;
  queueMicrotask(() => {
    this._queued = false;
    this.render();
  });
}

That small queue prevents five property assignments from causing five renders, while keeping the component internally coherent no matter who writes to it.

Field notes

  • Version contracts, not internals. A visually identical release that renames an event is breaking.
  • Components should not import application routers, stores, analytics, or app-specific token systems.
  • A plain HTML fixture using <script type="module"> is the best proof there is no hidden framework dependency.
  • If seven or more attributes configure one behavior cluster, split the component or introduce a parent element.

Was this lesson helpful?

Sign in to rate