npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cycle-routing-driver

v1.4.2

Published

A bare-bones router for simple frontend routing in CycleJS

Downloads

94

Readme

cycle-routing-driver

CircleCI

Routing driver for CycleJS that enables simple client-side routing, without worrying about the frills that more complex routers bring. Includes a React integration to make UI route usage simple.

Intention

This library considers data-loading and rendering outside of its scope - this is all about converting back and forth between window.location and a route object, with some extra goodness to make it fun to use.

A route object is a simple bundle of three properties - page, data, and query. Every route has a name which is set in the page property, and is generated according to the configuration used when creating the router. When there are url parameters in the configuration, those values are mapped into the data property. And finally, any query string info is mapped into the query property.

In addition to the properties mentioned above, a route object also has a function called toUrl. This function can be called with a "route-like" argument to generate the URL for another route, while preserving the rest of the current routes details. For example, postViewRoute.toUrl({page: "post.edit"}) would preserve the data for the post being viewed, (eg. the post id parameter in the URL) but change the path to match the post edit route. This is useful when generating links in markup which relate to the current route.

Usage

npm install --save cycle-routing-driver

Updating the location bar and clicking links already works, so most of the work is hands-off. If you do need to force a route change based on an event, just stream it in via the sinks.

import {run} from '@cycle/run'
import makeRoutingDriver, {routes} from 'cycle-routing-driver'

function main(sources) {
	// in addition to path data and parameters, we get query string data so your page can render accordingly
	const modal_ = sources.route.map(({query}) => query.modal)

	return {
		// other stuff...,
		route: xs.merge(
			// if you would prefer to just bounce bad urls somewhere, you can!
			sources.route.filter(({page}) => page === `404`).mapTo({page: `homepage`}),

			// updating the URL always happens contextually - what streams through is reduced into the existing route
			sources.DOM.select(`.signup-modal`).events(`click`).mapTo({query: {modal: `signup`}}),

			// mapping query values to null removes them entirely
			sources.DOM.select(`.modal-close`).events(`click`).mapTo({query: {modal: null}}),
		),
	}
}

/*
 * the configuration below supports the following route structure:
 * /homepage
 * /about
 * /posts/:post
 * /posts/:post/edit
 * /posts/:post/performance (this will auto-redirect to /posts/:post/performance/dashboard)
 * /posts/:post/performance/demographics
 * /posts/:post/performance/reach
 * /posts/:post/performance/dashboard
 * /posts/:post/performance/search-ranking
 */
run(main, {
	// other stuff...,
	route: makeRoutingDriver(
		// the `routes` DSL provides a clean interface for defining basic routes
		routes`
		homepage
		about
		post (/posts/:post)
			edit
			performance -> dashboard
				demographics
				reach
				dashboard
				search-ranking
		`,
		{
			params: {
				post: {
					toData: _ => _,
					toParam: post => post.id || post,
				},
			},
		}
	)
})

React Integration

Because I use React for nearly all my frontend work, I've added a custom integration for easy use of the routing within a React application. See the example below, using example routes from the code above. Note the use of the Router, Route, and Link components, as well as the useRoute hook.

import {run} from '@cycle/run'
import makeReactDriver from '@sunny-g/cycle-react-driver'
import makeRoutingDriver, {routes} from 'cycle-routing-driver'
import {Router, Route, Link, useRoute} from 'cycle-routing-driver/dist/react/router'

import Modal from './ui/components/modal'
import Homepage from './ui/pages/home'
import EditPost from './ui/pages/post/edit'

const PageName = () => {
	const route = useRoute()

	return <span>Current page: {route.page}</span>
}

const App = ({actions}) => {
	return <div>
		<h1><PageName /></h1>

		<nav>
			<Link className="nav-link" to="homepage" activeClassName="active">Home</Link>
			<a className="nav-link" onClick={() => actions.modal.activate(`signup`)}>Signup</a>
			<Link className="nav-link" to={{page: `post.edit`, data: {post: {id: 1}}}} activeClassName="active">Edit Post 1</Link>
			<Link className="nav-link" to={{page: `post.edit`, data: {post: 2}}} activeClassName="active">Edit Post 2</Link>
			<Link className="nav-link" to={{page: `post.edit`, data: {post: {id: 3}}}} activeClassName="active">Edit Post 3</Link>
		</nav>

		<main>
			<Route match="homepage"><Homepage /></Route>
			<Route match="post" strict>This doesn't show on subroutes (like post.edit)</Route>
			<Route match="post.edit"><EditPost /></Route>
		</main>

		<aside>
			<Route match={{page: "post.edit", data: {post: {id: 1}}}}>
				Always save the first post!
			</Route>
		</aside>

		<Route match={({query}) => !!query.modal}>
			<Modal active onClose={actions.modal.deactivate} />
		</Route>
	</div>
}

function main(sources) {
	const actions = {
		modal: {
			activate: sources.react.handler(`modal.activate`),
			deactivate: sources.react.handler(`modal.deactivate`),
		}
	}

	return {
		// other stuff...,
		route: xs.merge(
			sources.route.filter(({page}) => page === `404`).mapTo({page: `homepage`}),
			sources.react.event(`modal.activate`).map((modal) => ({query: {modal}})),
			sources.react.event(`modal.deactivate`).mapTo({query: {modal: null}}),
		),
		react: sources.route.map((route) =>
			<Router route={route}>
				<App actions={actions}/>
			</Router>
		),
	}
}

run(main, {
	// other stuff...,
	route: makeRoutingDriver(/*route definition...*/),
	react: makeReactDriver(document.getElementById(`app`)),
})