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

be-having

v0.0.15

Published

Apply element decorators / behaviors from a distance.

Downloads

5

Readme

be-having

NPM version How big is this package in your project?

Apply element decorators / behaviors from a distance.

be-having allows us to have our cake and eat it too. Let me explain.

A particular conundrum of modern web development, as I see it, has to do with the proper role two missing pieces of the platform should play -- custom attributes / decorators / behaviors, and its relationship to declarative template instantiation.

This dilemma is expounded on in great detail here, and the syntax be-having supports is identical to what is outlined in that dissertation.

In a nutshell, be-having allows us to consolidate lots of little inline binding of custom attributes, into one, centralized (JSON) blob. This has the following advantages:

  1. It becomes economical to edit the JSON blob as a single *make.mjs file, which, with the help of a build process, can be converted into a JSON import. Even better, we can benefit from compile time checks, autosuggest editing if we use a *.make.mts during development, which TypeScript compiles into *.make.mjs, and then some separate process compiles to a JSON blob file (make.json?).
  2. This blob can be shared, and parsed once, in the case that the template it enhances is repeated multiple times throughout the page, which is often the case with web components.
  3. The "non-verbal" spells defined by the make definition can be applied during template instantiation, if the dependencies have been loaded already (ideally), but if not, Ein Ba’aya, just apply the rules to the live DOM tree after template instantiation has completed (and the dependencies have finished loading post instantiation).

Unfortunately, support for JSON module imports, across browsers, is coming slower than I would have ever expected, so the extra energy required to be backwards compatible doesn't seem worth it for now. But please do apply a little "magical futuristic extrapolation" in the following discussion, when we talk about "make.ts." The end goal is for that file to be JSON, not JavaScript.

be-having is a central player in supporting declarative custom elements.

Real world examples:

Side Nav (drawer component):

html make.ts

Toggle Element:

html make.ts

Tree Context Menu:

html make.ts

Declarative-ish element that requires a little custom JavaScript in the class:

html, with the class defined within the script tag make.ts

Declarative-ish element that requires a little custom JavaScript in the class, using tertiary file:

html, with minimal clutter make.ts Diamond.ts

Lingo

The following example is a better introduction than the links above, in that the problem space it is solving is much smaller

<script nomodule be-having='{
    "make": {
        "button": {
            "be": "counted",
            "having": {
                "transform": 
            }
        },
        "form": {
            "be": "reformable",
            "having": "formHavingProps"
        }
    }
}'> 
    import 'be-reformable/be-reformable.js';
    import 'be-counted/be-counted.js';
    
    import {make} from 'my-package/make.js';

    //someday (sigh), when the other two browsers finally implement json modules, can switch to json
    export const make = ['my-package/make.js', 'https://esm.run/[email protected]/make.js']; 
    export const counted = {
        impl:  'be-counted/be-counted.js';
    };
    export const reformable = {
        impl: ['be-reformable/be-reformable.js', 'https://esm.run/[email protected]']
    }

    export const formHavingProps = {
        
    };
</script>

Awaiting [Untested]

There are some scenarios where we really would rather wait for one or more dependencies to finish loading before proceeding with the instructions defined in the make file.

To enable this, set await to true:

export const inquiring = {
    impl: ['be-inquiring/be-inquiring.js', 'https://esm.run/[email protected]/be-inquiring.js'],
    await: true,
}

Linking Tags to Templates or host methods (for example)

be-having supports a special selector:

"<>":

What this does: An element matches this selector if:

  1. The element has an href attribute matching the tag name, and there exists an element whose id matches the tag name in the Shadow DOM realm. or
  2. If the tag name matches a method name of the host, surrounded by angle brackets

be-having will also remove attribute href if the element doesn't natively support the href property once found. This allows developers to benefit from a little DX assistance, by adding href="#[id]" to the tag. VS Code (and hopefully most decent HTML editors) provide support for jumping to the element.