CustomElement API

The CustomElement resource is a fundamental concept in Aurelia 2, providing the core functionality for creating encapsulated and reusable components. This comprehensive guide covers all aspects of the CustomElement API, including methods, decorators, and configuration options.

Table of Contents

Core Methods

CustomElement.for

Retrieves the Aurelia controller associated with a DOM node. The controller provides access to the element's view model, lifecycle methods, and other properties.

Method Signatures

Parameters

  • node: Node - The DOM node for which to retrieve the controller

  • opts?: object - Optional configuration object with the following properties:

    • optional?: boolean - If true, returns null instead of throwing when no controller is found

    • searchParents?: boolean - If true, searches parent nodes (including containerless elements) for a controller

    • name?: string - If provided, only returns controllers for custom elements with this specific name

Examples

CustomElement.define

Registers a class as a custom element in Aurelia. This method can be called directly or is used internally by the @customElement decorator.

Method Signatures

Parameters

  • nameOrDef: string | PartialCustomElementDefinition - Either the element name or a complete definition object

  • Type?: Constructable - The class containing the element's logic (optional when using definition object)

Examples

CustomElement.getDefinition

Retrieves the CustomElementDefinition for a custom element class, providing access to all metadata about the element.

Method Signature

Parameters

  • Type: Constructable - The custom element class

Return Value

Returns a CustomElementDefinition object containing all metadata about the custom element.

Example

CustomElement.find

Searches for a custom element definition by name within a specific container's registry.

Method Signature

Parameters

  • container: IContainer - The dependency injection container to search in

  • name: string - The name of the custom element to find

Return Value

Returns the CustomElementDefinition if found, or null if no element with the specified name is registered.

Example

CustomElement.isType

Checks whether a given value is a custom element type (class decorated with @customElement or defined via CustomElement.define).

Method Signature

Parameters

  • value: any - The value to check

Return Value

Returns true if the value is a custom element type, false otherwise.

Example

Metadata Methods

CustomElement.annotate

Attaches metadata to a custom element class. This is typically used internally by decorators and the framework.

Method Signature

Parameters

  • Type: Constructable - The custom element class to annotate

  • prop: string - The property key for the annotation

  • value: any - The value to associate with the property

Example

CustomElement.getAnnotation

Retrieves metadata that was previously attached to a custom element class.

Method Signature

Parameters

  • Type: Constructable - The custom element class

  • prop: string - The property key to retrieve

Return Value

Returns the annotation value, or undefined if not found.

Example

Utility Methods

CustomElement.generateName

Generates a unique name for a custom element, useful for anonymous or dynamically created elements.

Method Signature

Return Value

A string representing a unique name (typically in the format unnamed-{number}).

Example

CustomElement.generateType

Dynamically generates a CustomElementType with a given name and prototype properties.

Method Signature

Parameters

  • name: string - The name for the generated type

  • proto?: object - An optional object containing properties and methods to add to the prototype

Return Value

A CustomElementType that can be used with CustomElement.define.

Example

CustomElement.createInjectable

Creates an InjectableToken for dependency injection scenarios.

Method Signature

Return Value

An InterfaceSymbol that can be used as a dependency injection token.

Example

CustomElement.keyFrom

Generates the registry key used internally to store and retrieve custom element definitions.

Method Signature

Parameters

  • name: string - The custom element name

Return Value

A string representing the internal registry key.

Example

Decorators

@customElement Decorator

The primary decorator for marking a class as a custom element.

Syntax

Examples

@useShadowDOM Decorator

Enables Shadow DOM for the custom element.

Syntax

Example

@containerless Decorator

Renders the custom element without its element container.

Syntax

Example

@capture Decorator

Enables capturing of all attributes and bindings that are not explicitly defined as bindables or template controllers.

Syntax

Example

@processContent Decorator

Defines a hook that processes the element's content before compilation.

Syntax

Example

Definition Objects

PartialCustomElementDefinition

An object that describes a custom element's configuration. All properties are optional.

Properties

Example

CustomElementDefinition

The complete, resolved definition of a custom element (read-only).

Key Properties

Programmatic Resource Aliases

PartialCustomElementDefinition.aliases is only one way to expose alternative names. For reusable libraries or bridge packages you often need to add aliases outside of the definition itself. The runtime provides two helpers to make that ergonomic.

alias(...aliases) decorator

Apply the decorator directly to any custom element, custom attribute, value converter, or binding behavior to append aliases to the resource metadata.

The decorator merges with aliases declared via the definition object, so you can sprinkle default aliases in a base class and extend them in derived implementations without clobbering earlier metadata.

registerAliases(...)

When you need to attach aliases to an existing resource (for example, to keep backwards compatibility after a rename), call registerAliases during app startup.

The resource argument identifies which registry to update. Pass CustomElement, CustomAttribute, ValueConverter, or BindingBehavior depending on the resource you are aliasing. Because aliases are registered against the supplied container you can scope them to individual feature modules or make them global by running the task in your root configuration.

Best Practices

1. Use Decorators Over Direct API Calls

2. Type Your Controllers

3. Handle Errors Gracefully

4. Leverage Definition Objects for Complex Elements

Last updated

Was this helpful?