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

widgetize

v2.0.1

Published

Custom Element based HTML5 Widgets and Views using Browserify

Downloads

7

Readme

Widgetize Build Status npm version

Custom Element based HTML5 Widgets and Views using Browserify or Webpack

Create reusable and encapsulated HTML5 Widgets and Application Views using HTML5, CSS3 and Javascript.

Simply npm install and require() / import a widget and it will be automatically included in your Browserify or Webpack project, for use in your HTML page as a custom element.

Install

npm install widgetize --save

You may also require a custom element polyfill.

npm install document-register-element --save

Usage

ES6 Syntax is optional

JS

import widgetize from 'widgetize';

/**
 * Time Widget
 */
class TimeWidget extends HTMLElement
{
    init() {
        this._timer = null;

        this._timeElement = null;
    }

    attach(dom, content) {
        this._timeElement = dom.querySelector('time');

        this._timer = setInterval(() => {
            this.invalidate();
        }, 1000);
    }

    update(dom) {
        this._timeElement.textContent = new Date();
    }

    detach(dom) {
        clearInterval(this._timer);
    }
}


export default widgetize('time-widget', TimeWidget, 'The Time is: <span></span>');

HTML

<html>
    <head>
        <script src="bundle.js"></script>
    </head>
    <body>
        <time-widget></time-widget>
    </body>
</html>

API

widgetize(tagName, constructor [, template] [, options])

Creates a widget and registers it with the browser. Returns a reference to the widget's constructor.

tagName String Tag name of the element to use in HTML. Must contain at least one -. e.g. my-tag

constructor Function Constructor function for the widgets definition. Can be null. Instances are created with the new operator.

template String Template to use for the elements HTML content.

options Object Options

.shadow String Whether to use the ShadowDOM if it is available. Defaults to true.

The created widget has a lifecycle which can be programmatically accessed by the object defined by the Constructor function.

The following instance methods are available to be overridden by the widget

init()

Called when a widget is created, either by being called with new Widget() or when parsed by the browser in the DOM.

A good place to initialise instance variables.

attach(dom, content)

Called when the widget is added to the DOM, either by being used with .appendChild(widget) or when rendered by the browser in the DOM.

dom is a reference to the element's shadow DOM if supported, or the element itself.

content is a Document Fragment containing any content the element had.

The place to make one time modifications to the widget DOM, available as dom, and to add event listeners.

update(dom)

Called after attach() and then once per Event Loop execution after a call to invalidate.

dom is a reference to the element's shadow DOM if supported, or the element itself.

The place to make updates to the DOM after a change of state of the widget.

By waiting for update() to be called, DOM updates for multiple changes of state can be scheduled together.

detach(dom)

Called when the widget is removed from the DOM, either by being used with `` or when the browser removes the element from the DOM.

dom is a reference to the element's shadow DOM if supported, or the element itself.

The place to clean up references and event listeners etc.

The following instance methods are available to be used by the widget

invalidate()

Invalidates the widget, so that update() will be called in the next Event Loop execution.

Multiple calls to invalidate() within the same Event Loop execution, will only tigger one call to update() in the next Event Loop execution.

Examples

Test

npm install
npm run install:examples
npm test