Skip to content
A plugin capable of transforming Aurelia components into standards-compliant Web Components.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
build
dist
doc
src
.editorconfig
.gitignore
.npmignore
CONTRIBUTING.md
ISSUE_TEMPLATE.md
LICENSE
README.md
bower.json
karma.conf.js
package-lock.json
package.json
rollup.config.js
tsconfig.json
tslint.json

README.md

web-components

npm Version Join the chat at https://gitter.im/aurelia/discuss

This library is part of the Aurelia platform and contains a plugin capable of transforming Aurelia components into standards-compliant Web Components.

To keep up to date on Aurelia, please visit and subscribe to the official blog and our email list. We also invite you to follow us on twitter. If you have questions, please join our community on Gitter or use stack overflow. Documentation can be found in our developer hub.

Platform Support

This library can be used in the browser.

Usage

Here's an example configuration for main.js to show how to convert all global custom elements into web components:

import { CustomElementRegistry } from 'aurelia-web-components';

export function configure(aurelia) {
   aurelia.use
     .standardConfiguration()
     .developmentLogging()
     .globalResources('resources/my-component');

   aurelia
     .start()
     .then(() => {
      const registry = aurelia.container.get(CustomElementRegistry);

      //The following line takes all global resource custom elements and registers them as web components.
      //Once the element is registered, in-page elements will begin rendering.
      registry.useGlobalElements();
    });
 }

Warning: Note that calling .useGlobalElements() will remove all global elements from your global resources, including <router-view> and <compose>.

Alternatively, custom elements can also be registered on the fly:

import { InlineViewStrategy, useView } from 'aurelia-templating';
import { CustomElementRegistry } from 'aurelia-web-components';

export function configure(aurelia) {
   aurelia.use
     .standardConfiguration()
     .developmentLogging()
     .globalResources('resources/my-component');

   aurelia
     .start()
     .then(() => {
      const registry = aurelia.container.get(CustomElementRegistry);

      // the following register 
      return registry.register(class MyButton {

        static $view = `<template>
          <button>\${icon}<slot></slot></button>
        </template>`

        @bindable icon
      });
     })
     // or
     .then(() => {
      const registry = aurelia.container.get(CustomElementRegistry);

      // with useView path strategy
      @useView('path/to/view.html')
      class MyCarousel {
        // ...
      }

      // the following register 
      return Promise.all([
        // with static view strategy
        registry.register(class MyButton {

          static $view = `<template>
            <button>\${icon}<slot></slot></button>
          </template>`

          // ...
        }),
        // with inline view strategy, via getViewStrategy
        registry.register(class MyPanel {
          // ...
          getViewStrategy() {
            return new InlineViewStrategy(
              `<template>
                <h1>\${heading}</h1>
                <slot></slot>
              </template>`
            )
          }
        }),
        // with pre-defined view model class, using @useView
        registry.register(MyCarousel)
      ]);
     })
     .then(() => {
       aurelia.setRoot('app')
     });
 }

Note: This plugin requires that your browser have native support for the CustomElements v1 spec or that you have configured a v1 spec-compliant polyfill prior to calling registry methods.

Building The Code

To build the code, follow these steps.

  1. Ensure that NodeJS is installed. This provides the platform on which the build tooling runs.
  2. From the project folder, execute the following command:
npm install
  1. To build the code, you can now run:
npm run build
  1. You will find the compiled code in the dist folder, available in module formats: UMD, AMD, CommonJS and ES6.

Running The Tests

To run the unit tests, first ensure that you have followed the steps above in order to install all dependencies and successfully build the library. Once you have done that, proceed with these additional steps:

  1. You can now run the tests with this command:
npm test

How it works

  • Each of custom element will be backed by a view model.
  • For each view model class, a corresponding native custom element class will be created and defined, with the name derived from metadata and fallbacks to view model class name. If there is hyphen - in the name of a custom element view model, a prefix (au- by default) will be added to the name. This can be change in CustomElementRegistry instance.
  • Slot: By default, content projection is done using Aurelia slot emulation. This is to keep it consistent with the rest of Aurelia ecosystem. To use native slot/shadow dom for content projection, decorate view model class with @useShadowDOM.
You can’t perform that action at this time.