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

component-literal

v1.5.3

Published

A reactive, flexible, and dead simple way to create DOM components with template literals.

Downloads

20

Readme


COMPONENT LITERAL

Description

A flexible, and dead simple way to create reactive DOM components with template literals.

Components can be ultra simple or slightly more complex depending on the scenario. The slightly more complex style employs a reactive update method, and focuses on updating the data rather than the entire compenent itself. Updating the component's data will automatically trigger a re-render of the HTML, and will efficiently update the DOM using morphdom. All methods can be method chained together for ease of organization. Render and event methods will always update in the correct order, no matter how the code is organized on the page.


Usage

import { ComponentLiteral, Component } from 'component-literal';

Component <HTML/>

Arguments

The HTML string used to create the component. Must be valid HTML. Using tagged template literal syntax, dynamic values can be used via '${value}'. Values can be either strings, elements or a Component Literal. An element will be coverted to a string and injected as innerHTML. The HTML will be condensed and turned into an actual DOM element.


EXAMPLE

Component `<div class="collection-title">THE TITLE</div>`;

Class - ComponentLiteral(config)

Arguments

config (object) : The config object is an object literal comprising of data, and a render method. Also uses the shorthand sytax for creating Component Literal components. There must be a data object nested in the config as well an "html()" method. Updating the data will automatically re-render the html and bind any event handlers.


Method - render(target)

Arguments

target (string or DOM element) : Uses querySelectorAll. Will search for the target element and render if it exists in the DOM. Can be method chained from the Component.build() method.


Method - events(methods)

Arguments

methods (object) : Comprised of a event listener and a target, the syntax is as follows:

EXAMPLE

Component.events({
  'click .item' (e) {
    console.log(e);
  }
});

Method - update(props)

props (object) : When updating a component, use the same property key to update the data object nested in the build method.


EXAMPLE

import { ComponentLiteral, Component } from 'component-literal';

const list = new ComponentLiteral({
    data: {
      items: [
        { name: "red" }, 
        { name: "beach house" }, 
        { name: "novice" }
      ],
      classNames: {
        title: "novice",
      }
    },
    html() {
      return (
        Component `<div class="collection-list">
            ${this.data.items.map((item, i) => {
              return (
                `<div data-id="item_${ i + 1 }" class="item ${this.data.classNames.title}">
                  ${ item.name }
                </div>`
              );
            })}
          </div>`
      );
    }
  })
  .events({
    "click .expert" (e) {
      e.currentTarget.innerHTML = "clicked";
    }
  }).render("#main-content");

  list.update({ 
    items: [
      { name: "blue" }, 
      { name: "mountain cabin" }, 
      { name: "expert" }
    ], 
    classNames: { 
      title: "expert" 
    }
  });

morphdom

This module was created to solve the problem of updating the DOM in response to a UI component or page being rerendered. One way to update the DOM is to simply toss away the existing DOM tree and replace it with a new DOM tree (e.g., myContainer.innerHTML = newHTML). While replacing an existing DOM tree with an entirely new DOM tree will actually be very fast, it comes with a cost. The cost is that all of the internal state associated with the existing DOM nodes (scroll positions, input caret positions, CSS transition states, etc.) will be lost. Instead of replacing the existing DOM tree with a new DOM tree we want to transform the existing DOM tree to match the new DOM tree while minimizing the number of changes to the existing DOM tree. This is exactly what the morphdom module does! Give it an existing DOM node tree and a target DOM node tree and it will efficiently transform the existing DOM node tree to exactly match the target DOM node tree with the minimum amount of changes.

Learn more about morphdom