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
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
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
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
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)
CustomElement.getDefinition
Retrieves the CustomElementDefinition for a custom element class, providing access to all metadata about the element.
Method Signature
Type: Constructable - The custom element class
Returns a CustomElementDefinition object containing all metadata about the custom element.
CustomElement.find
Searches for a custom element definition by name within a specific container's registry.
Method Signature
container: IContainer - The dependency injection container to search in
name: string - The name of the custom element to find
Returns the CustomElementDefinition if found, or null if no element with the specified name is registered.
CustomElement.isType
Checks whether a given value is a custom element type (class decorated with @customElement or defined via CustomElement.define).
Method Signature
value: any - The value to check
Returns true if the value is a custom element type, false otherwise.
CustomElement.annotate
Attaches metadata to a custom element class. This is typically used internally by decorators and the framework.
Method Signature
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
CustomElement.getAnnotation
Retrieves metadata that was previously attached to a custom element class.
Method Signature
Type: Constructable - The custom element class
prop: string - The property key to retrieve
Returns the annotation value, or undefined if not found.
Utility Methods
CustomElement.generateName
Generates a unique name for a custom element, useful for anonymous or dynamically created elements.
Method Signature
A string representing a unique name (typically in the format unnamed-{number}).
CustomElement.generateType
Dynamically generates a CustomElementType with a given name and prototype properties.
Method Signature
name: string - The name for the generated type
proto?: object - An optional object containing properties and methods to add to the prototype
A CustomElementType that can be used with CustomElement.define.
CustomElement.createInjectable
Creates an InjectableToken for dependency injection scenarios.
Method Signature
An InterfaceSymbol that can be used as a dependency injection token.
CustomElement.keyFrom
Generates the registry key used internally to store and retrieve custom element definitions.
Method Signature
name: string - The custom element name
A string representing the internal registry key.
@customElement Decorator
The primary decorator for marking a class as a custom element.
@useShadowDOM Decorator
Enables Shadow DOM for the custom element.
@containerless Decorator
Renders the custom element without its element container.
@capture Decorator
Enables capturing of all attributes and bindings that are not explicitly defined as bindables or template controllers.
@processContent Decorator
Defines a hook that processes the element's content before compilation.
Definition Objects
PartialCustomElementDefinition
An object that describes a custom element's configuration. All properties are optional.
CustomElementDefinition
The complete, resolved definition of a custom element (read-only).
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.
1. Use Decorators Over Direct API Calls
2. Type Your Controllers
3. Handle Errors Gracefully
4. Leverage Definition Objects for Complex Elements