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

bloc-them

v9.0.0

Published

BLoC Framework for creating Javascript based UI.

Downloads

12

Readme

Bloc-Them

The simplest, but mighty reactive UI framework, any JS developer can learn in less than 15 minutes!

Tutorial

The core theme of this framework is "Separation of concerns". Its provides JS classes to separately encapsulate Business logic and UI code.

Lets start with the code:

<!--Custom web component we created in below our script!-->
<counter-widget></counter-widget>
<script type="module">
    import {Bloc, ListenerWidget,html, render } from "./index.js";

    class CounterBloc extends Bloc{
        constructor(){
            super(0);
        }
        increment(){
            this.emit(this.state+1);
        }
        decrement(){
            this.emit(this.state-1);
        }
    }

    class CounterWidget extends ListenerWidget{
        constructor(){
            super({
                blocName:"CounterBloc",
                hostedBlocs:{
                    CounterBloc: new CounterBloc()
                }
            });
        }

        build(state){
            return html`
                <div>
                    <div>
                        <button @click=${()=>this.bloc().increment()}>increment</button>
                        <button @click=${()=>this.bloc().decrement()}>decrement</button>
                    </div>
                    <div>Result: ${state}</div>
                </div>`;
        }
    }
    //custom web component defined here
    customElements.define("counter-widget",CounterWidget);
</script>

What the above code does:

Business Logic is encapsulated in Bloc class (Business Logic Components) and the UI code logic in ListenerWidget.

  1. CounterBloc contains all your business logic (manges state of count).
  2. CounterWidget contains all the UI code.

After configuring, the ListenerWidget, it starts listening for changes in state from its subscribed Bloc. As soon as a bloc emit a new state, the listener widget will reactively update itself with new values.

CounterBloc

/**
 * Business logic must be encapsulated in Bloc class
*/
 class CounterBloc extends Bloc{
    constructor(){
        //initial state of the Bloc
        super(0);
    }
    increment(){
        //emit function 
        //provided in Bloc class 
        //to emit new state
        this.emit(this.state+1);
    }
    decrement(){
        //emit new state
        this.emit(this.state-1);
    }
}

CounterWidget

/**
 * ListenerWidget listens for state changes from blocs
 * In this case we have provided the blocName as `CounterBloc`
 */
class CounterWidget extends ListenerWidget{
        constructor(){
            super({
                blocName:"CounterBloc",//listen to this bloc
                hostedBlocs:{ //Listener UI can host different blocs for other nested UIs
                    CounterBloc: new CounterBloc()
                }
            });
        }

        build(state){
            //`html` is tagged template literal which helps in converting the code into HTML dom Nodes
            //@click is used to attach event listener to a particular node.
            //${state} can be used to get values 
            return html`
                <div>
                    <div>
                        <button @click=${()=>this.bloc().increment()}>increment</button>
                        <button @click=${()=>this.bloc().decrement()}>decrement</button>
                    </div>
                    <div>Result: ${state}</div>
                </div>`;
        }
    }

whats that html

html is a tagged template. Its actually plain old Javascript!, and not JSX. It helps in converting template strings into DOM Node.

No JSX , do it means it re-render entire nodes.

It do not uses virtual dom, but still it only modify those portion of UI nodes, which have been updated!!! Its comparable to virtual DOM, though using plain old vanilla javascript.

More about what can go inside html

You can add any valid HTML code inside html tagged template string. You can add event listeners using @ prefix: For example @touchmove to listen to touchmove event. You can add custom properties to a component using .: For example <my-app .config=${runtimeConfig}>, and whenever the runtimeConfig changes , it will automatically change the properties on my-app. You can also add attributes as such <div style=${"color:white;display:"+{true?"block":"none"}}> Also can add and aut remove optional attributes using ?: For example <div ?optional_att=${ifTrueThenShow}>.

And thats it!!!


THE END

See how this has been used to create complex UI web component library : use-them