1.1.8 • Published 6 years ago

soo.js v1.1.8

Weekly downloads
7
License
ISC
Repository
github
Last release
6 years ago

Soo

Why ?

  • Make custom elements with easy API
  • Library size gziped 1KB
  • No bundling needed, for virtual-dom or anything else, so if using modern browsers, just plug and create.
  • Has same API as omi but is great alternative if you want to create custom elements without JSX, virtual DOM and store.

install

npm install soo.js

or add to index.html

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

or

import Soo from "https://unpkg.com/soo.js"

Examples

Check live examples here

Example files here


Basic example

import Soo from "https://unpkg.com/soo.js";
import {HTML} from "https://unpkg.com/kelbas"

class Test extends Soo {
  install() {
    this.data = { liked: false, name: "Like me!" };
  }

  likeIt() {
    this.data.liked = true;
    this.update();
  }
  

  css() {
    return `:host {
              padding:5px;
            }
            button {
                background:green;
                padding:10px 5px;
                outline:none;
                cursor:pointer;
            }`;
  }

  render() {
    if (this.data.liked) {
      return HTML`<span>Liked</span>`;
    }
    return HTML`<button onclick="${this.likeIt.bind(this)}">${this.data.name}</button>`;
  }
}

customElements.define("like-button", Test);

Async example

class Test extends Soo {
    getData() {
        return fetch('https://jsonplaceholder.typicode.com/users/')
        .then(response => response.json())
        .then(json => Promise.resolve(json))
    }
  
    css() {
      return `:host {
                display:grid;
              }
              #users-container {
                  display:grid;
                  width:400px;
              }
              .users-list {
                  display:grid;
                  grid-row-gap:3px;
                  margin:5px 0;
                  background:teal;
                  padding:10px;
                  color:white;
              }
              .name {
                  text-decoration:underline;
              }
             `;
    }
  
    async render() {
        const data = await this.getData();

        return HTML`
                    <section id="users-container">
                        <h3>Users list</h3>
                        ${data.map(item => `<div class="users-list">
                                                <span class="name">Name: ${item.name}</span>
                                                <span class="emai">Email: ${item.email}</span>
                                            </div>`).join("")}
                    </section>
                   `
    }
  }
    
  customElements.define("async-example", Test);

API explanation

FunctionsvalueUsage
updateMakes component re-render
installFires at the start of component, best place to set properties
installedFires after component is rendered
uninstallFires after component is removed
afterUpdateFires after component is updated
beforeRenderFires before component is rendered
fireevent name, payloadfires event thats listenable from component
fireGlobalevent name, payloadfires event thats listenable from document
renderHTML stringreturn your HTML element
csscss stringreturn your component style
1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago