jj
    Preparing search index...

    Function defineComponent

    • Registers the custom element with the browser and waits till it is defined.

      Parameters

      • name: string

        The custom element name. Supports kebab-case or PascalCase and normalizes to kebab-case.

      • constructor: CustomElementConstructor

        The custom element constructor/class.

      • Optionaloptions: ElementDefinitionOptions

        Optional native customElements.define options.

      Returns Promise<boolean>

      A promise resolving to true if already defined, false if defined by this call.

      Note that the component name should contain a hyphen to be valid.

      Defining components before usage is important for reliability. If markup is rendered before the browser knows the custom element definition, upgrade timing can become race-prone and appear flaky across environments.

      class MyComponent extends HTMLElement {}
      await defineComponent('my-component', MyComponent)

      A common pattern is exposing a static defined promise on the component class.

      export class MyComponent extends HTMLElement {
      static defined = defineComponent('my-component', MyComponent)
      }

      That way, importers can await readiness declaratively and in parallel.

      import { MyComponent, YourComponent, TheirComponent } ...
      await Promise.all([
      MyComponent.defined,
      YourComponent.defined,
      TheirComponent.defined,
      ])

      If name is not a string or constructor is not a function.

      If the normalized name is not a valid custom-element name.

      If another constructor is already defined for the same normalized name.