Web components

From j4flmao/agent-skills · View interactive version

Web components is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about custom elements, shadow DOM, slots and templates, ElementInternals, form-associated elements, declarative shadow DOM or framework-free cross-framework components. Produces standards-based web components. Not for Lit (use frontend-lit) or Stencil (use stencil).

How to use this skill

Skill file

Web Components

Purpose

Build vanilla web components using the native Custom Elements API: encapsulated shadow DOM, typed lifecycle, form participation via ElementInternals, and cross-framework compatibility patterns.

Agent Protocol

Trigger

Exact user phrases: "web component", "custom element", "shadow DOM", "HTML template", "CustomElementsRegistry", "ElementInternals", "form-associated element", "cross-framework component", "vanilla web component".

Input Context

Before activating, verify:

Output Artifact

No file output. Produces element design, encapsulation strategy, framework integration as text.

Response Format

Element Design: {name} — {purpose}
Encapsulation: {open shadow DOM / closed / light DOM}
Framework Integration: {attribute reflection, event mapping}

No preamble. No postamble. No explanations. No filler/hedging/transitions. Compress output — why use many token when few do trick.

Completion Criteria

Max Response Length

2560 tokens.

Web Component Architecture / Decision Trees

Architecture Decision Tree

Is this a reusable component shared across frameworks?
  |-- YES --> Web Components (framework-agnostic by nature)
  |     Use case: design system components, shared widgets
  |
  |-- NO, single framework only -->
        |-- Using React? --> React component (simpler, better DX)
        |-- Using Vue? --> Vue SFC (simpler, better DX)
        |-- Using Angular? --> Angular component (simpler, better DX)
        |-- Vanilla JS project? --> Web Component

Shadow DOM Decision Tree

Does the component need style encapsulation?
  |-- YES -->
  |     |-- Needs to be accessible from outside for testing? -->
  |     |     YES: attachShadow({ mode: 'open' })
  |     |     NO:  attachShadow({ mode: 'closed' }) — rare, breaks tooling
  |     |
  |     |-- Content projection needed? -->
  |           Use <slot> elements in shadow DOM template
  |
  |-- NO -->
        Light DOM (no shadow root, styles are global)
        Simpler, but CSS can leak in/out

Form-Associated Decision Tree

Does the component represent a form value?
  |-- YES (input, select, checkbox, custom file picker) -->
  |     Set static formAssociated = true
  |     Use attachInternals() for form participation
  |     Update value: _internals.setFormValue(val)
  |     Report validity: _internals.setValidity(flags)
  |
  |-- NO (button, tooltip, accordion, card) -->
        Regular custom element, no form involvement

Cross-Framework Wrapper Decision Tree

What framework consumes this component?
  |-- React -->
  |     Wrapper: useRef + useEffect to set properties/attributes
  |     Events: addEventListener in useEffect, cleanup in return
  |     Complex props: set via element.property (not attribute)
  |
  |-- Vue -->
  |     Wrapper: template with :prop binding + @event handler
  |     v-model: implement with :value + @input pattern
  |
  |-- Angular -->
  |     Wrapper: CUSTOM_ELEMENTS_SCHEMA for template
  |     or Angular wrapper component with ElementRef

Workflow

Step 1: Custom Element Lifecycle

class BaseElement extends HTMLElement {
  constructor() {
    super()
    this._internals = this.attachInternals?.()
  }

  connectedCallback() {
    this._render()
    this._addListeners()
  }

  disconnectedCallback() {
    this._removeListeners()
  }

  attributeChangedCallback(name, oldValue, newValue) {
    if (oldValue !== newValue) this._render()
  }

  adoptedCallback() {
    this._render()
  }
}

Four lifecycle hooks: constructor → connected → disconnected → attributeChanged. adoptedCallback fires when element moves between documents.

Step 2: Attributes & Properties

class UserAvatar extends HTMLElement {
  static get observedAttributes() {
    return ['src', 'alt', 'size']
  }

  get src() { return this.getAttribute('src') ?? '' }
  set src(val) { this.setAttribute('src', val) }

  get alt() { return this.getAttribute('alt') ?? '' }
  set alt(val) { this.setAttribute('alt', val) }

  get size() { return parseInt(this.getAttribute('size') ?? '40') }
  set size(val) { this.setAttribute('size', String(val)) }

  constructor() {
    super()
    const root = this.attachShadow({ mode: 'open' })
    // Build the DOM once; never interpolate attribute values into innerHTML (XSS)
    this._img = document.createElement('img')
    this._img.style.borderRadius = '50%'
    root.append(this._img)
  }

  connectedCallback() {
    this._render()
  }

  attributeChangedCallback(name, oldVal, newVal) {
    if (oldVal !== newVal) this._render()
  }

  _render() {
    this._img.src = this.src
    this._img.alt = this.alt
    this._img.width = this.size
    this._img.height = this.size
  }
}
customElements.define('user-avatar', UserAvatar)

Property setter delegates to setAttribute, triggering attributeChangedCallback. This ensures consistency whether the consumer sets properties or attributes.

Step 3: Shadow DOM Encapsulation

class Tooltip extends HTMLElement {
  constructor() {
    super()
    const template = document.getElementById('tooltip-template')
    this.attachShadow({ mode: 'open' })
      .appendChild(template.content.cloneNode(true))
  }

  connectedCallback() {
    this.shadowRoot.querySelector('slot')
      .addEventListener('slotchange', () => this._position())
  }
}

| Mode | Behavior |

|------|----------|

| open | Accessible via element.shadowRoot — testing and inspection |

| closed | Not accessible externally — element.shadowRoot is null |

Use open mode for testability. Use closed only when absolute encapsulation is required (design system internals).

Step 4: Slots & Templates

<template id="accordion-template">
  <style>
    :host { display: block; border: 1px solid #ddd; border-radius: 4px; }
    .header { padding: 12px 16px; cursor: pointer; background: #f5f5f5; }
    .content { padding: 0 16px; max-height: 0; overflow: hidden; transition: max-height 0.3s; }
    :host([open]) .content { max-height: 500px; padding: 16px; }
  </style>
  <div class="header" part="header">
    <slot name="summary">Details</slot>
  </div>
  <div class="content" part="content">
    <slot></slot>
  </div>
</template>
customElements.define('my-accordion', class extends HTMLElement {
  constructor() {
    super()
    const tmpl = document.getElementById('accordion-template')
    this.attachShadow({ mode: 'open' }).appendChild(tmpl.content.cloneNode(true))
  }

  connectedCallback() {
    this.shadowRoot.querySelector('.header')
      .addEventListener('click', () => this.toggleAttribute('open'))
  }
})

Usage:

<my-accordion open>
  <span slot="summary">Shipping Info</span>
  <p>Orders ship within 2-3 business days.</p>
</my-accordion>

Step 5: Form-Associated Elements (ElementInternals)

class FormInput extends HTMLElement {
  static formAssociated = true

  constructor() {
    super()
    this._internals = this.attachInternals()
    const root = this.attachShadow({ mode: 'open' })
    this._input = document.createElement('input')
    this._input.type = 'text'
    root.append(this._input)
    this._internals.setFormValue('')
  }

  static get observedAttributes() { return ['value', 'required', 'disabled', 'name'] }

  get value() { return this.getAttribute('value') ?? '' }
  set value(val) { this.setAttribute('value', val); this._internals.setFormValue(val) }

  get form() { return this._internals.form }

  connectedCallback() {
    this._render()
    this._input.addEventListener('input', (e) => {
        this.value = e.target.value
        this._internals.setValidity({})
        this.dispatchEvent(new Event('input', { bubbles: true, composed: true }))
      })
  }

  attributeChangedCallback(name, oldVal, newVal) {
    if (oldVal !== newVal) this._render()
  }

  _render() {
    // Update the existing input instead of re-creating it (keeps focus and listeners)
    if (this._input.value !== this.value) this._input.value = this.value
    this._input.disabled = this.hasAttribute('disabled')
    this._input.required = this.hasAttribute('required')
  }
}
customElements.define('form-input', FormInput)

formAssociated = true enables native form participation — values submit with parent <form>. Use _internals.setValidity() for constraint validation.

Step 6: Cross-Framework Compatibility

// React wrapper
export function ReactUserAvatar(props) {
  const ref = useRef(null)
  useEffect(() => {
    const el = ref.current
    if (props.src) el.src = props.src
    if (props.alt) el.alt = props.alt
    if (props.size) el.size = props.size
    const handler = (e) => props.onSelect?.(e.detail)
    el.addEventListener('select', handler)
    return () => el.removeEventListener('select', handler)
  }, [props.src, props.alt, props.size])
  return <user-avatar ref={ref} />
}

// Vue wrapper
const VueUserAvatar = {
  props: ['src', 'alt', 'size'],
  emits: ['select'],
  template: '<user-avatar :src="src" :alt="alt" :size="size" @select="$emit(\'select\', $event.detail)" />',
}

Key compatibility rules:

Common Pitfalls

1. Not Cleaning Up in disconnectedCallback

Event listeners and MutationObservers attached in connectedCallback must be removed in disconnectedCallback. Otherwise, the element leaks memory.

2. attributeChangedCallback Not Triggering

attributeChangedCallback only fires for attributes listed in observedAttributes. If you add a custom attribute without listing it, the callback won't fire.

3. Property Setters Not Using setAttribute

// BAD -- setting internal state without triggering attributeChanged
set size(val) { this._size = val; this._render(); }

// GOOD -- delegates to setAttribute, triggers attributeChanged
set size(val) { this.setAttribute('size', val); }

4. Events Not Crossing Shadow Boundary

Events dispatched from within shadow DOM don't cross the boundary by default. Use composed: true to bubble up through shadow roots.

5. Naming Without Hyphen

Custom element names MUST contain a hyphen (e.g., my-button). Single-word names conflict with built-in HTML elements.

Compared With

| Approach | Bundle Size | Framework Agnostic | SSR | Learning Curve |

|----------|------------|-------------------|-----|---------------|

| Vanilla Web Component | 0KB (native) | Yes | Limited (declarative shadow DOM) | Medium |

| Lit | ~5KB | Yes | Yes | Low |

| Stencil | ~8KB | Yes | Yes | Medium |

| React component | 0KB (runtime) | No (React only) | Yes | Low |

| Vue SFC | 0KB (runtime) | No (Vue only) | Yes | Low |

Performance Considerations

Accessibility Considerations

Security Considerations

Rules

References

Handoff

No artifact produced.

Next skill: frontend-lit for Lit-based web components. Or frontend-universal-design-system for design token integration.

Carry forward: custom element patterns, shadow DOM conventions, cross-framework wrapper approach.

Architecture Decision Trees

Shadow DOM Decision Tree

`

Does the component need style isolation?

├── No → Light DOM component (styles inherited, faster)

└── Yes → Does it need to be styled from outside?

├── Yes → Open shadow DOM + CSS custom properties + ::part()

└── No → Closed shadow DOM (max isolation, cannot style externally)

Does the component contain light DOM children (slots)?

├── Yes → Shadow DOM with slot elements

└── No → Shadow DOM with no slots

`

Custom Element Registration Decision Tree

`

Is the element used in a single app?

├── Yes → Define directly with customElements.define()

└── No → Package as library with lazy registration

Does it depend on other custom elements?

├── Yes → Export registration function that registers dependencies first

└── No → Self-registering module via side-effect import

Need polyfills for legacy browsers?

├── Yes → Dynamic polyfill loading with feature detection

└── No → Modern browsers only, no polyfills

`

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.