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

gas-html

v1.3.0

Published

A lightweight library for creating html elements, compatable with node and the browser

Downloads

28

Readme

gas-html

gas-html is a small library for easy creation and updating of HTMLElements. This project has been created to help provide an easier syntax for creating elements in small web projects. The core of gas-html is the h function which can be used to create elements with attributes, classes and styles. This library also exposes methods for standard html tags.

Installation

For Node:

npm i gas-html

For Browser Module: unpkg.com/gas-html@latest/dist/es2015/index.js

<script
  type="module"
  src="unpkg.com/gas-html@latest/dist/es2015/index.js"
></script>

or

import {h, span,...} from 'unpkg.com/gas-html@latest/dist/es2015/index.js'

For Browser without ES Modules: unpkg.com/gas-html@latest/dist/es5/index.js

<script src="unpkg.com/gas-html@latest/dist/es5/index.js"></script>

You can always copy the code from unpkg or github and edit to your needs.

Basic Syntax

The h function is a curried function. The first call expects either a string for a html tag, or an existing HTMLElement. The second call expects classes, styles and attributes. The third call expects children. This js

import { h } from "gas-html";
const myDiv = h("div")({
  id: "id",
  style: { minHeight: "10vh" },
  cls: "my-class",
})(h("span")()("some content"));

generates the following html:

<div id="id" style="min-height:10vh" class="my-class">
  <span> some content </span>
</div>

As mentioned, the library exports common html tags, so instead of using h('span'), you can import the span function, e.g.

import { div, span } from "gas-html";
const myDiv = div({ id: "id", style: { minHeight: "10vh" }, cls: "my-class" })(
  span()("some content"),
);

Classes

Adding a class to an element can be done in 3 ways:

  • Object syntax: Add classes using an object, where the key is the class to add and the value is a boolean, if the value is truthy then the key will be added to the class.
  • String syntax: Add classes as regular strings
  • Array syntax: Add an array of class definitions that are either objects, strings or arrays (recursively).

e.g.

import { span } from "gas-html";

const objectSyntax = span({ cls: { "is-added": true, "is-not-added": false } });
const stringSyntax = span({ cls: "class-1 class-2" });
const arraySyntax = span({
  cls: ["class-1 class-2", { "is-added": true, "is-not-added": false }],
});

More Examples

Basic table

import { table, thead, tr, th, tbody, td } from "gas-html";
const createTable = () =>
  table({ cls: "my-table" })(
    thead()(
      tr()(
        th({ style: { fontWeight: "bold" } })("Heading 1"),
        th({ style: { fontWeight: "bold" } })("Heading 2"),
      ),
    ),
    tbody()(
      tr()(td({ "data-key": 1 })("Body 1"), td({ "data-key": 2 })("Body 2")),
    ),
  );

Reuse a 'Styled' Element

import { table, thead, tr, th, tbody, td } from "gas-html";

const myTh = th({ cls: "my-th", style: { fontWeight: "bold" } });
const createTable = () =>
  table({ cls: "my-table" })(
    thead()(tr()(myTh("Heading 1"), myTh("Heading 2"))),
    tbody()(
      tr()(td({ "data-key": 1 })("Body 1"), td({ "data-key": 2 })("Body 2")),
    ),
  );

Update an existing element

import { span, img, h } from "gas-html";
const myIEl = i({ cls: "fas fa-spinner" })();
h(myIEl)({ cls: "fas fa-exclamation-triangle" })();

const myOtherEl = document.createElement("h1");
h(myOtherEl)({ cls: "this-works-too" })();

// add children to an existing element
h(myOtherEl)()(img({ src: "/dog.png" }), span()("Title"));

Registering Events using 'on'

import { btn, h } from "gas-html";
const myBtn = btn({
  on: {
    click: (e) => {
      console.log("Clicked", e);
    },
  },
})("My Button");

// remove event listener
h(myBtn)({
  on: {
    click: undefined,
  },
})();

Create a 'component' with 'props'

import { button, h, div } from "./index.js";
const MyBtn = (props) =>
  button({
    on: {
      click: (e) => props.onClick(props, e),
    },
  })(props.name);

const myCounter = MyBtn({
  name: "1",
  onClick: (props) => {
    props.name++;
    h(myCounter)()(props.name);
  },
});