All Projects → pateketrueke → Yrv

pateketrueke / Yrv

Your routing! (for Svelte)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Yrv

Svelte Router
Simple Svelte Router for Single Page Applications (SPA).
Stars: ✭ 44 (-69.23%)
Mutual labels:  router, svelte
svelte-starter-kit
Svelte starter kit — router, state management and testing included.
Stars: ✭ 16 (-88.81%)
Mutual labels:  router, svelte
svelte-micro
Light & reactive one-component router for Svelte
Stars: ✭ 81 (-43.36%)
Mutual labels:  router, svelte
Curi
A JavaScript router for single-page applications
Stars: ✭ 262 (+83.22%)
Mutual labels:  router, svelte
Crayon
Simple framework agnostic UI router for SPAs
Stars: ✭ 310 (+116.78%)
Mutual labels:  router, svelte
Abstract State Router
Like ui-router, but without all the Angular. The best way to structure a single-page webapp.
Stars: ✭ 288 (+101.4%)
Mutual labels:  router, svelte
svelte-router
Router component for Svelte
Stars: ✭ 63 (-55.94%)
Mutual labels:  router, svelte
Svelte Router
Svelte Router adds routing to your Svelte apps. It's designed for Single Page Applications (SPA). Includes localisation, guards and nested layouts.
Stars: ✭ 310 (+116.78%)
Mutual labels:  router, svelte
Svelte Store Router
Store-based router for Svelte
Stars: ✭ 54 (-62.24%)
Mutual labels:  router, svelte
Nanocal
Airbnb range picker rip-off
Stars: ✭ 132 (-7.69%)
Mutual labels:  svelte
Routify Starter
Stars: ✭ 137 (-4.2%)
Mutual labels:  svelte
Laravel Router
An organized approach to handling routes in Laravel.
Stars: ✭ 130 (-9.09%)
Mutual labels:  router
Tsparticles
tsParticles - Easily create highly customizable particles animations and use them as animated backgrounds for your website. Ready to use components available for React.js, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Inferno, Solid, Riot and Web Components.
Stars: ✭ 2,694 (+1783.92%)
Mutual labels:  svelte
Grip
The microframework for writing powerful web applications.
Stars: ✭ 137 (-4.2%)
Mutual labels:  router
Linux Router
Set Linux as router in one command. Support Internet sharing, redsocks, Wifi hotspot, IPv6. Can also be used for routing VM/containers
Stars: ✭ 129 (-9.79%)
Mutual labels:  router
Dowse
The Awareness Hub for the Internet of Things
Stars: ✭ 139 (-2.8%)
Mutual labels:  router
Svelte Simple Modal
A simple, small, and content-agnostic modal for Svelte v3
Stars: ✭ 130 (-9.09%)
Mutual labels:  svelte
Luthier Ci
Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework
Stars: ✭ 129 (-9.79%)
Mutual labels:  router
Sb Admin Svelte
StartBootstrap SB Admin rebuilt using Svelte + Sveltestrap
Stars: ✭ 143 (+0%)
Mutual labels:  svelte
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (-2.8%)
Mutual labels:  router

yrv

Build status NPM version Known Vulnerabilities donate

The v is for Svelte

Built on top of abstract-nested-router, so you can use nested routers, also:

  • Advanced parameters can be used, e.g. /:id<\d+>see docs
  • ARIA-compliant, sets [aria-current="page"] on active links
  • Seamless <base href="..." /> integration
  • Conditionals and redirection through props
  • Fallback <Route /> handlers
  • Hash and URI-based routes
  • Support for query-string
  • REPL ready!

yrv will use any base-href found on the current page to rewrite links and routes.

Usage

Install yrv through NPM or Yarn:

<script>
  import { Router, Route, Link } from 'yrv';
</script>

<Link href="/">Home</Link>
| <Link href="/World">Hello</Link>
| <Link href="/not/found">NotFound</Link>

<p>
  <Router>
    <Route exact>Hello World</Route>
    <Route fallback>Not found</Route>
    <Route exact path="/:name" let:router>Hey {router.params.name}!</Route>
  </Router>
</p>

Notice fallback routes can’t be placed at the beginning, otherwise further routes will not be mounted. 💣

Components

You MUST declare at least, one top-level Router to setup the bindings.

<Router {path} {pending} {disabled} {condition} {nofallback} />

This component will hold any given routes as children, path is always derived from parent routes.

Available props:

  • {path} — Any segment to derive a fullpath from, defaults to /
  • {pending} — Svelte-component or String; top-level pending support
  • {disabled} — Boolean; Similar to condition, but for bound props
  • {condition} — Function; if given, render only if evaluates to true
  • {nofallback} — If set, non-matched routes will never raise a failure

Nested routers does not need the same path to be declared inside, e.g. if the router for /top has a /sub router inside, inner router will use the route /top/sub, (the same as declaring /top/sub route outside the parent router).

<Route {key} {path} {exact} {pending} {fallback} {component} {disabled} {condition} {redirect} let:router />

Main container for routing, they can hold any component or children.

Available props:

  • {key} — The route identity, not its path; defaults to random pseudo-hash
  • {path} — Any segment to derive a fullpath from, default to /
  • {exact} — If set, the route will render only if the route exactly matches
  • {pending} — Svelte-component or String; rendered during the loading of dynamic components
  • {fallback} — If set, the route will render only if no more routes were matched
  • {component} — Accepts either a valid svelte-component, a promise, or a dynamic import function
  • {disabled} — Boolean; Similar to condition, but for bound props
  • {condition} — Function; if given, the route will render only if evaluates to true
  • {redirect} — Alternate redirection location, only if the previous condition was true
  • let:router — Injects the router context, it also provides failure in case of errors

If you omit exact, then /x would match both / and /x routes — see docs

When yrv adds a new route, it'll use any given key from its props — once routes are detached they're also removed from the router registry, due to that, the next time the same route is mounted a new key is generated (if isn't present already).

<script>
  import SvelteComponent from 'path/to/svelte-component.svelte';
</script>

<Link href="/">Home</Link>
| <Link href="/svelte-component">Svelte component</Link>
| <Link href="/promise">Promised component</Link>
| <Link href="/lazy">Lazy component</Link>

<p>
  <Router>
    <Route exact>Hello World</Route>
    <Route exact path="/svelte-component" component={SvelteComponent}/>
    <Route exact path="/promise" component="{import('path/to/other-component.svelte')}"/>
    <Route exact path="/lazy" component="{() => import('path/to/another-component.svelte')}"/>
  </Router>
</p>

Behind the scenes, for making dynamic-imports work, the bundler should inline them or just write-out the required chunks to make it work natively (<script type="module" />) or through shimport, etc.

<Link {go} {href} {open} {title} {exact} {reload} {replace} {class} />

In order to navigate, you can use Link components, or regular links, etc.

All href values MUST be absolute, only links starting with / or # are allowed.

Available props:

  • {go} — History shortcut (see below)
  • {href} — New location, default to /
  • {open} — Same behavior as <a target="_blank">
  • {title} — HTML title-attribute value
  • {button} — If set, will use button-tag instead
  • {exact} — Determine if link should match exactly to be set as active
  • {reload} — Use location.href instead
  • {replace} — Use history.replaceState() instead
  • {class} — Custom class-name for the mounted anchor

The value for open can be a string including the window specs, e.g. width=400,height=200 — a on:close event will be fired once the opened window is closed.

Normal on:click events are still allowed, so you can use:

<Link on:click={() => location.reload()}>Reload</Link>

Active links will gain the [aria-current] attribute, and [disabled] if they're buttons.

Aditionally, you can setup go to move around:

  • "back" — String; if given, will invoke history.back()
  • "fwd" — String; if given, will invoke history.fwd()
  • n — Number; if given, it'll be used to invoke history.go(n)

If navigating through history is not possible a normal redirection will run. ⚓️

Public API

  • navigateTo(path[, options]) — Change the location, supported options are:
    • reload — If true, it will use document.location.href instead
    • replace — If true, it will use history.replaceState() instead
    • params — Used to replace :placeholders from given path
    • queryParams — Additional search-params for the new location
  • $router — Store with shared routeInfo details, similar to let:router

yrv gracefully degrades to location.hash on environments where history is not suitable, also it can be forced through Router.hashchange = true.

Route Info

Route changes are propagated through stores, if you want to listen too just subscribe, e.g.

import { router } from 'yrv';

router.subscribe(e => {
  if (!e.initial) console.log(e);
});

Using this technique you gain access to the same detail object as let:router does.

Notice the initial property is present as soon the store is initialized, consecutive changes will not have it anymore.

IE11 support

Support for IE11 is granted if you include, at least, the following polyfills before your application:

<script>if (!!window.MSInputMethodContext && !!document.documentMode)
  document.write('<script src="https://polyfill.io/v3/polyfill.min.js?features=default,Promise,Object.getOwnPropertyDescriptors"><\/script>');</script>
<script src="your-app.js"></script>

document.write() is used because conditional comments were dropped in IE10, so this way you can conditionally load polyfills anyway.

Also, you MUST enable either buble or babel within your build pipeline to transpile down to ES5.

Frequently Asked Questions

How to conditionally render a <Router /> component?

Both Route/Router components support the disabled and condition props, but:

  • Use condition to allow/disallow route-dispatching dynamically
  • Use disabled to skip from rendering, it will add/remove the route

This new disabled prop would work as you're expecting:

<Router disabled={!showNavBar}>
  ...
</Router>

What means the exact property and how it works?

Say you have three routes:

  • /a (exact)
  • /a/b (non-exact)
  • /a/b/c (exact)

Now, you navigate from /a to /a/b/c:

  • Since /a was active, and it was exact, yrv clears out the routeInfo for that route.
  • Since /a/b is not exact, yrv activate this route because is half-way to the final route.

If you plan to have more routes nested, then the route will never be exact (at least at top-levels).

This is also true for <Link /> components — as soon as they match the [aria-current] attribute will be added on them to denote active links.

If the link for /a were also exact, then it'll be active if the matching route is /a only.

Why path can't be an empty string like other routers does?

Even if browsers treat http://localhost:8080 and http://localhost:8080/ as the same thing I wanted to keep paths clear as possible.

Internally yrv normalizes any given URI to keep a trailing slash, so /foo is /foo/ for matching purposes.

Also, the default path is usually / so there's no point on having to declare anything else:

<Route>OK</Route>
<Route path="/">OK</Route>

What is routeInfo and how can I access it outside routes?

This object is very similar to what you get with let:router inside components.

Use the $router store to access it, e.g.

<script>
  import { router } from 'yrv';
</script>
<pre>{JSON.stringify($router, null, 2)}</pre>

Why does Yrv not work with Parcel or webpack/snowpack?

If you're getting any of the errors below:

  • store.subscribe is not a function
  • Class constructor SvelteComponent cannot be invoked without 'new'
  • 'on_outro' is not exported by [...]
  • 'target' is a required option

Make sure you're using the right settings:

  1. Add mainFields into resolve config, e.g. mainFields: ['svelte', 'browser', 'module', 'main']
  2. Remove exclude: /node_modules/ from svelte-loader config

If you're using an online tool that is not the official Svelte REPL the behavior is unexpected and no further support will be granted.

Can I use hash-based routes à la Gmail? e.g. index.html#/profile, index.html#/book/42?

Yes, URIs like that are suitable for embedded apps like Electron, where normal URLs would fail.

Also this mode is the default used on the Svelte REPL, because is not an iframe, nor a regular webpage... it's a weird thing!

If you enable Router.hashchange = true all your regular links will be automatically rewritten to hash-based URIs instead, see how it works in our test suite.

Why I'm getting <Component> was created with unknown prop 'router' in the browser's console?

If you're not using the router prop inside your route-components then just add:

<script>
  export const router = null;
</script>

That will remove the warning and also will make eslint-plugin-svelte3 in your workflow happy.

Why router.subscribe is called two times when I first open the page?

Any subscription to stores will fire twice as they have an initial value, once the router resolves (e.g. the initial route) then a second event is fired.

In this case, and additional property initial is added to identify such event.

Is there any method that allows me to detect route change?

Yes, you can subscribe to the router store, e.g. router.subscribe(...)see above.

Is there a way to reduce the bundle size of yrv?

Since v0.0.33 you'll be getting the most reduced version we can ship, however it comes without development warnings.

You can configure your bundler to map 'yrv': './node_modules/yrv/build/dev/index.js' (I'm not sure if importing yrv/build/dev works too)

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].