Skip to content

Patterns & Composition

Composition means building a larger, more capable custom element by assembling smaller, focused custom elements inside it. Instead of writing one monolithic element that does everything, you break the work into pieces — each piece is its own custom element — and then nest them together to form the complete widget.

This mirrors how native HTML already works: a <form> contains <input>, <label>, and <button> elements, each responsible for a small slice of the overall experience. Web Components lets you build the same kind of layered, reusable structure with your own tag names.

The diagram below shows a <card-widget> element that composes two inner elements: <user-avatar> and <action-button>.

flowchart LR
  CW["card-widget"]
  UA["user-avatar"]
  AB["action-button"]
  CW --> UA
  CW --> AB
A composed widget tree
LessonSlugWhat you will learn
Composing ComponentscompositionNesting elements, attribute-based communication, and CustomEvent bubbling
Form-Associated Elementsform-associatedMaking custom elements participate in native HTML forms
Accessibility PatternsaccessibilityARIA roles, keyboard navigation, and focus management
Declarative Shadow DOMdeclarative-shadow-domServer-rendered shadow roots with the shadowrootmode attribute

The example below defines two custom elements: <name-tag> renders bold text from its name attribute, and <greeting-card> creates a <name-tag> and appends a welcome message — composing two elements into one widget.

What does "composition" mean in Web Components?
Which lifecycle callback is the right place to append child custom elements?
In this module, which lesson covers participating in a native HTML form?