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

makeastatement

v0.1.0

Published

super simple state management for vanilla JS

Downloads

1

Readme

STATEMENT

super simple state management for vanilla JS

npm License

STATEMENT makes it really easy to add reactive, state-based elements to your vanilla JS projects.

Assigning a variable to a new 'Statement' object will automatically render the associated HTML template whenever any of its values change.

const counter = new Statement{
    state: { value: 0 },
    element: document.body,
    template: counter => html`<h1>The count is ${counter.value}</h1>`
}

counter.value ++

Instatllation

Using NPM CLI

Install statement from NPM.

npm install statement

Then import like this:

import { Statement, html } from "statement";

ES Modules

If you use ES Modules, you don't need NPM. You can import from a CDN URL in your browser or on CodePen.

<script type="module">
  import { Statement,html } from 'https://cdn.skypack.dev/statement';
</script>

Making a Statement

To create a Statement, all you need to do is assign a variable to the constructor function using the new keyword and provide state, element and template arguments:

const data = new Statement(
    state,
    element,
    template
)

These 3 arguments are all that's needed to

state is an object that contains your inital state. e.g.

{
    name: "Statement",
    version: 1.0,
    difficulty: "easy"
}
  • element is the element that you want the template to be rendered inside. e.g.
document.getElementById("data-container")

template is a function that returns a string of HTML that will be rendered inside the target element. It uses the html tag function provided by µhtml that accepts a template literal as an argument and returns a string of HTML that depends on the value of the properties in state. The template literal contains the HTML code to be displayed inside element and uses ${expression} placeholders to insert properties of state. e.g.

data => html`<h1>${data.name}</h1>`

Putting all this together would look like the following:

const data = new Statement(
    state: {name: "Statement"},
    element: document.getElementById("data-container"),
    template: data => html`<h1>${data.name}</h1>`
)

Creating the statement will trigger the initial render of the template inside the element using the initial values provided as state:

<div id="data-container">
  <h1>Statement</h1>
</div>

Statement

Once you have created a statement, you can then make any changes to the variable you assigned it to as you usually would using JavaScript and the view will update to reflect those changes. For example, you could assign one of the properties to a different value:

data.name = "Super Statement"

This would result in the HTML inside the "data-container" element automatically updating to reflect this change:

<div id="data-container">
  <h1>Super Statement</h1>
</div>

Super Statement

When creating a Statement, all you need to remember is the mneuomonic S.E.T.

  • state
  • element
  • template

Simple Counter Example

You can see this example live on CodePen.

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Statement Counter App</title>
        <script type="module" src="app.js"></script>
    </head>
    <body>
        <h1>Statement</h1>
        <h2>Counter Example</h2>
        <div id='counter'></div>
        <button id='down'>-</button>
        <button id='up'>+</button>
    </body>
</html>

app.js:

import { Statement,html } from "statement"

const counter = new Statement({
  state: { value: 10 },
  element: document.getElementById("counter"),
  template: counter => html`${counter.count}`
})                                                    
 
document.getElementById("down")
  .addEventListener("click",e => counter.value--)
document.getElementById("up")
  .addEventListener("click",e => counter.value++)

License

Released under Unlicense by @daz4126.