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

@muban/template

v1.0.0-alpha.4

Published

Writing templates for Muban components during development

Downloads

411

Readme

muban-template

Please check the full documentation.

This library makes use of:

  • htm that provides a jsx-like syntax as tagged template string and convert it to JSX/Hyperscript to render it with vhtml.
  • vhtml that renders parsed JSX/Hypertext to a string.
  • eslint-plugin-lit and eslint-plugin-lit-a11y for linting support.

The main purpose of this library is to use together with @muban/muban while developing components before it's integrated on the server to be previewed in storybook, or when rendering static pages.

Getting started

Installing

Add @muban/template to your project:

yarn add @muban/template

Simple template

Create our template:

import { html } from "@muban/template";

type MyComponentProps = {
  welcomeText: string;
};

function myComponentTemplate({ welcomeText }: MyComponentProps, ref?: string) {
  return html`
    <div data-component="my-component" data-ref=${ref}>
      ${welcomeText}
    </div>
  `;
}

Exports

  • html – the tagged template string, the core of this library.
  • unsafeHTML – to render any "HTML content" coming from an API (that you trust) – doesn't work with html entities or the < content character.
  • jsonScriptTemplate – a helper function that renders a JSON script tag for an object that can be used instead of a lot of different data attributes, or to use objects as props.
  • ComponentTemplate – a type for a Muban Template Function.
  • ComponentTemplateResult – a type for the Template result (to be consumed by other code).
  • TemplateMap – a helper type to extract the "props type" out of a template function as part of a Record of templates, and formats them to be used in a name/props data structure.

About special characters

By default, the template compiler will escape any special characters into html entities when rendered, so they display as such on the screen;

html`<span>"</span>`;

is rendered as

<span>&quot;</span>

However, this doesn't work for the < character, since it will mess up the template parser.

Besides that, it's common practise to user html entities in your content;

html`<span>&quot;</span>`;

unfortunately this is double encoded now;

<span>&amp;quot;</span>

To work around this, we can use the dangerouslySetInnerHTML escape hatch;

html`<span dangerouslySetInnerHTML=${{ __html: '&quot;' }}></span>`;

which properly renders as

<span>&quot;</span>

Use this to render any html content that you trust (e.g. coming from CMS with sanitized output).

False values on data attributes

HTML data- attributes with false values will be stringified in the resulting HTML

html`<div data-test=${false}>foo</div>`

Will become

<div data-test="false">foo</div>

This only will happen for data attributes (attributes that start with data-)

html`<div data-test=${false} another-attribute=${false}>foo</div>`

Will become

<div data-test="false">foo</div>

Non data attributes with a false value will be ignored in the resulting HTML