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

lit-funce

v1.0.3

Published

A <sub>funky, not too funcy</sub> helper for writing functional web components using [lit-html](https://lit-html.polymer-project.org/).

Downloads

1

Readme

[lit-funce] lit-html Functional Custom Elements

A funky, not too funcy helper for writing functional web components using lit-html.

Installation

yarn add lit-html
yarn add lit-funce

Or optionally npm install lit-{html, funce}.
There is a peer dependency to lit-html.

Usage

Define a web component in functional style...

// abutton.js
import { funce, html } from 'lit-funce';

// register web component, declare observed attributes
funce("a-button", ['color'], aButton);

// The custom element is defined by the render function. 
// The function gets invoked on every update with the 'host' argument.
// Host is an instance of HTMLElement extended by custom attributes (passed in "funce()") and properties (passed in "Host.props()").
// Here 'init' and 'props' are host methods (see below), 'color' is an observed attribute, 'clicks' is a custom property (see init.props... below)
 function aButton({ clicks, color, init, props }) {
    
    const style = clicks ? `border-color:${color}; color:${color}` : '';

    const label = !clicks ?
        "please click" :
        `thank you ${clicks > 1 && `x ${clicks}` || ''}`;

    // 'init' is the same instance as 'host' but it is only injected on the first invocation (think connectedCallback)
    // The idiom "init?.foo" can be used to do something only once on first invocation.
    // Here the host gets extended by a new property 'clicks'
    init?.props({
        clicks: 0,
    });

    // The 'props' method can be used to define or update properties on the host
    // Here props is used to update he clicks property value on the host.
    function clicked() {
        return props({clicks: ++clicks});
    }

    // lit-html is used to render the result. The function must return a valid lit-html template result.
    // Here the lit-html @event directive is used to bind the click handler.
    return html`<button @click=${clicked} style=${style}>${label}</button>`;
}

...and use it

<!DOCTYPE html>
<html>
    <script type="module">
        import "./abutton.js"
    </script>

    <a-button color="blue"></a-button>
</html>

See demo...

Init and Dispose

Logic in connectedCallback and disconnectedCallback can be expressed using the init and dispose methods of the host element. The idiom init?.* can be used to invoke a method exclusively in the setup phase (think connectedCallback).

Clock example

<html>
    <script type="module">
        import { funce, html } from "../lit-funce.js";

        const millis = () => (Date.now() % 1000).toString().padStart(3, '0');

        funce("c-lock", ["interval"], clock);
        function clock({ init, interval, render }) {
            // call init?.props(p: object) to update or extend host properties;
            // an object result of a function is applied to `props` as well
            init?.props({
                timeout: setInterval(render, interval),
                stop: ({ timeout }) => ({ timeout: clearInterval(timeout) }),
                start: ({ interval }) => ({ timeout: setInterval(render, interval) })
            });
            // call init?.dispose(fn: (host) => any) to hook a dispose function (think disconnnectedCallback) 
            init?.dispose(({ timeout }) => clearInterval(timeout));

            return html`${new Date().toLocaleTimeString()}.${millis()}`;
        }

        window.$ = document.querySelector.bind(document);
    </script>

    <c-lock interval="1">Toggle</c-lock>

    <div>
        <button onclick="$('c-lock').start()">Start</Button>
        <button onclick="$('c-lock').stop()">Stop</Button>
    </div>
</html>

See demo...