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

@ryanmorr/elemental

v0.1.0

Published

A functional approach to creating autonomous custom elements

Downloads

30

Readme

elemental

Version Badge License Build Status

A functional approach to creating autonomous custom elements

Install

Download the CJS, ESM, UMD versions or install via NPM:

npm install @ryanmorr/elemental

Usage

Define an autonomous custom element by providing the tag name and an initialization function:

import elemental from '@ryanmorr/elemental';

// Define a custom element and return the class
const CustomElement = elemental('custom-element', (element) => {
    // Return an HTML string or DOM node to set the shadow root content
    return '<div>Hello World</div>';
});

// Create an element instance
const element = document.createElement('custom-element');

// Initialization function is called when the element is mounted to the DOM
document.body.appendChild(element);

Optionally define default properties:

elemental('custom-element', {msg: 'World'}, (element) => {
    // Default properties are added to the element instance on initialization
    return `Hello ${element.msg}`;
});

Default properties with a primitive value (string/number/boolean/null) are automatically reflected into attributes on initialization and observed for changes. After initialization, attribute changes are reflected into properties, but property changes are not reflected into attributes:

elemental('custom-element', {foo: 'abc', bar: 123, baz: {}}, (element) => {
    // Primitive default properties are reflected to attributes on initialization
    element.getAttribute('foo'); //=> "abc"
    element.getAttribute('bar'); //=> "123"
    element.getAttribute('baz'); //=> null

    // A property assignment is not reflected into an attribute
    element.foo = 'xyz';
    element.getAttribute('foo'); //=> "abc"

    // Setting an attribute will be reflected into a property 
    // and convert the value to its natural type
    element.setAttribute('bar', '789');
    element.bar; //=> 789 (number not string)
});

Subscribe to DOM state changes via the observe method:

elemental('custom-element', {foo: 'abc', bar: 123}, (element) => {
    // Returns a function to stop future calls
    const stop = element.observe('mount', (parentElement) => {
        // Called everytime the element is mounted to the DOM
    });

    element.observe('unmount', () => {
        // Called everytime the element is unmounted from the DOM
    });

    element.observe('prop', (name, newVal, oldVal) => {
        // Called everytime a default property changes
    });

    element.observe('prop:foo', (newVal, oldVal) => {
        // Called everytime the "foo" property changes
    });

    element.observe('attr', (name, newVal, oldVal) => {
        // Called everytime a reflected attribute changes
    });

    element.observe('attr:bar', (newVal, oldVal) => {
        // Called everytime the "bar" attribute changes
    });
});

Use the html property to get and set the shadow root:

elemental('custom-element', (element) => {
    // Supports HTML strings
    element.html = '<div></div>';
    element.html.innerHTML; //=> "<div></div>"

    // Supports DOM nodes
    element.html = document.createElement('span');
    element.html.innerHTML; //=> "<span></span>"
});

Add scoped CSS to the custom element via the css property. It supports CSS strings, <style> elements, CSSStyleSheet instances, or an array of any of those types. Each assignment to the css property appends to the CSS and does not replace it:

elemental('custom-element', (element) => {
    // Add CSS via string
    element.css = `
        .foo {
            color: red;
        }
    `;

    // Appends <style> element to CSS
    const style = document.createElement('style');
    style.textContent = `
        .bar {
            color: blue;
        }
    `;
    element.css = style;

    // Optionally define CSS within the shadow root using <style> and/or <link> elements
    return `
        <link rel="stylesheet" href="custom-element.css">
        <style>
        .baz {
            color: green;
        }
        </style>
        <div class="foo"></div>
        <div class="bar"></div>
        <div class="baz"></div>
    `;
});

License

This project is dedicated to the public domain as described by the Unlicense.