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

markup-as-js

v1.2.1

Published

A library that provides Flutter-like functions for creating DOM nodes

Downloads

1

Readme

Markup As JS

NPM version

A tiny, no-dependency library that provides simple functions for creating DOM nodes. Inspired by Flutter's "UI as Code" initiative.

Installation

npm install --save markup-as-js

Example Usage

import { div, p, b } from 'markup-as-js';

let el = div(
  {class: "foobar"}, //This is a set of key-value pairs representing the attributes of this node
  p(
    "I ", //String children are automatically converted to TextNodes
    b(
      "really"
    ), 
    " like cheese 🧀"
  )
);

console.log(el);

The above snippet creates an HTMLElement containing the following:

<div class="foobar"><p>I <b>really</b> like cheese 🧀</p></div>

Observables and Thenables

Observables (things that implement .subscribe()) and Thenables (things that implement .then(), like ES6 Promises) can be passed as node children or attribute values.

When the value is updated (for observables) or resolves (for thenables), the DOM node or attribute that the observable was passed to will be updated. This facilitates reactive programming. For example:

const counter = new SimpleObservable(0);
const isTen = new SimpleObservable(false);
counter.subscribe(v => isTen.set(v === 10));

return div(
  p(
    counter,
  ),
  button(
    {
      onclick: () => counter.set(counter.value + 1),
      disabled: isTen,
    },
    "Increment"
  ),
  new Promise(resolve => setTimeout(() => resolve("This text node shows up after five seconds"), 5000))
);

(SimpleObservable is a class provided by this library that is, unsurprisingly, a simple observable. It has subscribe and set methods and a value property, and the constructor takes the initial value.)

Important Note: If you ever discard a node that was passed an observable without also discarding the Observable (including unsubscribing it from any observables it is subscribed to), you must call .teardown() on the discarded node. Otherwise, the observable will still have a reference to the discarded node and it will not be garbage-collected, causing a memory leak.