GithubHelp home page GithubHelp logo

sk-router's Introduction

This has been moved to the skatejs monorepo!

sk-router

A web component router that's compatible with code-splitting out of the box.

Install

npm install @skatejs/sk-router

Usage

In these examples we've used the hyperscript h export in @skatejs/val to create real DOM using JSX so that we can express them in a readable manner while still setting complex data types (which you cannot do with HTML).

/** @jsx h */

import { Route, Router } from '@skatejs/sk-router';
import { h } from '@skatejs/val';
import { Index, NotFound } from './pages';

const router = (
  <Router>
    <Route page={Index} path="/" />
    <Route page={NotFound} path="*" />
  </Router>
);

The page prop on the Route component is just a function that returns a DOM node. This means that it can be:

  • A custom element constructor.
  • A function that mounts a React DOM tree to a node and returns the node.
  • A function that does anything, so long as it returns a normal DOM node.

Optimisation / code-splitting

The example above loads everything up front. However, in a large app, you'd probably want to split up your pages into separate resources. Webpack can easily do this for you using import(). All you need to do to enable this is to:

  1. Use a function as your component instead of a constructor.
  2. Call import() in the function.
  3. Ensure the module you import contains a default export that can be anything that page takes, as mentioned above.
const lazyRoute = (
  <Route page={() => import('./pages/docs')} path="/docs" />
);

Links

We use PageJS underneath the hood, but we also export a Link component so that it doesn't have to capture all anchor clicks on the page, which could cause some confusion.

import { Link } from '@skatejs/sk-router';

const indexLink = (
  <Link href="/">Home</Link>
);

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.