Code Monkey home page Code Monkey logo

web-merge's Introduction

Web Merge

Build Status

Declarative client-side web fragments (apps/files) merger using web component APIs

NOTE

EXPERIMENTAL DO NOT USE IN PRODUCTION

Installation

1. via npm (Recommended)

$ npm install @soubai/web-merge

# or

$ yarn add @soubai/web-merge

Then import it in your script

import '@soubai/web-merge'

2. via unpkg

<script type="module" src="//unpkg.com/@soubai/web-merge"></script>

Usage

use <web-merge> custom element to load you fragment into web application

<web-merge content="./fragments/fragment-1.html"></web-merge>

<web-merge content="./fragments/fragment-2.html"></web-merge>

Attributes

content: define the source of you fragments (files,urls ...)

<web-merge content="./fragments/fragment-1.html"></web-merge>


<web-merge content="http://localhost:3000"></web-merge>

lazy: allow lazy loading for a fragment

<web-merge lazy content="./fragments/fragment-1.html"></web-merge>

route: define the route of the component that will be loaded when the this route is fired.

<web-merge route='/cart' content="http://127.0.0.1:3000"></web-merge>

Features

1.State management

Web-merge includes a build-in simple state management system based on event (not Event bus).

The fragment parent expose a state class that can help you to mutate and get new store

        
// fragment-3.html

const { state } = parent;

//initial state 

state.init({ count: 0 })

//event name 

const COUNT_CHANGE = "countChange";

//Apply side effects 
state.on(COUNT_CHANGE, ({ count }) => {
    document.querySelector("span.value").textContent = count;
});

//mutations 

document.getElementById("inc").addEventListener("click", function () {
    state.dispatch(COUNT_CHANGE, ({ count }) => ({ count: count + 1 }));
        });

document.getElementById("dec").addEventListener("click", function () {
    state.dispatch(COUNT_CHANGE, ({ count }) => ({ count: count - 1 }));
});

2.Routing

Web-merge includes a build-in pushState router that will helps you to load fragments/apps based on route.

The fragment parent expose a router class that can help you to perform routing action to load fragments/apps

You need to warper your fragments/apps with web-merge-router component and define base attribute as root route of your routing system; Than add route attribute to your fragments/apps to define the route that will load the fragment

<!-- index.html -->

<web-merge-router base="/examples">
<web-merge route="/" content="./fragments/fragment-6.html"></web-merge>
<web-merge route="/about" content="./fragments/fragment-5.html"></web-merge>
</web-merge-router>
        
// fragment-5.html

const { router } = parent;
        
<!-- fragment-5.html -->

<ul>
<li>
<a onclick="router.onNavigate('/')" href="#">Home</a>
</li>
<li>
<a onclick="router.onNavigate('/about')" href="#">About</a>

</li>
</ul>

Development

After cloning package on your local machine :

$ npm install

# start dev server to test examples locally

$ yarn dev

# Build 

$ yarn build

Changelog

See the changelog here

web-merge's People

Contributors

soub4i avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

icodein

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.