DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk7 min

Bytes #216: Using Web Components Responsibly

Web Components are browser capabilities, not a mandate to wrap every interface fragment in a custom tag. Here is how to decide when to use custom elements, Shadow DOM, templates and slots, and how to keep them accessible.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Web Components are a set of browser capabilities, not a requirement to wrap every piece of interface in a custom tag. Use a custom element when a reusable component needs behavior that native HTML cannot express, and keep native elements wherever they already provide the semantics, behavior and keyboard support you need. Shadow DOM, templates and slots are optional tools that you add only when a specific problem calls for them.

What the platform provides

Web Components combine three browser features. Custom elements add new element types with your own behavior. Shadow DOM gives a component a scoped tree of nodes. Templates and slots let you define reusable structure and accept content from the page that uses the component. Each feature is independent, so you can use one without the others.

As an Amazon Associate I earn from qualifying purchases.

MDN describes a typical implementation in four steps:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Define a class that extends HTMLElement and holds the component’s behavior.
  2. Register the class with customElements.define() through the browser’s custom element registry. The tag name must contain a hyphen, for example rating-stars.
  3. Optionally call this.attachShadow({ mode: "open" }) to create a Shadow DOM tree.
  4. Optionally use <template> and <slot> for reusable markup and projected content.

After registration, the element is used in HTML much like a built-in element. Nothing in that sequence requires Shadow DOM, so a custom element without it is a normal, supported pattern.

When should I use Web Components?

Start from the native element. A <button>, <dialog>, <details> or <select> already exposes a role, a name, states and keyboard handling to assistive technology. Restyling a native control is usually cheaper and safer than rebuilding it.

Use native HTML when it already carries the meaning

If a native element matches the job, use it and style it with CSS. Custom versions of built-in controls must reproduce everything the browser already does for free, which is where most accessibility regressions come from.

Use a custom element when reusable behavior is the point

A custom element earns its place when the same behavior must appear across pages or applications, when it needs to initialize and clean up with its place in the document, and when it benefits from a stable declarative API that authors can configure in markup. A date range picker, a tabbed panel that manages its own focus, or a media player with a consistent control set are typical examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should every custom element use Shadow DOM?

No. Shadow DOM is worth its cost when it solves a real problem. It scopes styles, so page CSS does not select the component’s internal nodes and styles defined inside the shadow tree do not leak out. That protects components that will run on pages whose CSS you do not control. A component that is meant to take on the site’s typography and spacing, or whose markup is simple enough that collisions are unlikely, can skip it and work directly with its light DOM.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Plan the styling hooks before you ship

Encapsulation creates a styling boundary, so customization has to be designed in. The W3C Technical Architecture Group points to two options: CSS custom properties, which let consumers set values such as colors or spacing, and CSS Shadow Parts, which expose named pieces of the internal tree for targeted styling. Document the hooks you support and treat everything else as private.

Closed mode is not a security boundary

Creating a shadow root with mode: "closed" means page scripts cannot reach the internals through the ordinary shadowRoot property. MDN is explicit that this is not a strong security mechanism. Use it, if at all, to discourage casual access, and never to hide secrets or enforce a trust boundary.

Design the API for the platform

The W3C TAG’s guidance on web platform compatible components, published in 2018, recommends APIs that feel familiar to anyone who knows HTML. These are design recommendations rather than formal conformance requirements, but they make components predictable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use familiar names and simple declarative configuration

Choose attribute names that follow existing HTML conventions, such as disabled, label or value. Let simple configuration live in attributes so it can be written in markup, and reserve JavaScript for richer data.

Keep attributes and properties in sync

Authors expect an attribute and its matching JavaScript property to agree. For a boolean attribute, presence means true, so the value is set by adding or removing the attribute rather than by copying a string:

get disabled() {
  return this.hasAttribute("disabled");
}
set disabled(value) {
  if (value) {
    this.setAttribute("disabled", "");
  } else {
    this.removeAttribute("disabled");
  }
}

Reflecting the property to the attribute this way means <my-control disabled> in markup and control.disabled = true in script produce the same state, and the component’s observedAttributes callback can respond to both.

Communicate outward with events

Send changes to the page with dispatchEvent(), using a descriptive name and a detail payload. If the event must reach listeners outside the component’s shadow tree, set composed: true as well as bubbles: true; otherwise listeners on the host will not receive it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Account for lifecycle timing

The W3C TAG cautions authors not to assume that a custom element is already attached to the document when its constructor runs. The constructor can execute before the element is connected, so keep it limited to setting up internal state and, if needed, the shadow root. Work that depends on the document, such as adding attributes, reading layout or attaching global listeners, belongs in connectedCallback(), and matching cleanup belongs in disconnectedCallback().

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Preserve composition and fallback

Slots let consumers supply content while the component provides the structure around it. A default slot accepts any child nodes, and a named slot such as <slot name="label"> places a specific piece of content where the component needs it. Web.dev recommends slots for composability.

Web.dev also notes that nested content remains visible and accessible in browsers without custom element support, which makes slotted markup a useful baseline. That is a statement about visibility and accessibility of the content, not a promise that the component’s behavior works without JavaScript or without custom element support. Test those states separately if your component depends on them.

How do I make a custom element accessible?

Treat accessibility as part of the component’s contract. W3C guidance on custom controls says that authors need accessibility provisions when a native control is not suitable, and that the component must expose its names and roles, make user-settable properties available and notify assistive technology when values change. Work through these areas in order.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Prefer a native control first

Building a custom control is the step that creates the accessibility obligations. Confirm that no native element provides the behavior before you take that responsibility on.

Expose names, roles and states

A custom control must communicate what it is, what it does and what state it is in. Use the ARIA attributes that match the pattern, such as role, aria-checked, aria-expanded or aria-disabled, and keep them synchronized with the component’s state. Attributes that reference other elements by ID, such as aria-labelledby, generally resolve only within the same tree, so when the label lives in a different shadow root, pass the text as an attribute or keep both elements in one tree.

Support keyboard operation

The W3C TAG states: “Interactive elements are focusable, and can be interacted with using a keyboard in addition to mouse/touch.” (Guidelines for creating web platform compatible components, 2018.) Make every interactive part focusable, give it a visible focus indicator, and respond to the keys its pattern expects. W3C also says keyboard focus must be able to leave an interactive component using only the keyboard. If a component uses an unusual exit method, explain it to users.

Announce changes to assistive technology

When a value changes without a focus move, such as a status update or a validation result, assistive technology will not notice unless the component signals the change. Reflect the new state in the appropriate ARIA attribute and use a live region where the change is informational.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test with the keyboard and with assistive technology

A component that looks correct has not thereby been shown to be accessible. W3C guidance on custom controls calls for testing accessibility support. Check the complete set of behaviors before release:

  • Tab and Shift+Tab reach every interactive part and leave the component.
  • Each documented key pattern performs its action and no other keys interfere with the page.
  • Focus is visible and does not disappear when the component re-renders.
  • Name, role and state are announced correctly by at least one screen reader.
  • Value changes are announced once and without interrupting the user.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Comparing the options

When you are choosing between a native element, a custom element with Shadow DOM and a custom element without it, use the axes below. They synthesize MDN and W3C design guidance and are not a published scoring framework.

Axis Question to answer Favors a custom element when Warning sign
Semantics and built-in behavior Can a native element already supply the control and its meaning? No native element matches the interaction pattern You are rebuilding a button, checkbox or dialog
Encapsulation Does isolating DOM and CSS solve a real maintenance or reuse problem? The component ships to pages whose CSS you do not control Shadow DOM is added by default with no styling plan
Composition and styling Can consumers supply content and adjust appearance through stable hooks? Slots and documented custom properties or parts cover the use cases Consumers must reach into internal nodes to customize
Accessibility Are names, roles, states, keyboard interaction, focus and announcements supported and tested? Each pattern is implemented and checked with keyboard and assistive technology Visual appearance is the only evidence of accessibility
Lifecycle and integration Can the component initialize safely before connection and expose a predictable declarative and JavaScript API? Setup happens in connectedCallback, and attributes and properties agree The constructor touches the document or attributes drift from properties

Further reading

Developing Web Components by Jarrod Overson is the book most directly on this subject in the material reviewed for this piece. Its current availability and pricing were not confirmed, so check a bookseller before you buy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.