All Projects → lukeed → Navaid

lukeed / Navaid

Licence: mit
A navigation aid (aka, router) for the browser in 850 bytes~!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Navaid

Frontexpress
An Express.js-Style router for the front-end
Stars: ✭ 263 (-59.41%)
Mutual labels:  router, navigation, history, browser
Redux Saga Router
A router for Redux Saga
Stars: ✭ 153 (-76.39%)
Mutual labels:  router, navigation, history
Sheet Router
fast, modular client-side router
Stars: ✭ 219 (-66.2%)
Mutual labels:  router, history, browser
browser
Routing and Navigation for browser apps
Stars: ✭ 31 (-95.22%)
Mutual labels:  router, navigation
go router
The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
Stars: ✭ 380 (-41.36%)
Mutual labels:  router, navigation
react-native-boilerplate
Ready-made structure of your next React Native application within a few minutes.
Stars: ✭ 36 (-94.44%)
Mutual labels:  router, navigation
awesome-maps-ukraine
A curated list of maps of Ukraine, ukrainian mappers and tools that they use or develop for creating and publish maps
Stars: ✭ 35 (-94.6%)
Mutual labels:  navigation, history
React Native Simple Router
A community maintained router component for React Native
Stars: ✭ 266 (-58.95%)
Mutual labels:  router, navigation
Hybrid Navigation
React Native Navigation that supports seamless navigation between Native and React.
Stars: ✭ 258 (-60.19%)
Mutual labels:  router, navigation
Browser
Create Elm programs that run in browsers!
Stars: ✭ 284 (-56.17%)
Mutual labels:  history, browser
Route Composer
Protocol oriented, Cocoa UI abstractions based library that helps to handle view controllers composition, navigation and deep linking tasks in the iOS application. Can be used as the universal replacement for the Coordinator pattern.
Stars: ✭ 362 (-44.14%)
Mutual labels:  router, navigation
navigation-skeleton
This component allows you to show skeletons of pages during navigation process.
Stars: ✭ 16 (-97.53%)
Mutual labels:  router, navigation
Helm
A graph-based SwiftUI router
Stars: ✭ 64 (-90.12%)
Mutual labels:  router, navigation
universal-router
↩️ Router for every occasions
Stars: ✭ 64 (-90.12%)
Mutual labels:  router, navigation
ultra-router
Router for component-based web apps. Pair with React or <BYOF />.
Stars: ✭ 35 (-94.6%)
Mutual labels:  router, history
Portal Lite
Multi-platform Personalized Portal: Web, Browser Extension. All components are web apps--users can compose their own Portal freely, and developers can contribute to the Privoce Web App library to easily incorporate their web app to our Portal.
Stars: ✭ 335 (-48.3%)
Mutual labels:  navigation, browser
Auto route library
Flutter route generator
Stars: ✭ 434 (-33.02%)
Mutual labels:  router, navigation
Mobx Router
A simple router for MobX + React apps
Stars: ✭ 489 (-24.54%)
Mutual labels:  router, history
qlevar router
Manage you project Routes. Create nested routes. Simply navigation without context to your pages. Change only one sub widget in your page when navigating to new route.
Stars: ✭ 51 (-92.13%)
Mutual labels:  router, navigation
Parrot
Web router specially designed for building SPAs using Meteor
Stars: ✭ 75 (-88.43%)
Mutual labels:  router, navigation
navaid
A navigation aid (aka, router) for the browser in 865 bytes~!

Install

$ npm install --save navaid

Usage

const navaid = require('navaid');

// Setup router
let router = navaid();
// or: new Navaid();

// Attach routes
router
  .on('/', () => {
    console.log('~> /');
  })
  .on('/users/:username', params => {
    console.log('~> /users/:username', params);
  })
  .on('/books/*', params => {
    console.log('~> /books/*', params);
  });

// Run as single instance
router.run('/');
//=> "~> /"
router.run('/users/lukeed');
//=> "~> /users/:username { username: 'lukeed' }"
router.run('/books/kids/narnia');
//=> "~> /books/* { wild: 'kids/narnia' }"

// Run as long-lived router w/ history & "<a>" bindings
// Also immediately calls `run()` handler for current location
router.listen();

API

navaid(base, on404)

Returns: Navaid

base

Type: String
Default: '/'

The base pathname for your application. By default, Navaid assumes it's operating on the root.

All routes will be processed relative to this path (see on()). Similarly, while listening, Navaid will not intercept any links that don't lead with this base pathname.

Note: Navaid will accept a base with or without a leading and/or trailing slash, as all cases are handled identically.

on404

Type: Function
Default: undefined

The function to run if the incoming pathname did not match any of your defined defined patterns.
When defined, this function will receive the formatted uri as its only parameter; see format().

Important: Navaid only processes routes that match your base path!
Put differently, on404 will never run for URLs that do not begin with base.
This allows you to mount multiple Navaid instances on the same page, each with different base paths. 😉

format(uri)

Returns: String or false

Formats and returns a pathname relative to the base path.

If the uri does not begin with the base, then false will be returned instead.
Otherwise, the return value will always lead with a slash (/).

Note: This is called automatically within the listen() and run() methods.

uri

Type: String

The path to format.

Note: Much like base, paths with or without leading and trailing slashes are handled identically.

route(uri, replace)

Returns: undefined

Programmatically route to a path whilst modifying & using the history events.

uri

Type: String

The desired path to navigate.

Important: Navaid will prefix your uri with the base, if/when defined.

replace

Type: Boolean
Default: false

If true, then history.replaceState will be used. Otherwise history.pushState will be used.

This is generally used for redirects; for example, redirecting a user away from a login page if they're already logged in.

on(pattern, handler)

Returns: Navaid

Define a function handler to run when a route matches the given pattern string.

pattern

Type: String

The supported pattern types are:

  • static (/users)
  • named parameters (/users/:id)
  • nested parameters (/users/:id/books/:title)
  • optional parameters (/users/:id?/books/:title?)
  • any match / wildcards (/users/*)

Note: It does not matter if your pattern begins with a / — it will be added if missing.

handler

Type: Function

The function to run when its pattern is matched.

Note: This can also be a Promise or async function, but it's up to you to handle its result/error.

For patterns with named parameters and/or wildcards, the handler will receive an "params" object containing the parsed values.

When wildcards are used, the "params" object will fill a wild key with the URL segment(s) that match from that point & onward.

let r = new Navaid();

r.on('/users/:id', console.log).run('/users/lukeed');
//=> { id: 'lukeed' }

r.on('/users/:id/books/:title?', console.log).run('/users/lukeed/books/narnia');
//=> { id: 'lukeed', title: 'narnia' }

r.on('/users/:id/jobs/*').run('/users/lukeed/jobs/foo/bar/baz/bat');
//=> { id: 'lukeed', wild: 'foo/bar/baz/bat' }

run(uri)

Returns: Navaid

Executes the matching handler for the specified path.

Unlike route(), this does not pass through the history state nor emit any events.

Note: You'll generally want to use listen() instead, but run() may be useful in Node.js/SSR contexts.

uri

Type: String
Default: location.pathname

The pathname to process. Its matching handler (as defined by on()) will be executed.

listen(uri?)

Returns: Navaid

Attaches global listeners to synchronize your router with URL changes, which allows Navaid to respond consistently to your browser's BACK and FORWARD buttons.

These are the (global) events that Navaid creates and/or responds to:

  • popstate
  • replacestate
  • pushstate

Navaid will also bind to any click event(s) on anchor tags (<a >base path. Navaid will not intercept links that have any target attribute or if the link was clicked with a special modifier (eg; ALT, SHIFT, CMD, or CTRL).

uri

Type: String
Default: undefined

(Optional) Any value passed to listen() will be forwarded to the underlying run() call.
When not defined, run() will read the current location.pathname value.

unlisten()

Returns: undefined

Detach all listeners initialized by listen().

Note: This method is only available after listen() has been invoked.

License

MIT © Luke Edwards

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].