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} {disabled} {condition} {nofallback} />
This component will hold any given routes as children, path is always derived from parent ones.
Available props:
{path}
β Any segment to derive a fullpath from, default to/
{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
<Route {key} {path} {props} {exact} {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; default to random pseudo-hash{path}
β Any segment to derive a fullpath from, default to/
{props}
β Additional properties for rendered component{exact}
β If set, the route will render only if the route exactly matches{fallback}
β If set, the route will render only if no more routes were matched{component}
β A valid svelte-component to render if the route matches{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 truelet:router
β Injects therouter
context, it also providesfailure
in case of errors
If you omit
exact
, then/x
would match both/
and/x
routes β see docs
<Link {go} {href} {title} {exact} {reload} {replace} {class|className} />
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/
{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}
β Uselocation.href
instead{replace}
β Usehistory.replaceState()
instead{class|className}
β Custom class-name for the mounted anchor
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 moving around:
"back"
β String; if given, will invokehistory.back()
"fwd"
β String; if given, will invokehistory.fwd()
n
β Number; if given, it'll be used to invokehistory.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 usedocument.location.href
insteadreplace
β If true, it will usehistory.replaceState()
insteadparams
β Used to replace:placeholders
from given pathqueryParams
β Additional search-params for the new location
$router
β Store with shared routeInfo details, similar tolet:router
yrv
gracefully degrades tolocation.hash
on environments wherehistory
is not suitable, also it can be forced throughRouter.hashchange = true
.