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

routed-enhancement

v0.1.3

Published

This router helps to progressively enhancement DOM elements on a webpage. A router is used to upgrade the elements. The router has multiple methods to instantiate a controller that is responsible for the behaviour of the elements.

Downloads

36

Readme

Routed enhancement

This router helps to progressively enhancement DOM elements on a webpage. A router is used to upgrade the elements. The router has multiple methods to instantiate a controller that is responsible for the behaviour of the elements.

Constructing a new router

Instantiating a new router is easy. An see the an easy example below.

import Router from 'routed-enhancement';

// instantiate the route
let namespace = 'my-namespace';
let router = new Router(namespace);

// now we can add routes
router.selector('nav', 'nav-controller');

// dispatch the routes (once the dom is ready, which is been taken care of in the router). 
router.dispatch();

When the router from the example is dispatched, it will select all the nav elements on the webpage. If there is more 1 element it available, it will import the module my-namespace/nav-controller. The controller is required to have the following interface.

export default class {
  
  process($selector) {
    
  }
  
}

The variable $selector contains the list elements found on the page as a jquery object.

Available methods

The following methods are available on the router.

Selector method

The selector method looks for available DOM elements on the page. Only when at least 1 element is found, the controller is imported and executed. The selector is not a live selector. So the element has to be on the page when the page is ready (or when the router dispatched, but that is true in almost every case).

selector(cssSelector, moduleNameOfController);

Example

// in the router
router.selector('nav', 'nav-controller');

// in nav-controller.js
export default class {
  
  process($selector) {
    $selector.find('a').click(/* do something with the click here */);
  }
  
}

Component method

The component method looks for available DOM elements on the page and creates class instances per element. The element is injected in the constructor. The selector is not a live selector. So the element has to be on the page when the page is ready (or when the router dispatched, but that is true in almost every case). The advantage over selector is that an instance is created per element, which allows the instance to carry/remember state of the element.

component(cssSelector, moduleNameOfController);

Example

// in the router
router.component('form', 'form-component');

// in nav-controller.js
export default class {
  
  constructor($selector) {
    this.$form = $selector;
  }
  
  initialize() {
    this.$form.submit(
      e => {
        
      }
    );
  }
  
}

Ready method

The ready method is fired when the DOM is ready. This can be usefull to attach polyfills.

ready(moduleNameOfController);

Example

// in the router
router.ready('polyfills');

// in polyfills.js
export default class {
  
  ready() {
    if (!Modernizr.flexbox) {
      import('flexibility').then((m) => m.default(document.body));
    }
  }
  
}

Resize method

The resize method is fired when the screen is resized. This can be usefull to execute scripts when the page is resized. Only when the constraint returns true and the when at least 1 element that satisfies the css selector is found, the controller is imported and executed. The method activate and deactivate on the controller are executed only when the constraint switches state (true/false).

resize(constraint, cssSelector, moduleNameOfController);

Example.

// in the router
router.resize((width, height) => width > 960 && height < 960, '.banners', 'banner-controller');

// in banner-controller.js
export default class {
  
  activate($selector, width, height) {
    // the window size satisfies the constraint
    $selector.find('a').unbind('click');
  }
  
  deactivate ($selector, width, height) {
    // the window size does not satisfy the constraint
    $selector.find('a').click(/* do something with the click here */);
  }
  
}

Scroll method

The scroll method is fired when the scrollbar is moved. Only when the constraint returns true and the when at least 1 element that satisfies the css selector is found, the controller is imported and executed. The method activate and deactivate on the controller are executed only when the constraint switches state (true/false).

scroll(constraint, cssSelector, moduleNameOfController);

Example.

// in the router
router.scroll((scrollTop) => scrollTop > 100, '.logo', 'logo-controller');

// in logo-controller.js
export default class {
  
  activate($selector, scrollTop) {
    // the scroll position satisfies the constraint
    $selector.addClass('scrolled');
  }
  
  deactivate ($selector, scrollTop) {
    // the scroll position does not satisfy the constraint
    $selector.removeClass('scrolled');
  }
  
}

Requirements

In order to use this package in the browser, one needs to transpile the ES6 code used in the package to javascript that is supported by the browser. You could use Babel for this purpose. Please keep in mind that you also need to transpile dynamic imports. Finally, this packages also leans on jquery, which is a dependency of this package.

Usage with Genkgo DTK

To use it with the Genkgo DTK, you need to set the transpile setting to true because vendor packages are not transpiled by default.

new DtkApp({
  "npm": {
    "routed-enhancement": {
      "transpile": true
    }
  }
})