Git Product home page Git Product logo

lit-router's Introduction

LitElement Router

A simple and lightweight LitElement Router.

Coverage Status npm version Published on webcomponents.org Known Vulnerabilities CircleCI

Installation

npm install lit-element-router --save

Usage

Working example

You can find a working project on StackBlitz https://stackblitz.com/edit/lit-element-router

Minimal

import { LitElement, html } from 'lit-element';
import { routerMixin } from 'lit-element-router';

class MyApp extends routerMixin(LitElement) {

    static get routes() {
        return [{
            name: 'home',
            pattern: '',
            data: { title: 'Home' }
        }, {
            name: 'info',
            pattern: 'info'
        }, {
            name: 'user',
            pattern: 'user/:id'
        }, {
            name: 'not-found',
            pattern: '*'
        }];
    }

    onRoute(route, params, query, data) {
        console.log(route, params, query, data)
    }
}

customElements.define('my-app', MyApp);

Complete Example Using JavaScript Mixins in Details

Dont like mixins check out other examples

Don't want to use mixins interface you cane use a simple version in this tutorial: https://github.com/hamedasemi/lit-element-router/blob/mainline/README_NOT_MIXIN.md

Make any arbitary components or elements to a router using router mixins method

import { LitElement, html } from 'lit-element';
import { routerMixin } from 'lit-element-router';

class MyApp extends routerMixin(LitElement) {

}

customElements.define('my-app', MyApp);

Register routes and the onRoute function

import { LitElement, html } from 'lit-element';
import { routerMixin } from 'lit-element-router';

class MyApp extends routerMixin(LitElement) {
    static get routes() {
        return [{
            name: 'home',
            pattern: ''
        }, {
            name: 'info',
            pattern: 'info'
        }, {
            name: 'user',
            pattern: 'user/:id'
        }, {
            name: 'not-found',
            pattern: '*'
        }];
    }

    onRoute(route, params, query, data) {
        this.route = route;
        this.params = params;
    }
}

customElements.define('my-app', MyApp);

Make any arbitary components or elements to a router outlet using router outlet mixins method

import { LitElement, html } from 'lit-element';
import { routerOutletMixin } from 'lit-element-router';

export class AnyArbitaryLitElement extends routerOutletMixin(LitElement) {
    
}

customElements.define('any-arbitary-lit-element', AnyArbitaryLitElement);

Put the components under router outlet

import { LitElement, html } from 'lit-element';
import { routerMixin } from 'lit-element-router';

class MyApp extends routerMixin(LitElement) {
    static get routes() {
        return [{
            name: 'home',
            pattern: ''
        }, {
            name: 'info',
            pattern: 'info'
        }, {
            name: 'user',
            pattern: 'user/:id'
        }, {
            name: 'not-found',
            pattern: '*'
        }];
    }

    onRoute(route, params, query, data) {
        this.route = route;
        this.params = params;
    }

    render() {
        return html`
            <any-arbitary-lit-element current-route='${this.route}'>
                <div route='home'>Home any-arbitary-lit-element</div>
                <div route='info'>mY Info any-arbitary-lit-element</div>
                <div route='user'>User ${this.params.id} any-arbitary-lit-element</div>
                <div route='not-authorized'>Not Authorized any-arbitary-lit-element</div>
                <div route='not-found'>Not Found any-arbitary-lit-element</div>
            </any-arbitary-lit-element>
        `;
}

customElements.define('my-app', MyApp);

Make any arbitary components or elements to a router link using router link mixins method

import { LitElement, html } from 'lit-element';
import { routerLinkMixin } from 'lit-element-router';

export class AnArbitaryLitElement extends routerLinkMixin(LitElement) {
    
}

customElements.define('an-arbitary-lit-element', AnArbitaryLitElement);

Navigate using the router navigate method

import { LitElement, html } from 'lit-element';
import { routerLinkMixin } from 'lit-element-router';

export class AnArbitaryLitElement extends routerLinkMixin(LitElement) {
    constructor() {
        super()
        this.href = ''
    }
    static get properties() {
        return {
            href: { type: String }
        }
    }
    render() {
        return html`
            <a href='${this.href}' @click='${this.linkClick}'><slot></slot></a>
        `
    }
    linkClick(event) {
        event.preventDefault();
        this.navigate(this.href);
    }
}

customElements.define('an-arbitary-lit-element', AnArbitaryLitElement);

Browsers support

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
iOS Safari
iOS Safari
Samsung
Samsung
Opera
Opera
IE11, Edge last 2 versions last 2 versions last 2 versions last 2 versions last 2 versions last 2 versions

lit-router's People

Contributors

ankon avatar hamedasemi avatar lasserosenow avatar

Watchers

 avatar  avatar

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.