• Stars
    star
    109
  • Rank 319,018 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 6 years ago

Reviews

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

Repository Details

Router Driver built for Cycle.js

cyclic-router

cyclic-router V5 is a routing library built for Cycle.js. Unlike previous versions, cyclic-router V5 is not a driver. It is a main function-wrapper (routerify) which relies on @cycle/history for driver source/sink interactions.

For older versions of cyclic-router, V4 and earlier please go to the old README

Installation

Using npm:

$ npm install --save @cycle/history # @cycle/history is a peerDependency of cyclic-router
$ npm install --save-dev @types/history # @cycle/history uses ReactTraining/history 
                                       # under the hood (For Typescript users only)
$ npm install --save cyclic-router

Routerify requires you to inject the route matcher. We'll use switch-path for our examples but other matching libraries could be adapted to be used here:

$ npm install --save switch-path
// using an ES6 transpiler, like babel
import {routerify} from 'cyclic-router'

// not using an ES6 transpiler
var routerify = require('cyclic-router').routerify

Basic Usage

import xs from 'xstream';
import {run} from '@cycle/run';
import {makeDOMDriver} from '@cycle/dom';
import {routerify} from 'cyclic-router';
import {makeHistoryDriver} from '@cycle/history';
import switchPath from 'switch-path';

function main(sources) {
  const pageSinks$ = sources.router.routedComponent({
    '/': HomeComponent,
    '/other': OtherComponent
  })(sources);
  
  return {
    DOM: pageSinks$.map(c => c.DOM).flatten(),
    router: xs.of('/other') // Notice use of 'router' sink name, 
                            // which proxies the original 'history' sink
  };
}

const mainWithRouting = routerify(main, switchPath)

run(mainWithRouting, {
  DOM: makeDOMDriver('#app'),
  history: makeHistoryDriver() // create history driver as usual,
                               // but it gets proxied by routerify
});

Routerify Options

Routerify accepts an options object like so: routerify(main, switchPath, options)

The options object conforms to the interface:

interface RouterOptions {
    basename?: string;
    historyName?: string;
    routerName?: string;
    omitHistory?: boolean;
}
  • basename - The root router path, defaults to /
  • historyName - The source/sink name associated with the raw history driver, defaults to history
  • routerName - The source/sink name you want exposed to your app which captures the functionality/streams associated with routerify. Defaults to router
  • omitHistory - Routerify hides the source/sink name associated with the raw history driver (given in the historyName option) from your app and only exposes the source/sink name associated with the router (given in the routerName option). Defaults to true, If you would like your app to have access to both the raw history source/sink and the router source/sink (injected by routerify), set this option to false

Route Parameters

This behavior changes based on the injected route matcher. In the case of switch-path, you can pass route parameters to your component by adding them to the component sources.

const routes = {
  '/:id': id => sources => YourComponent({props$: Observable.of({id}), ...sources})
}

Dynamically change route

You can dynamically change route from code by emitting inputs handled by the history driver.

...
import sampleCombine from 'xstream/extra/sampleCombine'

function main(sources) {
  // ...
  const homePageClick$ = sources.DOM.select(".home").events("click");
  const previousPageClick$ = sources.DOM.select(".previous").events("click");
  const nextPageClick$ = sources.DOM.select(".next").events("click");
  const oldPageClick$ = sources.DOM.select(".old").events("click");
  const aboutPageClick$ = sources.DOM.select(".about").events("click");
  const replacePageClick$ = sources.DOM.select(".replace").events("click");
  
  return {
    // ...
    router: xs.merge(
        // Go to page "/"
        homePageClick$.mapTo("/"),
        
        // Go back to previous page
        previousPageClick$.mapTo({ type: "goBack" }),
        
        // Go forward to next page
        nextPageClick$.mapTo({ type: "goForward" }),
        
        // Go back from 5 pages
        oldPageClick$.mapTo({ type: "go", value: -5 }),
        
        // Go to page "/about" with some state set to router's location
        aboutPageClick$.mapTo({ pathname: "/about", state: { some: "state" } })
        
        // Replace the current history entry with the same path, but with new state info
        replacePageClick$.compose(sampleCombine(sources.router.history$)).map(([_, route]) => {
          return { type: 'replace', pathname: route.pathname, state: { some: "different state" } }
        ))
    ),
  };
}

More Repositories

1

awesome-cyclejs

A curated list of awesome Cycle.js resources
826
star
2

redux-cycles

Bring functional reactive programming to Redux using Cycle.js
JavaScript
745
star
3

create-cycle-app

Create Cycle.js apps with no build configuration.
JavaScript
238
star
4

cycle-android

A Cycle.js port for Android
Java
88
star
5

one-fits-all

The one-fits-all flavor for create-cycle-app
JavaScript
34
star
6

cycle-canvas

A canvas driver for Cycle.js
JavaScript
33
star
7

typescript-starter-cycle

An opinionated starter for Cycle.js projects powered by TypeScript.
TypeScript
33
star
8

xstream-boilerplate

DEPRECATED! use create-cycle-app instead
JavaScript
27
star
9

README

Cycle.js Community README, code of conduct and introductions.
JavaScript
24
star
10

create-cycle-app-flavors

JavaScript
17
star
11

cyclejs-sortable

Makes all children of a selected component sortable
TypeScript
17
star
12

cyclejs-utils

A few helper functions for dealing with sinks
TypeScript
15
star
13

component-combinators

Component model for cyclejs
CSS
13
star
14

cycle-idb

A cycle driver for IndexedDB.
JavaScript
13
star
15

cycle-remote-data

A Cycle.js driver for elegantly loading data
TypeScript
12
star
16

boids

Boids in Cycle.js (bird flocking simulator)
JavaScript
12
star
17

cyclejs-modal

An easy way to open custom modals in a cyclejs app
TypeScript
12
star
18

cycle-keyboard

A keyboard driver for cycle.js
TypeScript
9
star
19

learn-to-cycle

A book on @cyclejs
9
star
20

cycle-mouse-driver

Handles mouse events on the document in Cycle.js applications
JavaScript
9
star
21

built-with-cycle

A website to showcase the cool projects built with Cycle.js
JavaScript
9
star
22

cycle-webworker

A simple webworker driver for Cycle.js
TypeScript
8
star
23

cycle-regl

A Cycle.js driver for regl, a declarative and functional interface to WebGL
TypeScript
6
star
24

cycle-delayed-driver

Create a driver in the future as a response to a specific event
JavaScript
5
star
25

cycle-svg-pan-and-zoom

A Google Maps style SVG pan and zoom component for Cycle.js
JavaScript
5
star
26

xstream-marbles

Interactive marble diagrams of xstream Streams
TypeScript
5
star
27

cycle-serverless

A driver for using cyclejs and xstream in serverless FaaS environments (Azure Functions)
JavaScript
2
star
28

utilities

A collection of small helpful scripts related to Cycle.js
PHP
1
star
29

cycle-classic-dom

A tiny helper library for working with classic JS DOM libraries in Cycle.js
TypeScript
1
star