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

plum-element

v0.0.7

Published

A tiny base class for web components to sync between properties and attributes

Downloads

13

Readme

plum-element

Build Status npm version Node.js Version

A tiny base class for web components to sync between properties and attributes. ⚡️ No dependencies, less than 500 bytes gzipped.

Installation

yarn add plum-element

Usage

class ToDoElement extends PlumElement {
  // Declare properties.
  static get plProps(): PlumPropDefs {
    return {
      // Define a string property named 'description'.
      description: 's',
      // Define a boolean property named 'completed'.
      completed: 'b',
    };
  }

  // Description property getter and setter.
  get description(): string {
    return this.getPLProp('description');
  }
  set description(val: string) {
    this.setPLProp('description', val);
  }

  // Completed property getter and setter.
  get completed(): boolean {
    return this.getPLProp('completed');
  }
  set completed(val: boolean) {
    this.setPLProp('completed', val);
  }
}
customElements.define('todo-element', ToDoElement);

PlumElement properties and attributes are automatically in sync:

<todo-element id="element" description="Fix bugs" completed></todo-element>

<script>
  const element = document.getElementById('element');

  // Get initial attributes.
  element.description; // 'Fix bugs'
  element.completed; // true

  // Property changes are reflected in attributes.
  element.description = 'Create bugs';
  element.getAttribute('description'); // Returns 'Create bugs'

  // Attribute changes are also reflected in properties.
  element.removeAttribute('completed');
  element.completed; // Returns false
</script>

Supported property types

  • s string.
  • n number.
  • b boolean. false removes the attribute.
  • a array. JSON is used for serialization / deserialization.
  • object object. JSON is used for serialization / deserialization.

NOTE: null or undefined property values always remove the attribute.

Property / attribute change callback

Whenever a property or attribute changes, these two functions are always called in pairs.

class MyElement extends PlumElement {
  // Called when a property is updated.
  // `attrToPropUpdate` true if this is triggered by an attribute change.
  onPLPropUpdated(name: string, oldValue: unknown, newValue: unknown, attrToPropUpdate: boolean) {}

  // Called when an attribute is updated.
  // `attrToPropUpdate` true if this is triggered by an attribute change.
  onPLAttributeUpdated(
    name: string,
    oldValue: string | null,
    newValue: string | null,
    attrToPropUpdate: boolean,
  ) {}
}

Just like the standard attributeChangedCallback, these two functions are also called before an element is connected, use HTMLElement.isConnected if you are only interested in changes when an element is connected. Example:

class MyElement extends PlumElement {
  connectedCallback() {
    // Handle property values all at once in `connectedCallback`.
  }

  onPLPropUpdated(name: string, oldValue: unknown, newValue: unknown, attrToPropUpdate: boolean) {
    // Ignore property changes when the element is not connected.
    if (!this.isConnected) {
      return;
    }
    // Handle property changes.
  }
}