Giter VIP home page Giter VIP logo

nho's Introduction

📌 Nho

Nho (nhỏ | small in Vietnamese) is a tiny JavaScript library for easy Web Component development.

Why Nho?

  • Writing a Web Component (WC) with vanilla JavaScript can be tedious. Alternatively, popular WC libs are overkill and overweighted (4KB+) for writing just a small WC like a "Buy now" button or a cart listing. Nho simplifies by staying lightweight, removing unnecessary APIs, and using a simple DOM diffing algorithm.

Features

  • 1.3KB gzipped.
  • Simple API, inspired from Vue.

Example

Limitations

  • In order to stay small, Nho skips advanced features (that popular front-end frameworks do have) like key, Fragments, memo. The DOM diffing algorithm is kinda naive (it's fast enough for small project though). If your components get too complex, consider other options.

Installation

npm

First, run

npm install nho

then

import { Nho } from 'nho';
class MyCounterChild extends Nho {}

CDN

First, add script to the html file

<script src="https://unpkg.com/nho"></script>

then

<script>
  let Nho = nho.Nho;
  class MyCounterChild extends Nho {}
</script>

Usage

/* Declare global styles. Styles will be injected to all Nho Elements */
Nho.style = `
  .box {
    background: blue;
    color: yellow;
  }
`

class MyCounterChild extends Nho {
  render(h) {
    /* Bind value from props */
    return h`<div>Child: ${this.props.count}</div>`
  }
}

class MyCounter extends Nho {
  setup() {
    /* This method run before mount */
    
    /* create component state using "this.reactive". state must be an object */
    this.state = this.reactive({ count: 1 });
 
    /* only use ref for storing DOM reference */
    this.pRef = this.ref();
    
    /* effect */
    this.effect(
      // effect value: fn -> value
      () => this.state.count,
      // effect callback: fn(old value, new value)
      (oldValue, newValue) => {
        console.log(oldValue, newValue)
      }
    )
  }
  
  onMounted() {
    /* This method run after mount */
    console.log('Mounted');
  }
  
  onUpdated() {
    /* This method run after each update. */
    console.log('Updated');

    /* P Ref */
    console.log('P Ref', this.pRef?.current);
  }
  
  onUnmounted() {
    /* This method run before unmount */
    console.log('Before unmount');
  }

  addCount() {
    /* Update state by redeclare its key-value. Avoid update the whole state. */
    this.state.count += 1;
  }
  
  render(h) {
    /* This method is used to render */
    
    /*
      SAME AS MODERN FRONT FRAMEWORKS
      - Must have only 1 root element
      - Bind state / event using value in literal string
      - Pass state to child element using props with 'p:' prefix
     */
    return h`
      <div class="box">
        <p ref=${this.pRef}>Name: ${this.state.count}</p>
        <button onclick=${this.addCount}>Add count</button>
        <my-counter-child p:count=${this.state.count + 5}></my-counter-child>
      </div>
    `
  }
}

customElements.define("my-counter", MyCounter);
customElements.define("my-counter-child", MyCounterChild);
<my-counter></my-counter>

How it works

  • It's better to dive into the code, but here is a quick sketch about how Nho works.

How Nho works

nho's People

Contributors

anh-ld avatar

Stargazers

Tuan Anh (Leo) Huynh avatar Nguyễn Việt Hùng avatar Socola Đại Ca avatar Minh Phan (Paul) avatar Hieu (Ryan) Hoang avatar Hieu Do avatar Leopold avatar Thomas avatar

Watchers

Socola Đại Ca avatar  avatar

Forkers

hta218

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.