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

litter-g

v0.0.36

Published

Use lit-html inside an html element

Downloads

90

Readme

Published on webcomponents.org

Actions Status

litter-g

Use Case

The use case for litter-g is spelled out quite nicely by React's introductory pages:

The majority of websites aren’t, and don’t need to be, single-page apps. With a few lines of code and no build tooling, try React in a small part of your website. You can then either gradually expand its presence, or keep it contained to a few dynamic widgets.

In one of their examples they show how you can use React inside a script tag. litter-g does something similar, but with lit-html.

Note: litter-g hopes to improve on the ergonomics of a similar component, xtal-method. Both components are meant for applications that aren't necessarily built on a JavaScript-centric paradigm. Applications that may be built using a server-centric framework, which just wants to strategically shake some JavaScript pixie dust as needed. Or maybe applications that are built using some "old" framework like Cold Fusion, but want to slowly convert into something more modern.

Syntax

The litter-g element applies its services onto the next sibling element:

<litter-g id=pronounList></litter-g>
<ul>
    <script nomodule>html`${input.map(i => html`<li>${i}</li>`)}`</script>
</ul>
...
<script>
    pronounList.input = ["He", "She", "They", "Other"];
</script>

Here, we are relying on the fact that giving a DOM element an id ("pronounList"), that id becomes a global constant, if the DOM element is outside ShadowDOM.

You can also specify the input via a JSON attribute:

<litter-g input='["He", "She", "They", "Other"]'></litter-g>
<ul>
    <script nomodule>
        html`${input.map(i => html`<li>${i}</li>`)}`
    </script>
</ul>

The snippet of lit syntax contained within the script child becomes the innerHTML renderer, and it re-renders anytime the input property changes.

Demo

        <p-d on=input to=[-_latitude] from=label  m=1></p-d>
    </label>
    
    

    <label>
        Longitude:
        <input aria-placeholder=Longitude placholder=Longitude value=12.452818>
        <!-- pass down (p-d) input value to _longitude property of div#long_lat -->
        <p-d on=input to=[-_longitude] from=label m=1></p-d>
    </label>

    
    <div -_latitude -_longitude >
        <script nomodule data-lit>
            tr = ({_latitude, _longitude}) => html`
                <a href="http://www.gps-coordinates.org/my-location.php?lat=${_latitude}&lng=${_longitude}" target="_blank">
                    (${_latitude},${_longitude})
                </a> 
            `;
        </script>
    </div>


    <style>
        .fieldInput{
            display:flex;
            flex-direction: row;
        }
        label{
            display:flex;
            flex-direction:row;
        }
    </style>
    <script type="module" src="https://unpkg.com/[email protected]/litter-g.js?module"></script>
    <script type="module" src="https://unpkg.com/[email protected]/p-d.js?module"></script>
</div>

Directives

All the lit-html directives that are part of the lit-html library are available for use.

Event Handling

It's a bit "hackish", but you can add event handlers, if you are careful to demark where the event handlers end, and the template begins, via the "magic string" //render:

<litter-g input='["He", "She", "They", "Other"]'></litter-g>
<ul id="pronouns">
    <script nomodule>
        function clickHandler(e){
            console.log(e);
        }
        //render
        html`${input.map((item, idx) => html`<li @click="${clickHandler}" id="li_${idx}">${item}</li>`)}`
    </script>
</ul>

Multivariable Functions

Sometimes we want a ui element to depend on more than one input parameter.

...
<litter-g id=mapCoordinates></litter-g>
<div>
    <script nomodule>
        tr = ({latitude, longitude}) => html`
            <a href="http://www.gps-coordinates.org/my-location.php?lat=${latitude}&lng=${longitude}" target="_blank">
                (${latitude},${longitude})
            </a> 
        `
    </script>
</div>

and does the following:

  1. Dynamically adds properties latitude, longitude to the litter-g instance.
  2. Updates the input property any time either of those properties change (with a little debouncing TODO), thus causing lit-html to re-render.

NB I: The "tr = " is optional text. This allows VSCode to recognize the expression, and provide helpful syntax coloring, autocomplete, etc. tr stands for "template result."

You can also add event handlers just as before, separated by the //render comment.

IE11 Support

Viewing Your Element

$ npm install
$ npm run serve

Running Tests

$ npm run test