Skip to content

Commit

Permalink
Merge pull request #7 from svelters/feature/named-routes
Browse files Browse the repository at this point in the history
Named routes
  • Loading branch information
Antoine Lelaisant authored Oct 18, 2019
2 parents 12ce521 + 4d7f55b commit c782975
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 6 deletions.
66 changes: 62 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ yarn add svelters-router
let routes = [
{
path: "/users",
component: Users
path: "/users",
component: Users
},
{
path: "/users/:id",
component: User
path: "/users/:id",
component: User
}
]
</script>
Expand Down Expand Up @@ -101,6 +101,8 @@ Route parameters are compiled by `svelters-router` and given to defined componen

### Links

#### The link action

```svelte
<script>
import { link } from 'svelters-router'
Expand All @@ -111,6 +113,18 @@ Route parameters are compiled by `svelters-router` and given to defined componen
<a href="/some/path" use:link>Just click here!</a>
```

#### The Link component

```svelte
<script>
import { Link } from 'svelters-router'
</script>
<p>I wonder how simple it is to create a link ?</p>
<Link href="/some/path">Just click here!</Link>
```

### To navigate somewhere programmatically

```svelte
Expand All @@ -129,3 +143,47 @@ Route parameters are compiled by `svelters-router` and given to defined componen
<button on:click={goToTheMoon}>Fly me to the moon!</button>
```

## Advanced

### Named routes

It's sometime more convenient to rely on route name instead of its path to generate links.

`svelters-router` provides all the stuff you need!

#### Route declaration

```svelte
<script>
import { Router, Route } from 'svelters-router'
import Home from './Home.svelte'
import Users from './Users.svelte'
import User from './User.svelte'
let routes = [
{
name: "user.show",
path: "/users/:id",
component: User
}
]
</script>
<Router {routes}>
<Route path="/" component={Home} />
<Route path="/users" component={Users} />
</Router>
```

#### Link to a named route

```svelte
<script>
import { Link } from 'svelters-router'
let userId = 1234
</script>
<Link name="user.show" params={{id: userId}}>See the user {userId}</Link>
```
28 changes: 28 additions & 0 deletions src/components/Link.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
import { derived } from 'svelte/store'
import link from '../link'
import { routes } from '../store'
import { compileParameters } from '../utils'
export let name = null
export let params = {}
export let href = null
let path = derived(routes, routes => {
if (href !== null) {
return href
}
const index = routes.findIndex(route => route.name === name)
if (index === -1) {
return null
}
return compileParameters(routes[index].path, params)
})
</script>

<a href={$path} use:link>
<slot></slot>
</a>
21 changes: 20 additions & 1 deletion src/components/Route.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
<script>
import { location } from '../store.js'
import { onMount, onDestroy } from 'svelte'
import { location, routes } from '../store.js'
import { matchPath, parseParameters } from '../utils.js'
export let name = null
export let path = null
export let component = null
let routeParams = {}
onMount(() => {
routes.update(routes => {
routes.push({ name, path })
return routes
})
})
onDestroy(() => {
routes.update(routes => {
const index = routes.findIndex(route => route.path === path)
routes.splice(index, 1)
return routes
})
})
$: if (matchPath($location.pathname, path)) {
routeParams = parseParameters($location.pathname, path)
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
</script>

{#each routes as route}
<Route path={route.path} component={route.component} />
<Route
component={route.component}
name={route.name}
path={route.path}
/>
{/each}
<slot></slot>
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as Router } from './components/Router.svelte'
export { default as Route } from './components/Route.svelte'
export { default as Link } from './components/Link.svelte'
export { push, replace, goBack, goForward } from './history'
export { default as link } from './link'
1 change: 1 addition & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { writable } from 'svelte/store'

export const location = writable(null)
export const routes = writable([])
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export const parseParameters = (location, path) => {
return memo
}, {})
}

export const compileParameters = (path, parameters) => {
const toPath = pathToRegexp.compile(path)

return toPath(parameters)
}

0 comments on commit c782975

Please sign in to comment.