• Stars
    star
    212
  • Rank 179,279 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple isomorphic router

Riot Router

Route logo

Build Status Code Quality NPM version NPM downloads MIT License Coverage Status

Simple isomorphic router

The Riot.js Router is the minimal router implementation with such technologies:

  • compatible with the DOM pushState and history API
  • isomorphic functional API
  • erre.js streams and javascript async generators
  • rawth.js urls parsing

It doesn't need Riot.js to work and can be used as standalone module.

For Riot.js 3 and the older route version please check the v3 branch

Table of Contents

Install

We have 2 editions:

edition file
UMD Version route.js
ESM Module route.esm.js
Standalone UMD Module route.standalone.js

Script injection

<script src="https://unpkg.com/@riotjs/[email protected]/route.js"></script>

Note: change the part x.x.x to the version numbers what you want to use: ex. 4.5.0 or 4.7.0.

ESM module

import { route } from 'https://unpkg.com/@riotjs/route/route.esm.js'

npm

$ npm i -S @riotjs/route

Download by yourself

Documentation

With Riot.js

You can import the <router> and <route> components in your application and use them as it follows:

<app>
  <router>

    <!-- These links will trigger automatically HTML5 history events -->
    <nav>
      <a href="/home">Home</a>
      <a href="/about">About</a>
      <a href="/team/gianluca">Gianluca</a>
    </nav>

    <!-- Your application routes will be rendered here -->
    <route path="/home">
      Home page
    </route>
    <route path="/about">
      About
    </route>
    <route path="/team/:person">
      Hello dear { route.params.person }
    </route>

  </router>

  <script>
    import { Router, Route } from '@riotjs/route'

    export default {
      components { Router, Route }
    }
  </script>
</app>

You can also use the riot.register method to register them globally

import { Route, Router } from '@riotjs/route'
import { register } from 'riot'

// now the Router and Route components are globally available
register('router', Router)
register('route', Route)

Router

The <router> component should wrap your application markup and will detect automatically all the clicks on links that should trigger a route event.

<router>
  <!-- this link will trigger a riot router event -->
  <a href="/path/somewhere">Link</a>
</router>
<!-- this link will work as normal link without triggering router events -->
<a href="/path/to/a/page">Link</a>

You can also specify the base of your application via component attributes:

<router base="/internal/path">
  <!-- this link is outside the base so it will work as a normal link -->
  <a href="/somewhere">Link<a>
</router>

The router component has also an onStarted callback that will be called asynchronously after the first route event will be called

<router onStarted={onRouterStarted}></router>

Route

The <route> component provides the route property to its children (it's simply a URL object) allowing you to detect the url params and queries.

<route path="/:some/:route/:param">
  {JSON.stringify(route.params)}
</route>

<route path="/search(.*)">
  <!-- Assuming the URL is "/search?q=awesome" -->

  {route.searchParams.get('q')}
</route>

Each <route> component has its own lifecycle attributes in order to let you know when it gets mounted or unmounted.

<app>
  <router>
    <route path="/home"
      on-before-mount={onBeforeHomeMount}
      on-mounted={onHomeMounted}
      on-before-unmount={onBeforeHomeUnmount}
      on-unmounted={onHomeUnmounted}
    />
  </router>
</app>

Standalone

This module was not only designed to be used with Riot.js but also as standalone module. Without importing the Riot.js components in your application you can use the core methods exported to build and customize your own router compatible with any kind of frontend setup.

Fundamentals

This module works on node and on any modern browser, it exports the router and router property exposed by rawth

import { route, router, setBase } from '@riotjs/route'

// required to set base first
setBase('/');

// create a route stream
const aboutStream = route('/about')

aboutStream.on.value(url => {
  console.log(url) // URL object
})

aboutStream.on.value(() => {
  console.log('just log that the about route was triggered')
})

// triggered on each route event
router.on.value(path => {
  // path is always a string in this function
  console.log(path)
})

// trigger a route change manually
router.push('/about')

// end the stream
aboutStream.end()

Base path

Before using the router in your browser you will need to set your application base path. This setting can be configured simply via setBase method:

import { setBase } from '@riotjs/route'

// in case you want to use the HTML5 history navigation
setBase(`/`)

// in case you use the hash navigation
setBase(`#`)

Setting the base path of your application route is mandatory and is the first you probably are going to do before creating your route listeners.

DOM binding

The example above is not really practical in case you are working in a browser environment. In that case you might want to bind your router to the DOM listening all the click events that might trigger a route change event. Window history popstate events should be also connected to the router. With the initDomListeners method you can automatically achieve all the features above:

import { initDomListeners } from '@riotjs/route'

const unsubscribe = initDomListeners()
// the router is connected to the page DOM

// ...tear down and disconnect the router from the DOM
unsubscribe()

The initDomListeners will intercept any link click on your application. However it can also receive a HTMLElement or a list of HTMLElements as argument to scope the click listener only to a specific DOM region of your application

import { initDomListeners } from '@riotjs/route'

initDomListeners(document.querySelector('.main-navigation'))

More Repositories

1

riot

Simple and elegant component-based UI library
JavaScript
14,827
star
2

examples

Demos and examples for Riot and submodules
Riot
357
star
3

riotify

browserify plugin for riot files
JavaScript
96
star
4

observable

Simple script to listen and dispatch events
JavaScript
65
star
5

riot.github.io

Riot.js official website
HTML
65
star
6

compiler

Riot.js compiler
JavaScript
62
star
7

webpack-loader

Riot official webpack loader
JavaScript
56
star
8

tmpl

Riot template engine
JavaScript
51
star
9

made-with-riot

A list of apps and components made with Riot.js library
JavaScript
47
star
10

cli

Riot.js command line tool
JavaScript
45
star
11

animore

Riot tags animations hooks
JavaScript
43
star
12

custom-elements

Simple API to create vanilla custom elements with riot
JavaScript
34
star
13

ssr

Riot.js node Server Side Rendering
JavaScript
29
star
14

rollup-plugin-riot

Rollup Plugin for Riot
JavaScript
25
star
15

dom-bindings

Riot.js DOM Bindings template engine
JavaScript
21
star
16

karma-riot

A Karma plugin. Compile and test HTML and JS in Riot tag files.
JavaScript
19
star
17

hot-reload

Riot hot reload API
JavaScript
16
star
18

hydrate

Riot.js hydrate rendering for SSR applications
JavaScript
15
star
19

syntax-highlight

Riot tags syntax rules
14
star
20

babel-preset-es2015-riot

The default riot babel preset
JavaScript
14
star
21

parcel-transformer-riot

A parcel plugin for riot.js
JavaScript
11
star
22

lazy

Lazy components loader for Riot.js
JavaScript
10
star
23

parser

Parser for the riot tags
JavaScript
9
star
24

now

Riot.js now builder
JavaScript
6
star
25

eslint-config

Riot.js default eslint config file
JavaScript
5
star
26

create-riot

Riot.js Components and Projects scaffolder
JavaScript
4
star
27

online-compiler

Compile your riot tags to javascript online
JavaScript
4
star
28

dom-nodes

List of all the available DOM and SVG nodes and helper functions to quickly test against them
JavaScript
4
star
29

bump

Simple CLI tool for bump a new version
JavaScript
3
star
30

branding

The Riot.js logos and branding files
3
star
31

util

Shared util functions that are used in several Riot.js modules
JavaScript
3
star
32

parcel-template

Riot.js Parcel Project scaffolding Template - Used in create-riot
JavaScript
2
star
33

webpack-template

Riot.js Webpack Project scaffolding Template - Used in create-riot
JavaScript
1
star
34

component-template

Riot.js Component scaffolding Template - Used in create-riot
JavaScript
1
star
35

riotjs.com

This repository is only needed to handle the redirects to our new domain riot.js.org
HTML
1
star
36

legacy

Riot.js legacy documentation
HTML
1
star
37

babel-preset

Babel Preset config used to compile all the Riot.js core modules
JavaScript
1
star