Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

Commit 7d7e4af

Browse files
author
Jorge Bucaran
committed
docs
1 parent 5b2c23c commit 7d7e4af

File tree

8 files changed

+169
-639
lines changed

8 files changed

+169
-639
lines changed

README.md

Lines changed: 149 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,68 @@
44
[![npm](https://img.shields.io/npm/v/@hyperapp/router.svg)](https://www.npmjs.org/package/hyperapp)
55
[![Slack](https://hyperappjs.herokuapp.com/badge.svg)](https://hyperappjs.herokuapp.com "Join us")
66

7-
@hyperapp/router provides utilities for routing client-side pages with [Hyperapp](https://github.com/hyperapp/hyperapp) using the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History).
7+
@hyperapp/router provides components for routing client-side pages with [Hyperapp](https://github.com/hyperapp/hyperapp) using the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History).
88

99
[Try it Online](http://hyperapp-router.surge.sh)
1010

1111
```jsx
12-
import { router, Link } from "@hyperapp/router"
13-
14-
app({
15-
view: [
16-
[
17-
"/",
18-
(state, actions) =>
19-
<Link to="/test" go={actions.router.go}>
20-
Test
21-
</Link>
22-
],
23-
[
24-
"/test",
25-
(state, actions) =>
26-
<Link to="/" go={actions.router.go}>
27-
Back
28-
</Link>
29-
]
30-
],
31-
mixins: [router()]
12+
import { h, app } from "hyperapp"
13+
import { location, Link, Route } from "@hyperapp/router"
14+
15+
const homeView = () => <h2>Home</h2>
16+
const aboutView = () => <h2>About</h2>
17+
const topicsView = ({ match }) => (
18+
<div>
19+
<h2>Topics</h2>
20+
<ul>
21+
<li>
22+
<Link to={`${match.url}/components`}>Components</Link>
23+
</li>
24+
<li>
25+
<Link to={`${match.url}/single-state-tree`}>Single State Tree</Link>
26+
</li>
27+
<li>
28+
<Link to={`${match.url}/routing`}>Routing</Link>
29+
</li>
30+
</ul>
31+
32+
{match.isExact && <h3>Please select a topic.</h3>}
33+
34+
<Route parent path={`${match.path}/:topicId`} view={topicView} />
35+
</div>
36+
)
37+
const topicView = ({ match }) => <h3>{match.params.topicId}</h3>
38+
39+
const actions = app({
40+
state: {
41+
location: location.state
42+
},
43+
actions: {
44+
location: location.actions
45+
},
46+
view: state =>
47+
<div>
48+
<ul>
49+
<li>
50+
<Link to="/">Home</Link>
51+
</li>
52+
<li>
53+
<Link to="/about">About</Link>
54+
</li>
55+
<li>
56+
<Link to="/topics">Topics</Link>
57+
</li>
58+
</ul>
59+
60+
<hr />
61+
62+
<Route path="/" view={homeView} />
63+
<Route path="/about" view={aboutView} />
64+
<Route parent path="/topics" view={topicsView} />
65+
</div>
3266
})
67+
68+
location.subscribe(actions.location)
3369
```
3470

3571
## Installation
@@ -43,7 +79,7 @@ Download the minified library from a [CDN](https://unpkg.com/@hyperapp/router).
4379
Then import from `router`.
4480

4581
```jsx
46-
const { router, Link } = router
82+
const { location, Route, Link } = router
4783
```
4884

4985
Or install with npm / Yarn.
@@ -52,120 +88,140 @@ Or install with npm / Yarn.
5288
npm i <a href="https://www.npmjs.com/package/@hyperapp/router">@hyperapp/router</a>
5389
</pre>
5490

55-
Then [bundle](https://github.com/hyperapp/hyperapp/blob/master/docs/getting-started.md#build-pipeline) and use as you would any other module.
91+
Then with a module bundler like [Rollup](https://github.com/rollup/rollup) or [Webpack](https://github.com/webpack/webpack), use as you would anything else.
5692

5793
```jsx
58-
import { router, Link } from "@hyperapp/router"
94+
import { location, Route, Link } from "@hyperapp/router"
5995
```
6096

61-
## Mixin
97+
## Usage
6298

63-
Use the router as any other [mixin](https://github.com/hyperapp/hyperapp/blob/master/docs/mixins.md). Then compose your view as an array of [routes](#routes).
99+
Add the `location` state and actions to your application.
64100

65101
```jsx
66-
app({
67-
view: [
68-
["/", state => <h1>Hi.</h1>]
69-
["*", state => <h1>404</h1>],
70-
],
71-
mixins: [router()]
102+
const actions = app({
103+
state: {
104+
location: location.state
105+
},
106+
actions: {
107+
location: location.actions
108+
}
72109
})
73110
```
74111

75-
### Routes
76-
77-
A route is a tuple that consists of a [path](#paths) and a [view](https://github.com/hyperapp/hyperapp/blob/master/docs/view.md).
78-
79-
<pre>
80-
[string, <a href="https://github.com/hyperapp/hyperapp/blob/master/docs/api.md#view">View</a>]
81-
</pre>
82-
83-
Routes are matched in the following three scenarios:
112+
Then call `subscribe` to listen to location change events.
84113

85-
- After the page is loaded.
86-
- When the browser fires a [popstate](https://developer.mozilla.org/en-US/docs/Web/Events/popstate) event.
87-
- When [actions.router.go](#actionsroutergo) is called.
114+
```js
115+
location.subscribe(actions.location)
116+
```
88117

89-
If [location.pathname](https://developer.mozilla.org/en-US/docs/Web/API/Location) matches the path of a supplied route, we'll render its view.
118+
## Modules
90119

91-
### Paths
120+
### location
92121

93-
#### `/`, `/foo`
122+
#### pathname
123+
#### previous
94124

95-
Match if location.pathname is `/`, `/foo`, etc.
125+
#### go
96126

97-
#### `/:key`
98127

99-
Match location.pathname using `[A-Za-z0-9]+` and save the matched path to [state.router.params](#staterouterparams).
128+
## Components
100129

101-
#### `*`
130+
### Route
102131

103-
Match anything. Declaration order dictates matching precedence. If you are using `*`, declare it last.
132+
Render some UI when the current [window location](https://developer.mozilla.org/en-US/docs/Web/API/Location) matches the given path. A route with no path always matches. Routes work as expected when nested inside other routes.
104133

105-
### state.router.match
134+
```jsx
135+
<Route path="/" render={homeView} />
136+
<Route path="/about" render={aboutView} />
137+
<Route parent path="/topics" render={topicsView} />
138+
```
106139

107-
The matched path.
140+
#### parent
108141

109-
<pre>
110-
string
111-
</pre>
142+
The route contains child routes.
112143

113-
### state.router.params
144+
#### path
114145

115-
The matched path params.
146+
The path to match against the current location.
116147

117-
<pre>
118-
{
119-
[key]: string
120-
}
121-
</pre>
148+
#### render
122149

123-
|path |location.pathname |state.router.params |
124-
|----------------------|---------------------|---------------------|
125-
|`/:foo` |/hyper | { foo: "hyper" } |
150+
The component to render when a match occurs.
126151

127-
### actions.router.go
152+
### Link
128153

129-
Update [location.pathname](https://developer.mozilla.org/en-US/docs/Web/API/Location) with the supplied path.
154+
Use the Link component to update the current [window location](https://developer.mozilla.org/en-US/docs/Web/API/Location) and navigate between views without a page reload. The new location will be pushed to the history stack using `history.pushState`.
130155

131-
<pre>
132-
actions.router.go(<a href="#paths">path</a>)
133-
</pre>
156+
```jsx
157+
<ul>
158+
<li>
159+
<Link to="/">Home</Link>
160+
</li>
161+
<li>
162+
<Link to="/about">About</Link>
163+
</li>
164+
<li>
165+
<Link to="/topics">Topics</Link>
166+
</li>
167+
</ul>
168+
```
134169

135-
### events.route
170+
#### to
136171

137-
Use route to make a network request, parse [location.search](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search), etc. This event is fired when a new route is matched.
172+
The link's destination.
138173

139-
<pre>
140-
<a id="routeevent"></a>route(<a href="#state">State</a>, <a href="#actions">Actions</a>, <a href="#routeinfo">RouteInfo</a>): <a href="#routeinfo">RouteInfo</a>
141-
</pre>
174+
### Redirect
142175

143-
#### RouteInfo
176+
Use the Redirect component to navigate to a new location. The new location will override the current location in the history stack using `history.replaceState`.
144177

145-
<pre>
146-
{
147-
match: string,
148-
params: any
178+
```jsx
179+
const Login = ({ from, login, redirectToReferrer }) => props => {
180+
if (redirectToReferrer) {
181+
return <Redirect to={from} />
182+
}
183+
184+
return (
185+
<div>
186+
<p>
187+
You must log in to view the page at {from}.
188+
</p>
189+
<button
190+
onclick={() => {
191+
auth.authenticate(userId => login(userId))
192+
}}
193+
>
194+
Log in
195+
</button>
196+
</div>
197+
)
149198
}
150-
</pre>
199+
```
151200

152-
## Link
201+
#### to
153202

154-
Use `Link` to create hyperlinks that map to a [route](#routes).
203+
The redirect's destination.
155204

156-
```jsx
157-
<Link to="/" go={actions.router.go}>Back Home</Link>
158-
```
205+
#### from
159206

160-
### to
207+
Overwrite the previous pathname. See [location.previous](#previous).
161208

162-
A route [path](#paths).
209+
### Switch
163210

164-
### go
211+
Use the Switch component when you want to ensure only one out of several routes is rendered. It always renders the first matching child.
165212

166-
A function that will be called with the supplied path when the hyperlink is clicked.
213+
```jsx
214+
<Switch>
215+
<Route path="/" view={Home} />
216+
<Route
217+
path="/old-match"
218+
view={() => <Redirect from="/old-match" to="/will-match" />}
219+
/>
220+
<Route path="/will-match" view={WillMatch} />
221+
<Route view={NoMatch} />
222+
</Switch>
223+
```
167224

168225
## License
169226

170227
@hyperapp/router is MIT licensed. See [LICENSE](LICENSE.md).
171-

package.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,14 @@
1414
"author": "Jorge Bucaran",
1515
"keywords": [
1616
"hyperapp",
17-
"react",
18-
"elm",
19-
"jsx",
20-
"virtual dom",
21-
"vdom",
22-
"hyperx",
23-
"hyperscript",
24-
"router"
17+
"router",
18+
"navigation"
2519
],
2620
"scripts": {
2721
"test": "jest --coverage --no-cache",
2822
"build": "npm run bundle && npm run minify",
2923
"bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn router -g hyperapp:hyperapp",
30-
"minify": "uglifyjs dist/router.js -o dist/router.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --in-source-map dist/router.js.map --source-map dist/router.js.map",
24+
"minify": "uglifyjs dist/router.js -o dist/router.js -mc pure_funcs=Object.defineProperty --source-map includeSources,url=router.js.map",
3125
"prepublish": "npm run build",
3226
"format": "prettier --semi false --write 'src/**/*.js'",
3327
"release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
@@ -41,7 +35,7 @@
4135
"jest": "^21.2.1",
4236
"prettier": "^1.8.2",
4337
"rollup": "^0.52.0",
44-
"uglify-js": "^2.7.5"
38+
"uglify-js": "3.2.1"
4539
},
4640
"peerDependencies": {
4741
"hyperapp": "*"

src/Route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function Route(props) {
1111

1212
return (
1313
match &&
14-
props.view({
14+
props.render({
1515
location: location,
1616
match: match
1717
})

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export { Link } from "./Link"
22
export { Route } from "./Route"
33
export { Switch } from "./Switch"
44
export { Redirect } from "./Redirect"
5-
export { default as location } from "./location"
5+
export { location } from "./location"

0 commit comments

Comments
 (0)