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

c-minor

v1.4.2

Published

Infinitesimally small package for creating HTML elements.

Downloads

16

Readme

c-minor

Infinitesimally small package for creating HTML elements

NPM package minimized gzipped size  NPM version info  NPM license info  Test status

c-minor is an extremely small and lightweight JavaScript package that enables the creation and modification of HTML elements with a concise and simple syntax.

It supports the addition of event listeners, attributes, properties, and children all while having a production size of under 300 bytes before compression! You can view an editable live demo at JSFiddle.

Documentation

Signatures

c(element, attrs, ...children)

c(element, ...children)

element

Type: string | HTMLElement

An existing HTMLElement or string to use as the tag name of a newly created element. If tag name is provided, an XML namespace can be specified using the syntax 'TAG@NAMESPACE'. For example, 'svg@http://www.w3.org/2000/svg'.

attrs

Type: { [key: string]: any } | undefined | null

An object containing key-value pairs of attributes, properties, and event listeners to add to the element.

Properties are prefixed with $. For example, $date: new Date() will set the date prop of the element to a newly created Date object.

Event listeners are prefixed with _. For example, _click: e => console.log(e) will add a click event listener that logs the event object to the console.

Keys without prefixes are used for attributes. For example, class: 'foo' sets the class attribute of the element to foo.

...children

Type: ( string | HTMLElement )[]

The remaining arguments are for children to append to the element. A child may be either a string or an HTMLElement. If the child is a string, it is converted to a Text node before being appended. This means strings containing HTML syntax can be safely appended to the element. Passing no children is permitted.

Return value

Type: HTMLElement

Returns value of element if it was a HTMLElement or the HTMLElement created by the function if element was a string.

Unsafely setting HTML from a string

If you want to set the HTML value of the element using a string, you can add a $innerHTML: '<h1>Unsafe HTML</h1>' entry to the attrs argument. Only do this for trusted HTML as this opens up possibilities for XSS attacks.

Example

c(
  document.body, // Existing element
  {
    style: 'color: red' // Attribute
  },
  c( // Child element
    'div', // New DIV element
    {
      _click: e => alert(e.target.prop), // Event listener
      $prop: 7 // Property
    },
    'Foo', // Child string
    c('span', 'Bar') // Attributeless element
  )
);

No-Transpilation Support

c-minor supports browsers that implement the append function on Element without transpilation. That is:

  • Chrome: >= 54
  • Edge: >= 17
  • Firefox: >= 49
  • Internet Explorer: None
  • Opera: >= 41
  • Safari: >= 10

Development Commands

  • npm run build - Generate publishable version of c-minor in the dist directory.
  • npm run test - Run both code tests and c.d.ts type tests.
  • npm run size - Determine the size of the files produced by the build script.