MDN Web Components Examples
repository·main·Indexed 25 days ago
https://github.com/mdn/web-components-examplesA collection of practical Web Component examples used to illustrate concepts in MDN Web Components documentation. The repository demonstrates autonomous custom elements, customized built-in elements, Shadow DOM usage, slots, lifecycle callbacks, and CSS pseudo-classes. Featured examples include <editable-list>, <edit-word>, <expanding-list>, and <custom-square>.
What's inside mdn-web-components-examples
- This repository contains a collection of Web Component examples designed to complement the MDN Web Components documentation. These examples demonstrate various concepts including autonomous custom elements, customized built-in elements, Shadow DOM usage, slots, and lifecycle callbacks.
Explore Web Component examples
mainThe following examples are available in this repository to demonstrate specific Web Component features:
- Event Properties:
composed-composed-pathdemonstrates theEvent.composedandEvent.composedPath()properties. - CSS Pseudo-classes:
defined-pseudo-classshows the usage of the:definedpseudo-class;slotted-pseudo-elementdemonstrates the::slottedpseudo-element. - Custom Elements (Autonomous):
<editable-list>: A list with addable/removable items vialist-itemattributes or UI interaction.<edit-word>: An element that reveals a text input for editing text when focused.<element-details>: Uses a<template>and<slot>to display element names and descriptions.<popup-info img="" text="">: An info icon that displays a popup via Shadow DOM based on attributes.<simple-template>: A basic demonstration of<template>and<slot>elements.
- Custom Elements (Customized Built-in):
<ul is="expanding-list">: An unordered list with expandable/collapsible children (inherits fromHTMLUListElement).<word-count>: Counts words inside an element and updates via an interval (inherits fromHTMLParagraphElement).
- Lifecycle and Slots:
life-cycle-callbacks: Uses<custom-square l="" c="">to demonstrate lifecycle callbacks through element creation, destruction, and attribute changes.slotchange: Uses<summary-display>to demonstrate theslotchangeevent andHTMLSlotElementinterface by dynamically assigning slot attributes to paragraphs.
- Event Properties:
Use the <editable-list> Web Component
mainThe<editable-list>custom element allows you to create a list where users can add new items via a text input and remove existing items using a remove button. It uses Shadow DOM for encapsulation and is configured via HTML attributes.Inspect slotted elements in 'my-paragraph'
mainWhen using
<my-paragraph>with slotted content (e.g.,<span>elements), you can access the slot relationship via theassignedSlotandslotproperties on the slotted elements.const slottedSpan = document.querySelector('my-paragraph span'); console.log(slottedSpan.assignedSlot); console.log(slottedSpan.slot);Demonstrate Event.composed and Event.composedPath() with Shadow DOM
mainThis example demonstrates how
Event.composedandEvent.composedPath()behave when interacting with Shadow DOM boundaries. It defines two custom elements:open-shadow(using an open shadow root) andclosed-shadow(using a closed shadow root). An event listener is attached to thehtmlelement to log the event's composition status and its propagation path.// Define an element with an open shadow root customElements.define('open-shadow', class extends HTMLElement { constructor() { super(); const pElem = document.createElement('p'); pElem.textContent = this.getAttribute('text'); const shadowRoot = this.attachShadow({mode: 'open'}); shadowRoot.appendChild(pElem); } } ); // Define an element with a closed shadow root customElements.define('closed-shadow', class extends HTMLElement { constructor() { super(); const pElem = document.createElement('p'); pElem.textContent = this.getAttribute('text'); const shadowRoot = this.attachShadow({mode: 'closed'}); shadowRoot.appendChild(pElem); } } ); // Listen for clicks on the document to inspect event properties document.querySelector('html').addEventListener('click', e => { console.log(e.composed); console.log(e.composedPath()); });Use the <summary-display> custom element and its slotchange event
mainThe
<summary-display>custom element demonstrates how to react to changes in a slot's assigned nodes using theslotchangeevent.When an element is assigned to a slot (for example, by setting the
slot="choice"attribute on a<p>element), theslotchangeevent fires on the<slot>element. You can listen for this event to detect when the content within a specific slot has changed and useslot.assignedNodes()to retrieve the new nodes.Implement CSS Shadow Parts in a Custom Element
mainYou can use the
partattribute on elements within a Shadow DOM to expose them to styling from the outside world via the::part()CSS pseudo-element. In this implementation, elements inside the shadow root are identified by theirpartattribute, and theirpartproperty is updated dynamically during interaction (e.g., on click) to apply different styles (like anactivestate).let template = document.getElementById("tabbed-custom-element"); globalThis.customElements.define(template.id, class extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.appendChild(template.content); let tabs = []; let children = this.shadowRoot.children; for(let elem of children) { if(elem.getAttribute('part')) { tabs.push(elem); } } tabs.forEach((tab) => { tab.addEventListener('click', (e) => { tabs.forEach((tab) => { tab.part = 'tab'; }); e.target.part = 'tab active'; }); }); } });Use the <word-count> custom element
mainThe
<word-count>element is a specialized version of the standard<p>(HTMLParagraphElement) that automatically displays a word count of its parent element within a shadow DOM.To use it, you must extend a paragraph element using the
customElements.defineAPI with the{ extends: 'p' }option. Because it is a 'customized built-in element', you use it in HTML via theisattribute on a standard<p>tag.Use the <custom-square> Web Component
mainThecustom-squareelement is a Web Component that renders a square div within a Shadow DOM. Its appearance is controlled via thesizeandcolorattributes. The component implements lifecycle callbacks to respond to being added to the DOM, removed from the DOM, or having its attributes changed.Use the <person-details> custom element
mainThe<person-details>custom element is a component that renders a template provided in the document. It attaches a shadow root inopenmode and clones the content of an element with the IDperson-templateinto that shadow root. It also applies internal styles for layout (padding, borders, and margins).Use the <popup-info> Web Component
mainThe
<popup-info>custom element displays an information box containing an icon and text. It uses a Shadow DOM with an open mode and relies on an external stylesheet (style.css) for its presentation.To configure the component, use the following attributes:
data-text: Sets the text content to be displayed in the info span.img: (Optional) Sets the URL for the icon image. If omitted, it defaults toimg/default.png.
Lifecycle callbacks in <custom-square>
mainThe
Squareclass implements the following standard Web Component lifecycle callbacks:connectedCallback(): Triggered when the element is added to the document. It logs a message and callsupdateStyle().disconnectedCallback(): Triggered when the element is removed from the document.adoptedCallback(): Triggered when the element is moved to a new document.attributeChangedCallback(name, oldValue, newValue): Triggered when thesizeorcolorattributes are modified. It callsupdateStyle()to apply the new values.