Skip to content

Non-transpiled ES13+ web components helper functions.

License

Notifications You must be signed in to change notification settings

browserkids/web-components

Repository files navigation

@browserkids/web-components

npm version Known Vulnerabilities Build Status Dependency Status

This is a collection of useful web components helper functions. They are all written in latest/cutting edge ECMAScript 2022 code and are not transpiled.


Installation

Using a CDN

Fastest way to get going. See for yourself:

<script type="module">
import { define } from 'https://unpkg.com/@browserkids/web-components';

define(class MyElement extends HTMLElement {
  /* Your custom element logic. */
});
</script>

Using a bundler

Semi-fast way as well. Just install it via npm.

npm install -S @browserkids/web-components

Import the functions where you need them.

import { define } from '@browserkids/web-components';

Browser support

Almost every function uses at least one feature of ECMAScript 2022 , but no ESNext features — promised. So support should be fine for “evergreen” browsers at the time of writing. This means that Internet Explorer is out of the game.

As this library is not transpiled nor ever will be, you should use polyfills in case you need to support a specific browser version.


API

  1. define(elementConstructor)

    The define() helper function registers a custom element constructor to the CustomElementRegistry. To reduce redundant tasks like creating a ShadowDOM, binding attributes, setting up event listeners or fetching DOM references, the given element constructor will be extended and enhanced to have a consistent developing experience.

    <my-element></my-element>
    
    <script type="module">
      import { define } from 'https://unpkg.com/@browserkids/web-components';
    
      define(class MyElement extends HTMLElement {
      // By default define() uses the class name as element name, 
      // you can override this by defining a custom name.
      static get name() { return 'my-element'; }
    
      // You can define a static settings field to override the settings 
      // for the individual functions that define() is using.
      static settings = {
        bindAttributes: {},
        bindEventListeners: {},
        createShadowRoot: {},
        findReferences: {},
      };
      
      // Will be used by bindAttributes and turns into a proxy object.
      data = {};
    
      // Will be used by createShadowRoot for shadow dom creation.
      template = '';
      
      // Will be called once the element has been inserted to the DOM,
      // and the constructor of that class has finished.
      ready() {}
    });
    
    </script>