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

silla

v0.0.3

Published

A super simple, super small, and pretty useful tool for building dom elements in JavaScript

Downloads

9

Readme

silla.js

A super simple, extremely small, and pretty useful tool for building dom elements in JavaScript

Getting Started

Install

npm i silla

Usage

import $ from 'silla';
$('h1', 'Hola, Mundo');

Todo List Example

const items = ['Start using silla', 'Focus on my project'];
$('section', () => {
  $('header', () => {
    $('h1', 'Todo List');
    const input = $('input type="text" placeholder="Add a new item"');
    $('button.add', 'Add').onclick = () => {
      $('li.todo-item', input.value, ul);
    };
  });
  const ul = $('ul.todo-items', () => {
    items.forEach(item => $('li.todo-item', item));
  });
});

Syntax

$(ELEMENT_STRING, CONTENT, ATTACH_NODE, REFERENCE_NODE);

Returns the created DOM Element

ELEMENT_STRING

Text-representatin of the element being created, in the following order:

  1. Tag. Defaults to div.
  2. Classes. Prefixed with a ., as in .first.second.third
  3. Id. Prefixed with a #, as in #first-name
  4. Attributes. Each is separated by a space, and functions the same as when createad in an HTML tag.
Examples:
  • $('div') or $('') or $()

    • <div></div>
  • $('div.block') or $('.block')

    • <div class="block"></div>
  • $('h1.title')

    • <h1 class="title"></h1>
  • $('input type="checkbox" checked')

    • <input type="checkbox" checked>
  • $('i.icon.close#close-icon aria-hidden="true" title="Close Icon"')

    • <i id="close-icon" aria-hidden="true" title="Close Icon" class="icon close"></i>

CONTENT (optional)

Either a function or string. (non-element value will be converted to a string)

Content as text:
  • $('span', 'Hello, World')<span>Hello, World</div>
  • $('button', 'Click Me')<button>Click Me</button>
  • $('button', 42)<button>42</button>
Content as a function:

Passes the dom element as an arg. This is usually unnecessary, since elements are automatically appended to the containing element. However, in cases of delayed execution (timeout, event callback, etc.) it can often be useful.

    $('ul.items', ul => {  // ul unused in this example
        $('li', 'Item 1');
        $('li', () => {
            $('span', 'Item');
            $('span', '2');
        });
    });

Output:

<ul class="items">
    <li>Item 1</li>
    <li>
        <span>Item</span>
        <span>2</span>
    </li>
</ul>

Note: If content arg is not specified, ATTACH_NODE and REFERENCE_NDOE can be used as 2nd and 3rd arguments, r espectively, as in this example:

$('input', document.body);

ATTACH_NODE (optional)

Determines which parent node to attach to. If not specified, attaches to the node in our current context. The context node is either the node for which we're currently executing its content function, or it is document.body.

$('section', () => {
   $('h1', 'This element is a child of section');
   $('.popup', '...and this is a child of body', document.body);
});

Be careful with code that has delayed execution, since its context is lost. For this reason, it's often useful to use the node argument to attach it explicitly.

$('section', section => {
   $('button', 'Click Me').onclick = () => {
      $('h1', 'This element gets appended to document.body');
      $('h1', 'Wherease this will get appended in section', section);
   };
});

REFERENCE_NODE (optional)

Rather than appending to the ATTACH_NODE you can insert before this reference node.

$('section', section => {
   const button = $('button', 'Insert Above')
   button.onclick = () => {
      $('div', `Item ${section.children.length}`, section, button);
   };
});