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

element-f

v2.0.0

Published

Define your custom elements with elegance

Downloads

96

Readme

element-f

Define your custom elements with elegance 👒

Installation

npm i element-f

Basics

In order to define a custom-element, you only need one definition function:

import elementF from "element-f";

const MyElement = elementF(function(){
  // Your logic goes here  
  const shadow = this.attachShadow({mode: 'open'});
});

To tap into lifecycle events, this function can use the "life" event emitter:

const MyElement = elementF(function(life)=> {
    const shadow = this.attachShadow({mode: 'open'});
    // Listen once to when this component connects to a document 
    life.once('connect', ()=> shadow.innerHTML = `I'm Alive!`);
});

The "life" event emitter supports three methods:

  • once(name, fn)on(name, fn) - Registers fn for events of name name. once() will invoke fn once.
    • name - The name of the event to listen to
    • fn(payload) - The function to be called when an event occurs
      • payload - An object containing information regarding the event
  • off(name, fn) - Removes an event handler previously registered using on or once.

The following events are thrown:

  • connect - Fired upon connectedCallback. Delivers no payload.
  • disconnect - Fired upon disconnectedCallback. Delivers no payload.
  • attribute:[Attribute Name] - Fired when an observed attribute changes. Delivers previousValue and newValue as payload.

To observe attributes, just add their list to elementF call:

const MyElement = elementF(function(life)=> {
    life.on('attribute:foo', ({ previousValue, newValue })=> {
        // Do something when attribute "foo" changes value
    });

    life.on('attribute:bar', ({ previousValue, newValue })=> {
        // Do something when attribute "bar" changes value
    });
}, ["foo", "bar"]);

Usage Examples

Whereas defining custom elements using standard class notation looks like this:

class MyButton extends HTMLElement {
    
    constructor(){
      super();
      console.log(`I'm alive!`);
    }

    static get observedAttributes(){
        return ['disabled'];
    }
    
    attributeChangedCallback(name, oldValue, newValue) {
      if(name === "disabled") this.classList.toggle('disabled', newValue);
    }

    connectCallback() {
      this.innerHTML = "<b>I'm an x-foo-with-markup!</b>";
    }
}

With element-f the same custom element definition would look like this:

const MyButton = elementF(function(life)=> {

  console.log(`I'm alive!`);
  life.on('connect', ()=> this.innerHTML = "<b>I'm an x-foo-with-markup!</b>");
  life.on('attribute:disabled', ({ newValue, oldValue })=> this.classList.toggle('disabled', newValue));

}, ['disabled']);

Compact, functional and elegant 😇

What does Element-F solve?

Element-F is a stylistic framework, not a fundamental solution to any specific architectural or functional problem. If you're happy with OOP-styled constructs, you probably wouldn't draw much enjoyment from using it :)