npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-custom-element

v1.0.4

Published

Convenience Class for Custom Elements

Downloads

2

Readme

SimpleElement

Simple Element is a convenience Class for Custom Elements. It adds functions around simpler property getter and setters, element selectors for manipulation through native element functions, and simple mutation observer for dom manipulation reactions.

Examples

Basic

At it's very simplest form you can create a custom element like this:

import {html, css, SimpleElement } from 'SimpleElement';

class MyElement extends SimpleElement {
  styles = css`:host[blue]{ color: blue;}`;
  markup = html`<slot></slot>`;

  properties = {
    blue: {type: Boolean}
  };

  constructor(){
    super();
    this.render();
  }
}
customElements.define("my-element", MyElement);

observed property

That is useful, but typically you are going to want more functionality, so adding more properties to react to will give you more functionality. That is done mostly through native functionality, but we have a selectors helper that will help in making DOM changes in the shadow dom. From the selectors there is an object to access the selected elements this.elements that can access the element with the key used in the selectors object.

import {html, css, SimpleElement } from 'SimpleElement';

class MyElement extends SimpleElement {
  styles = css`
    :host[blue]{
      color: blue;
    }
  `;
  markup = html`
    <em></em>
    <slot></slot>
  `;

  properties = {
    blue: {type: Boolean},
    prefix: {type: String}
  };
  selectors = {
    // defining the selector here
    prefix: {selector: 'em'}
  };

  constructor(){
    super();
    this.render();
  }

  static get observedAttributes() {
    return ["prefix"];
  }

  attributeChangedCallback(attrName, oldVal, newVal) {
    // using the `this.elements` object to access the prefix selected element
    if (attrName === "prefix") this.elements.prefix.textContent = newVal;
  }
}
customElements.define("my-element", MyElement);

event listener on elements

Sometimes you want to be able to add event listeners on your elements, we have a simple helper in the selector object to help with that.

import {html, css, SimpleElement } from 'SimpleElement';

class MyElement extends SimpleElement {
  styles = css`
    :host[blue]{
      color: blue;
    }
  `;
  markup = html`
    <em></em>
    <slot></slot>
    <button>Poke!</button>
  `;

  properties = {
    blue: {type: Boolean},
    prefix: {type: String}
  };
  selectors = {
    prefix: {selector: 'em'},
    button: {
      selector: 'button',
      event: 'click',
      handler: this.handleClick.bind(this)
    }
  };

  constructor(){
    super();
    this.render();
  }

  static get observedAttributes() {
    return ["prefix"];
  }

  attributeChangedCallback(attrName, oldVal, newVal) {
    if (attrName === "prefix") this.elements.prefix.textContent = newVal;
  }

   handleClick(e) {
    // we can use the custom element API here to dispatch an internal
    // event from the custom elements
    this.dispatchEvent(
      new CustomEvent("poked", {
        bubbles: true,
        // composed is important for events from inside a shadow dom
        // being dispatched outside.
        composed: true,
      })
    );
  }
}
customElements.define("my-element", MyElement);

Mutation Observer

Sometimes you need to use the tree from the light DOM to influence the Shadow DOM, for example, if you have option elements inside a custom selection component. You don't want the options to render in the light DOM, but want the API of sending options through the DOM tree.

import {html, css, SimpleElement } from 'SimpleElement';

class MyElement extends SimpleElement {
  styles = css`:host[blue]{ color: blue;}`;
  markup = html`
    <select></select>
  `;

  selectors = {
    select: {
      selector: 'select',
      event: 'changed',
      handler: this.handleChange.bind(this)
    }
  };

  constructor(){
    super();
    this.render({observe: true});
  }

  // use this function to handle any mutation in the light DOM
  // mutation is a MutationRecord (https://dom.spec.whatwg.org/#mutationrecord)
  handleMutation(mutation){
    [...this.elements.select.children].forEach(child => child.remove());
    [...this.children].forEach((child, i) => {
      if (!this.value && i === 0) {
        this.value = child.value || child.textContent;
      }
      if (child.localName === 'option') {
        this.elements.select.appendChild(child.cloneNode(true));
      }
    });
  }

  handleChange(e){
    this.dispatchEvent(
      new CustomEvent("change", {
        detail: this.value,
        bubbles: true,
        composed: true,
      })
    );
  }

}
customElements.define("my-element", MyElement);

This than can be used like this:

<my-element>
  <option>Option 1</option>
  <option>Option 2</option>
</my-element>