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

lit-html-axe

v0.3.0

Published

Write html tag as function with lit-html.

Downloads

1

Readme

lit-html-axe

Not ready for production yet. API should change.

Write html tags as function instead of string with lit-html.

Inspired by @hyperapp/html and hyperaxe.

Example

Using lit-element, html-tags and classnames:

import { LitElement, customElement, property, css } from "lit-element";
import htmlTags from "html-tags";
import htmlVoidTags from "html-tags/void";
import classnames from "classnames";
import { h, x } from "lit-html-axe";

// axe-nize html tags
const { main, header, h1, p, br, input } = x([...htmlTags, ...htmlVoidTags]);
// axe-nize cutom tag
const awesome = x('my-awesome-element');

@customElement("test-element")
export class TestElement extends LitElement {
  @property()
  title = "This is a test.";
  @property()
  username = "guest";

  static get styles() {
    const color = css`green`;
    return css`
      h1 {
        color: ${color};
      }
    `;
  }

  render() {
    // ----------------------------------------------------------------------
    return main({ id: "main", "class": classnames("main", "container") }, [
      header(h1(this.title)),
      p([
        h("span", `Hello, ${this.username}.`),
        br(),
        input({ type: "text", value: this.username, "@input": e => {
          const value = e.target.value;
          if (value != null && value !== "") {
            this.username = value;
          }
        } }),
        input({ type: "checkbox", "?checked": this.username === "guest" }, "default name"),
        br(),
        awesome({ "class": classnames("awesome") }, "My Awesome Element is HERE!") /* any custom tag is available */
      ])
    ]);
    // ----------------------------------------------------------------------
  }
}

Pros

  • Compatible with h() of hyperapp
  • Using x(), everything becomes function and thus easy to compose or wrap them up
  • Editor support for highlighting, word-completion and type-hints

Cons

  • Not compatible with hyperscript yet
  • See below for known performance issues

Installation

Not ready for production yet. API should change

yarn add lit-html-axe

Usage

import { axe, x } from "lit-element-axe";


// use x() with any tag name
const p = x("p");

// default is div
const div = x();

// or pass list of tag names
const { main, h1, input } = x(["main", "h1", "input"]);

// rename it when tag name is too long or a keyword
const {
  button: btn,
  "var": variable,
  "my-some-useful-custom-element": mine
} = x(["var", "button", "my-some-useful-custom-element"])

// if the argument is empty list, it returns {}
x([]) // -> {}

// pass content
p("Here goes content."); // -> html`<p>Here goes content.</p>`

// or list of contents
p([
  "Hi, there.",
  br(),
  "This is a content."
]);
// -> html`<p>Hi, there.<br>This is a content.</p>`

// specify attributes as object
h1({ id: "title", class: "header-title" }, "Title");
// -> html`<h1 id="title" class="header-title">Title</h1>`

// list of contents are also worked
p({ class: "text" }, [
  "Lorem ipsum",
  br(),
  "... and I don't know the rest."
]);
// -> html`<p class="text">Lorem ipsum<br>... and I don't know the rest.</p>`


// a bit complex one
main({ id: "main", class: "container" }, [
  h1({ id: "title", class: "container__title" }, "Example"),
  div({ class: "container__content" }, [
    p({ id: "status", class: "container__content--status" }, [
      "Button pressed?",
      html`<br>`
      input({ type: "text", "value": main_btn_state ? "clicked" : "not clicked" })
    ]),
    btn({ id: "main_btn", "@click": main_btn_clicked }, "Click me!"),
    mine(variable("some_variable"))
  ])
]);

Documentation

Generate with following command and see docs/.

yarn run doc

Or, visit https://kurousada.gitlab.io/lit-html-axe.

For Developpers

We recommend to use Linux/Mac because some npm scripts are written in Bash.

Installation

git clone https://gitlab.com/kurousada/lit-html-axe
cd lit-element-axe
yarn install --production=false

Test

Not yet prepared

yarn run test

Build

yarn run build  # see `build/` then

Performance issues

html() of lit-html caches DOM based on given values. However we have no way to know which to cache in h() of lit-html-axe because everything becomes string before given to it.

Thus, every value of all attributes and every contents will be cached. Also when a text contains a text binding, whole text will be cached instead of the text binding itself.

For example:

const v = "variable text";

const render = () =>
  h("p", [
    `value is ${v}`,
    h("input", { "type": "text", ".value": v })
  ]);

is equivalent to

const v = "variable text";

// Even static string will be cached although they won't change
// and whole text containing text binding will be cached too
const render = () =>
  html`<p>${
    "value is variable text"
  }${
    html`<input type="${"text"}" .value="${v}"></input>`
  }</p>`;

So it's better to write

const v = "variable text";

const render = () =>
  h("p", [
    `value is `,
    v,
    h("input", { "type": "text", ".value": v })
  ]);

We haven't measure how much it costs exactly, but it's recommended to use html() when you need efficient caching.

This will be solved (or at least improved) in the future.

License

MIT License (see LICENSE.txt)