Giter VIP home page Giter VIP logo

tiny-components's Introduction

<tiny-components>

This is a lightweight JavaScript library, that allows you to dynamically create reuseable Components through HTML Tags. You will use plain and standard HTML with custom elements. However the library only comes with 2 built-in Component, so there is no bloat, no new HTML dialect, no boilerplate, no syntax sugar to learn.

Embed <tiny-components> into your Website

Add the following script tag as the first script of your html document.

<script src="https://YanSchw.github.io/tiny-components/tiny-components.js"></script>

What is a Component?

A Component is just a HTML Tag and an anonymous Create-Function (see below). This function takes in:

  • A node this is the Node | Element in the DOM. It is the same object you retrieve e.g. through document.querySelector(...);
  • A state this is tiny-components State of this DOM object. You can use it to do common operations on your element.

Create your first Component

First, define your Component in JavaScript.

component('my-hello-world-component', (node, state) => {
    node.innerHTML = state.sometextvalue;
});

Then, add an HTML Tag that has the same name as your Component into the DOM.

<my-hello-world-component sometextvalue="Hello!"></my-hello-world-component>

Attributes you define in the HTML are available as Attributes in the JavaScript state.

'Hello!' now appears in the Component's place.

Redrawing Components

If you want to redraw a Component, you have to explicitly tell the library to do so.

component('my-redrawing-button', (node, state) => {
    let button = document.createElement('button');
    button.innerHTML = 'Redraw this Component';
    button.onclick = () => {
        state.redraw();
    };
    node.appendChild(button);
});

Using select() and selectAll()

Using these functions your can spare some typing:

function select(cssSelector) {
    return document.querySelector(cssSelector);
}
function selectAll(cssSelector) {
    return document.querySelectorAll(cssSelector);
}

Creating DOM Nodes | Elements dynamically

Using this function

function createNode(tag, parent, lambda) {
    let element = document.createElement(tag);
    lambda(element);
    parent.appendChild(element);
}

you can easily create new DOM Elementes as one-liners or nest them within each other. For example:

createNode('div', select('body'), div => {
    createNode('p', div, p => p.innerText = 'Hello!');
});

creates a 'Hello!' Paragraph within a div within the HTML body.

Built-in Components

<include value="...">

The built-in include Component is a C-like cut-and-paste tool, that let's you inject HTML-Text.

<include value="/html/navigation.html"></include>

<echo>

The built-in echo Component Parses a block of JavaScript and puts the last evaluated expression into the innerHTML.

<echo>
    let a = "<p>he";
    let b = "llo</p>";
    a + b;
</echo>

This creates a Paragraph, that says hello

Additional Components

Examples

Here are some examples:

tiny-components's People

Contributors

yanschw avatar

Watchers

 avatar

Forkers

greiner-petter

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.