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

ez-elements

v0.1.1

Published

[![Build Status](https://travis-ci.org/ez-elements/ez-elements.svg?branch=master)](https://travis-ci.org/ez-elements/ez-elements) [![Coverage Status](https://coveralls.io/repos/github/ez-elements/ez-elements/badge.svg?branch=master)](https://coveralls.io

Downloads

7

Readme

ez-elements

Build Status Coverage Status npm version languages

ez-elements is a thin wrapper for the built-in HTMLElement class that aims to simplify writing imperative element creation and manipulation approachable in TypeScript.

If you find yourself writing a lot of document.createElement and dealing with the low-level built-in HTMLElement class then ez-elements can simplify a lot of the boilerplate code you're writing.

Getting Started

As an example, lets look at using document.createElement directly to create a div with two spans inside that is then appended to document.body:

const someDiv = document.createElement('div');
someDiv.classList.add('some-div');
const spanOne = document.createElement('span');
spanOne.classList.add('some-span');
spanOne.textContent = 'Hello ';
const spanTwo = document.createElement('span');
spanTwo.classList.add('some-span'); 
spanTwo.textContent = 'World';
someDiv.append(spanOne, spanTwo);
document.body.appendChild(someDiv);

By using the ez function from ez-elements we can simplify this to the following:

import { ez } from 'ez-elements';

let spanOne, spanTwo; // variables not required, but you can assign them inside the append
const someDiv = ez('div', 'some-div').append(
  spanOne = ez('span', 'some-span').setTextContent('Hello '),
  spanTwo = ez('span', 'some-span').setTextContent('World'),
).appendTo(document.body);

Install

npm install ez-elements --save

Alternatively, you can install just the parts of the package that you want:

  • @ez-elements/core contains ez, EZElement, EZDiv and EZSpan.
  • @ez-elements/inputs contains EZTextInput and EZButton.
  • @ez-elements/jsx contains JSX.
  • @ez-elements/shadow contains EZShadowElement and extractStyleContents.

Examples

Check out the Examples page.

How it works (and why you should care)

The ez function has the following interface:

  • First argument is one of:
    • A HTML element tag name
    • A HTMLElement
    • An EZElement instance
  • Optional second argument is one of:
    • a single class name string
    • array of class names as strings
    • an object of class name to active bool
  • Returns an EZElement instance.
function ez<T extends keyof HTMLElementTagNameMap>(
    arg: T | HTMLElementTagNameMap[T] | EZElement<T>, 
    classes?: string | Array<string> | { [key: string]: boolean },
): EZElement<T>

You can also construct EZElement instances directly using:

const element = new EZElement('div');

Or extend EZElement to create components:

class SomeComponent extends EZElement<'div'> {
  constructor(text: string) {
    super('div');
    
    this.addClass('some-component').append(
        ez('span').setTextContent(text),
    );
  }
}

const instance = new SomeComponent('Hello World');
instance.appendTo(document.body);

The EZElement instance is a wrapper for a HTMLElement and provides a builder pattern interface to allow chaining, including appending/prepending children, appending to parents, adding classes, adding styles etc.

The reason you should care about what is happening under-the-hood is so that you can:

  • Meaningfully debug issues relating to usage of the wrapper.
  • Reason about the performance of your code without reading framework-specific guides or having a dedicated profiler/extension.
  • Work around shortcomings of the wrapper where appropriate.

Intent

The intent of the wrapper is to be thin and as close to stateless as possible whilst remaining useful.

The priorities of the package are to enable your code to be (in descending priority order):

  • Debuggable
  • Readable
  • Performant
  • Brief (as little code as possible)

Development

This repository uses lerna to manage the multiple packages contained within it.

# Installs dependencies *and links the packages together using lerna*
npm install  

# In one terminal - watch and rebuild the packages upon changes
npm run watch

# In another terminal - start a parcel server that serves a  single page app with some examples
cd examples
npm start