Creating Components

To create a new custom element with @beforesemicolon/web-component, you subclass the WebComponent base class and register it with the browser using customElements.define().

Here is a basic example of a component:

javascript
1import { WebComponent, html } from '@beforesemicolon/web-component'2 3class MyElement extends WebComponent {4    render() {5        return html`<p>Hello from MyElement!</p>`6    }7}8 9customElements.define('my-element', MyElement)

Tag Naming Requirements

Custom element tag names must adhere to the standard HTML specification:

  1. Must contain at least one hyphen (-): This distinguishes custom elements from standard HTML elements. For example, my-element, todo-list, and fancy-btn are valid, whereas myelement or button are invalid.
  2. Must start with a lowercase ASCII letter (e.g. a-z).
  3. Cannot contain uppercase letters: Standard HTML parsers treat tag names as case-insensitive, so custom element tags are defined in all lowercase.

Subclassing WebComponent

The WebComponent class inherits from the native HTMLElement. This means your component is a native custom element and has access to all standard DOM properties, methods, and lifecycle callbacks, while adding reactivity and convenience features.

Reserved Properties

Because WebComponent and the native HTMLElement define essential properties and methods for lifecycle management, rendering, state, and styling, you cannot use these names as reactive component properties (props).

Below is the complete list of reserved property names that cannot be overridden or declared in observedAttributes for props mapping:

Base Configuration & Styling

Reactivity & Internal Refs

DOM & Shadow Roots

Lifecycle Methods

Native HTMLElement Properties

[!WARNING] Attempting to define any of these reserved names as custom props or mapping them via observedAttributes will cause conflicts with the core runtime behavior and might break the component's reactivity or lifecycle.

edit this doc