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

nospa

v0.1.0

Published

A micro-framework which helps to organize frontend code for non-SPA websites based on various frameworks and CMS

Downloads

3

Readme

nospa

nospa is the way to organize front-end code for non-SPA websites.

Core ideas and features

  • HTML page generates on the sever-side with any CMS or framework depends on request data or system settings.
  • The app is the core object which controls components initialization/destruction. Init components on adding elements and destroy when removing. Provides lazy-loading and supports code-splitting.
  • The component is a piece of client-side logic.
  • Components are absolutely flexible and contain any logic inside. From simple event listener adding to Vue (or React, or any framework) component/widget initialization. Only 2 hooks (basically one) are required: onInit and onDestroy.
  • Components can be set up from HTML with props from data-attribute. Every component receives props in the same format.
  • Components API provides helpers to be as abstract as possible from HTML code.
  • The directive is a couple of functions that run on an element when it adds to the DOM and when it removes.

nospa inspired by Vue and made for cases when we cannot develop a full Vue app

Installation

npm i -S nospa

Usage

App

Set up and initialize app:

import { App } from 'nospa';
import ComponentName from './components/ComponentName';
import directiveName from './directives/directiveName';

const app = new App({
  el: '#app',
  components: {
    ComponentName,  
  },
  directives: {
    directiveName,  
  },
  methods: {
    someMethod() {
      console.log('App method called successful!');
    },
  },
  data: window.DATA || {},
  componentAttr: 'data-component',
  componentPropsAttr: 'data-component-props',
  componentLazyAttr: 'data-component-lazy',
  componentRefAttr: 'data-component-ref',
  directiveAttr: 'data-directive',
});
<div id="app">
  <div data-component="ComponentName"></div>
  <button type="button" data-directive="directiveName"></button>
</div>
<script>
  window.DATA = {
    foo: 'bar',
  };
</script>
<script src="js/app.js"></script>
<script>
  window.app.$methods.someMethod();
</script>

App methods

Documentation in progress TODO: $getComponentFromEl $insertHTML $removeEl $addEl $replaceEl

App data

Documentation in progress

Component

Basically component implements 2 methods - lifecycle hooks:

onInit - which is called when the component is initializing.

onDestroy - which is called when the component is destroying.

Everything else is determined by the implementation of the component`s logic.

import { Component } from 'nospa';

export default class Clicker extends Component {
  onInit() {
    this.value = 0;
    this.buttonRef = this.$getRef('button');
    this.valueRef = this.$getRef('value');
    
    if (buttonRef) {
      buttonRef.el.removeEventListener('click', this.handleClick);
    }
  }

  onDestroy() {
    if (this.buttonRef) {
      buttonRef.el.removeEventListener('click', this.handleClick)
    }
  }
  
  handleClick(e) {
    this.value += 1;
    if (this.valueRef) {
      valueRef.el.innerText = this.value;
    }
  }
}

Component can be defined as a class or as simple object:

export default {
  onInit() {
    this.value = 0;
    this.buttonRef = this.$getRef('button');
    this.valueRef = this.$getRef('value');
    
    if (buttonRef) {
      buttonRef.el.removeEventListener('click', this.handleClick);
    }
  },
  onDestroy() {
    if (this.buttonRef) {
      buttonRef.el.removeEventListener('click', this.handleClick)
    }
  },
  handleClick(e) {
    this.value += 1;
    if (this.valueRef) {
      valueRef.el.innerText = this.value;
    }
  }
}

Component instance properties

|:-:|:--| | $app | App instance | | $name | name under which component registered | | $el | element to which the component bound | | $props | an object with data parsed from data-component-props attribute |

Methods

App DOM manipulation methods available in the component instance: $getComponentFromEl, $insertHTML, $removeEl, $addEl, $replaceEl

Refs

Refs system is the way to get elements inside the component root element without sticking to class names or ids. It allows to bind the same component to different markup (of course there are possible limitations with elements hierarchy).

<div data-component="Component">
  <div data-component-ref="Component:refName"></div>    
</div>

Each ref can contain props object:

<div data-component="Component">
  <div data-component-ref='{ "name": "Component:refName", "props": { "foo": "bar" } }'></div>    
</div>

To the one element can be bound multiple refs from different components (one ref from each component):

<div data-component="Component">
  <div data-component="AnotherComponent">
      <div data-component-ref='[
        { "name": "Component:refName", "props": { "foo": "bar" } },
        "AnotherComponent:anotherRefName"
      ]'>
      </div>
  </div>    
</div>

| $getRef(name) | get single ref object (first with this name will be returned) |

<div data-component="Alert">
  <button type="button" data-component-ref="Alert:close"></button>
</div>
import { Component } from 'nospa';

export default class Alert extends Component {
  onInit() {
    this.closeRef = this.$getRef('close');
    console.log(this.closeRef); // { el: Element, props: {}, name: 'close' }
  }
}

| $getRefs(name) | get refs (all with this name will be returned) |

<div data-component="Tabs">
  <button type="button" data-component-ref='{ "name": "Tabs:button", "props": { "id": 1 } }'></button>
  <button type="button" data-component-ref='{ "name": "Tabs:button", "props": { "id": 2 } }'></button>
</div>
import { Component } from 'nospa';

export default class Tabs extends Component {
  onInit() {
    this.buttonsRefs = this.$getRefs('button');
    console.log(this.buttonsRefs);
    // [{ el: Element, props: { id: 1 }, name: 'button' }, { el: Element, props: { id: 2 }, name: 'button' }]
  }
}

| $getRefFromEl(el) | get ref from an element (if bound) |

<div data-component="Tabs">
  <button type="button" data-component-ref='{ "name": "Tabs:button", "props": { "id": 1 } }'></button>
  <button type="button" data-component-ref='{ "name": "Tabs:button", "props": { "id": 2 } }'></button>
</div>
import { Component } from 'nospa';

export default class Tabs extends Component {
  onInit() {
    this.$el.addEventListener('click', (e) => {
      let target = e.target;
      let isFound = false; 
      
      while (target !== this.$el && !isFound) {
        const ref = this.$getRefFromEl(target);
        if (ref && ref.name === 'button') {
          this.setActiveTab(ref.props.id);
          isFound = true;
          return;
        }
        
        target = target.parentNode;
      }
    });
  }
}

To attach delegated event listener use $onRef method

Events

Documentation in progress TODO: $onRef, $offRef, $emit, $on, $once, $off

Directive

Directive binding is very similar with refs binding.

<div data-directive="oneDirective"></div>
<div data-directive='{ "name": "oneDirective", "props": { "foo": "bar" } }'></div>
<div data-directive='[
  { "name": "oneDirective", "props": { "foo": "bar" } },
  "anotherDirective"
]'>
</div>

Directive example:

<button type="button" data-directive='{ "name": "clickLogger", "props": { "id": "foo" } }'></button>
function logClick() {
  console.log('Clicked!');
}

export default {
  bind(el, props) {
    console.log(`Click logger bound to the button with clicker id: ${props.id}`);
    el.addEventListener('click', logClick);
  },

  unbind(el) {
    el.removeEventListener('click', logClick);
  },
};

Browsers support

All modern browsers.

Works in IE11 with polyfills: