@browserkids/web-components

0.8.0 • Public • Published

@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 name = '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>

Readme

Keywords

Package Sidebar

Install

npm i @browserkids/web-components

Weekly Downloads

0

Version

0.8.0

License

MIT

Unpacked Size

12.4 kB

Total Files

5

Last publish

Collaborators

  • mzdr