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

tram-deco

v6.0.1

Published

Declarative Custom Elements using native Web Component APIs and specs

Downloads

221

Readme

Tram-Deco

Declarative Custom Elements using native Web Component APIs and specs.

Tram-Deco provides a more elegant interface for building Web Components, that remains as close as possible to the existing browser APIs. Tram-Deco is an experiment to determine the value of a declarative interface for building Web Components, without the addition of APIs that don't already exist.

Example

<!-- include the Tram-Deco library -->
<script src="https://unpkg.com/tram-deco@6"></script>

<!-- define some web components -->
<template id="componentDefinitions">
  <!-- definition for a custom-title tag! -->
  <custom-title>
    <!-- declarative shadow dom for the insides -->
    <template shadowrootmode="open">
      <!-- styles, for just this element -->
      <style>
        h1 {
          color: blue;
        }
      </style>

      <!-- dom, to show on the page -->
      <h1>
        <slot>Hello World</slot>
      </h1>
      <hr />
    </template>

    <!-- scripts, that let you define lifecycle methods -->
    <script td-method="connectedCallback">
      this.shadowRoot.querySelector('slot').addEventListener('slotchange', () => {
        document.title = this.textContent || 'Hello World';
      });
    </script>
  </custom-title>
</template>

<!-- process the template to generate new definitions -->
<script>
  TramDeco.processTemplate(componentDefinitions);
</script>

<!-- use our new element! -->
<custom-title>Tram-Deco is Cool!</custom-title>

Live on Codepen

How to use

There are two ways to use Tram-Deco in your project - you can either have component definitions in your served HTML template (in template tags), or you can export the components as part of a build step to be imported with script tags.

Template Component Definitions

If you don't want a build step, or are just building components for a dedicated static page, you can do the following to write component definitions in your main template:

Include the Tram-Deco library (you can point to either tram-deco.js or tram-deco.min.js)

<script src="https://unpkg.com/tram-deco@6/tram-deco.min.js"></script>

Create a template tag with your component definitions, and then use Tram-Deco to process that template

<template id="myDefinitions">
  <!-- component definitions -->
</template>

<script>
  TramDeco.processTemplate(myDefinitions);
</script>

Export JS Definition

[!important]

Tram-Deco import depends on setHTMLUnsafe, which is a recently released feature. Check caniuse.com to understand browser support and coverage here.

If you want to export your component definition, to be used in other projects, or to organize the components in different files, you can do the following:

Create a component definition file (.html) - this can include as many top-level component definitions as you'd like.

<!-- my-counter.html -->
<my-counter>
  <template shadowrootmode="open">
    <!-- ... -->
  </template>
</my-counter>

Run the following command in the command line, or as part of a build step:

npx tram-deco export-components my-counter.html

This will create a JS file that can be imported using a standard script tag:

<script src="./my-counter.js">

API

JS API

The processTemplate function takes in a single template tag with several component definitions, and builds Web Component definitions for all of them in the global custom elements registry.

The define function takes in a single element definition and builds a Web Component definition for the global custom elements registry. It is the underlying method used in processTemplate, and probably does not need to be called in isolation.

Component API

These attributes can be used to provide logic for different life cycle events of your component. They follow the standard API for Web Components.

Attribute to be used on a script tag in your component definition. This assigned property name will be attached to the element as a static property, and can be useful for adding observedAttributes, formAssociated, disableInternals, or disableShadow. You can also define custom static properties for your element.

Attribute to be used on a script tag in your component definition. The assigned method name will be attached to the element, and can be useful for adding to the constructor, or setting other Web Component APIs, such as connectedCallback, disconnectedCallback, adoptedCallback, or attributeChangedCallback. You can also define custom methods for your element.

Example Using Component API

<script src="https://unpkg.com/tram-deco@6"></script>

<template id="myCounter">
  <my-counter>
    <!-- observed attributes, to watch for attribute changes on count -->
    <script td-property="observedAttributes">
      ['count'];
    </script>

    <template shadowrootmode="open" shadowrootdelegatesfocus>
      <button><slot>Counter</slot>: <span>0</span></button>
    </template>

    <!-- when we mount this component, add an event listener -->
    <script td-method="connectedCallback">
      const button = this.shadowRoot.querySelector('button');
      button.addEventListener('click', (event) => {
        const newCount = parseInt(this.getAttribute('count')) + 1;
        this.setAttribute('count', newCount);
      });
    </script>

    <!-- when the count updates, update the template -->
    <script td-method="attributeChangedCallback">
      const span = this.shadowRoot.querySelector('span');
      span.textContent = this.getAttribute('count');
    </script>
  </my-counter>
</template>

<script>
  TramDeco.processTemplate(myCounter);
</script>

<my-counter count="0">Tram-Deco</my-counter>

Live on Codepen

contributions / discussions

If you think this is useful or interesting, I'd love to hear your thoughts! Feel free to reach out to me on mastodon, or join the Tram-One discord.