UNPKG

6.16 kBMarkdownView Raw
1<div align="center">
2
3![yrv](yrv.png)
4
5![Build status](https://github.com/pateketrueke/yrv/workflows/build/badge.svg)
6[![NPM version](https://img.shields.io/npm/v/yrv)](https://www.npmjs.com/package/yrv)
7[![Known Vulnerabilities](https://snyk.io/test/npm/yrv/badge.svg)](https://snyk.io/test/npm/yrv)
8
9</div>
10
11> The `v` is for Svelte
12
13Built on top of [abstract-nested-router](https://www.npmjs.com/package/abstract-nested-router), so you can use nested routers, also:
14
15- Advanced parameters can be used, e.g. `/:id<\d+>` &mdash; [see docs](https://www.npmjs.com/package/abstract-nested-router#params)
16- ARIA-compliant, sets `[aria-current="page"]` on active links
17- Seamless `<base href="..." />` integration
18- Conditionals and redirection through props
19- Fallback `<Route />` handlers
20- Hash and URI-based routes
21- Support for [query-string](https://www.npmjs.com/package/query-string)
22- [REPL ready!](https://svelte.dev/repl/0f07c6134b16432591a9a3a0095a80de?version=3.12.1)
23
24> `yrv` will use any _base-href_ found on the current page to rewrite links and routes.
25
26## Usage
27
28Install `yrv` through NPM or Yarn:
29
30```html
31<script>
32 import { Router, Route, Link } from 'yrv';
33</script>
34
35<Link href="/">Home</Link>
36| <Link href="/World">Hello</Link>
37| <Link href="/not/found">NotFound</Link>
38
39<p>
40 <Router>
41 <Route exact>Hello World</Route>
42 <Route fallback>Not found</Route>
43 <Route exact path="/:name" let:router>Hey {router.params.name}!</Route>
44 </Router>
45</p>
46```
47
48> Notice `fallback` routes can’t be placed at the beginning, otherwise further routes will not be mounted. :bomb:
49
50## Components
51
52> You MUST declare at least, one top-level `Router` to setup the bindings.
53
54### `<Router {path} {disabled} {condition} {nofallback} />`
55
56This component will hold any given routes as children, path is always derived from parent ones.
57
58Available props:
59
60- `{path}` &mdash; Any segment to derive a fullpath from, default to `/`
61- `{disabled}` &mdash; Boolean; Similar to condition, but for bound props
62- `{condition}` &mdash; Function; if given, render only if evaluates to true
63- `{nofallback}` &mdash; If set, non-matched routes will never raise a failure
64
65### `<Route {key} {path} {props} {exact} {dynamic} {pending} {fallback} {component} {disabled} {condition} {redirect} let:router />`
66
67Main container for routing, they can hold any component or children.
68
69Available props:
70
71- `{key}` &mdash; The route identity, not its path; default to random pseudo-hash
72- `{path}` &mdash; Any segment to derive a fullpath from, default to `/`
73- `{props}` &mdash; Additional properties for rendered component
74- `{exact}` &mdash; If set, the route will render only if the route exactly matches
75- `{dynamic}` &mdash; Promise, if set will resolve and use its result as lazy-component
76- `{pending}` &mdash; String, this value is rendered during the loading of dynamic components
77- `{fallback}` &mdash; If set, the route will render only if no more routes were matched
78- `{component}` &mdash; A valid svelte-component to render if the route matches
79- `{disabled}` &mdash; Boolean; Similar to condition, but for bound props
80- `{condition}` &mdash; Function; if given, the route will render only if evaluates to true
81- `{redirect}` &mdash; Alternate redirection location, only if the previous condition was true
82- `let:router` &mdash; Injects the `router` context, it also provides `failure` in case of errors
83
84> If you omit `exact`, then `/x` would match both `/` and `/x` routes &mdash; [see docs](https://www.npmjs.com/package/abstract-nested-router#params)
85
86### `<Link {go} {href} {title} {exact} {reload} {replace} {class|className} />`
87
88In order to navigate, you can use `Link` components, or regular links, etc.
89
90> All `href` values MUST be absolute, only links starting with `/` or `#` are allowed.
91
92Available props:
93
94- `{go}` &mdash; History shortcut (see below)
95- `{href}` &mdash; New location, default to `/`
96- `{title}` &mdash; HTML title-attribute value
97- `{button}` &mdash; If set, will use button-tag instead
98- `{exact}` &mdash; Determine if link should match exactly to be set as active
99- `{reload}` &mdash; Use `location.href` instead
100- `{replace}` &mdash; Use `history.replaceState()` instead
101- `{class|className}` &mdash; Custom class-name for the mounted anchor
102
103Normal `on:click` events are still allowed, so you can use:
104
105```html
106<Link on:click={() => location.reload()}>Reload</Link>
107```
108
109> Active _links_ will gain the `[aria-current]` attribute, and `[disabled]` if they're buttons.
110
111Aditionally, you can setup `go` to moving around:
112
113- `"back"` &mdash; String; if given, will invoke `history.back()`
114- `"fwd"` &mdash; String; if given, will invoke `history.fwd()`
115- `n` &mdash; Number; if given, it'll be used to invoke `history.go(n)`
116
117> If navigating through `history` is not possible a normal redirection will run. :anchor:
118
119## Public API
120
121- `navigateTo(path[, options])` &mdash; Change the location, supported options are:
122 - `reload` &mdash; If true, it will use `document.location.href` instead
123 - `replace` &mdash; If true, it will use `history.replaceState()` instead
124 - `params` &mdash; Used to replace `:placeholders` from given path
125 - `queryParams` &mdash; Additional search-params for the new location
126- `$router` &mdash; Store with shared routeInfo details, similar to `let:router`
127
128> `yrv` gracefully degrades to `location.hash` on environments where `history` is not suitable, also it can be forced through `Router.hashchange = true`.
129
130### IE11 support
131
132Support for IE11 is _granted_ if you include, at least, the following polyfills before your application:
133
134```html
135<script>if (!!window.MSInputMethodContext && !!document.documentMode)
136 document.write('<script src="https://polyfill.io/v3/polyfill.min.js?features=default,Promise,Object.getOwnPropertyDescriptors"><\/script>');</script>
137<script src="your-app.js"></script>
138```
139
140> `document.write()` is used because conditional comments were dropped in IE10, so this way you can conditionally load polyfills anyway.
141
142Also, you MUST to [enable either `buble` or `babel`](https://github.com/sveltejs/svelte/issues/2621) within your build pipeline to transpile down to ES5.