Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: vue binding #19

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
docs: vue binding
  • Loading branch information
thihathit committed Jun 13, 2023
commit 08884a9d3495bbf88762b61e1e34383eec17ac9c
115 changes: 114 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Usage

- [<img src="./docs/logos/javascript.svg" width="14"/> Vanilla JS](#-vanillajs)
- [<img src="./docs/logos/vue.svg" width="14"/> Vue](#-vue-bindings-via-shallowrefcomputed)
- [<img src="./docs/logos/react.svg" width="14"/> React](#-react-bindings-via-usestate)
- [<img src="./docs/logos/svelte.svg" width="14"/> Svelte](#-svelte-bindings-via-readablederived)

Expand Down Expand Up @@ -155,13 +156,126 @@ const App: FC = () => {
}
```

### <img src="./docs/logos/vue.svg" width="14"/> Vue bindings: via `shallowRef`/`computed`

```ts
// router.(ts|js)

import { computed, shallowRef } from 'vue'
import { CreateHistory } from 'rutter'

import { mapValues } from 'lodash-es'

const router = new CreateHistory({
routes: {
index: {
pathname: '/'
},
about: {
pathname: '/about'
},
blog: {
pathname: '/blog'
},
blogDetail: {
pathname: '/blog/:id'
}
}
})

const {
//
summaryState,
routeState,
watchSummaryState,
watchRouteState,
on
} = router

export const { redirect } = router

export const routerState = shallowRef(summaryState)
export const route = shallowRef(routeState)

export const is404 = computed(() => route.value.is404)

export const matches = computed(() => {
const { details } = routerState.value

type RouteNames = keyof typeof details

return mapValues(details, (_, name) => on(name as RouteNames))
})

watchSummaryState(state => {
routerState.value = state
})

watchRouteState(state => {
route.value = state
})
```

```vue
<script setup lang="ts">
// app.vue
import { redirect, route, matches, is404 } from './router'
</script>

<template>
<nav>
<button @click="() => redirect('index')">Index</button>

<button @click="() => redirect('blog')">Blog</button>

<a href="/invalid-url">
<button>404</button>
</a>
</nav>

<fieldset>
<legend>Body:</legend>
<div>
<h1 v-if="is404">404 Page</h1>

<template v-else>
<h1 v-if="matches.index">Index Page</h1>

<h1 v-if="matches.about">About Page</h1>

<template v-if="matches.blog">
<h1>Blog Page</h1>

<button
@click="() => redirect('blogDetail', { params: { id: 123 } })"
>
Blog Detail
</button>
</template>

<h1 v-if="matches.blogDetail">Blog Detail Page</h1>
</template>
</div>
</fieldset>

<fieldset>
<legend>Current route detail:</legend>

<code>
<pre>{{ route }}</pre>
</code>
</fieldset>
</template>
```

### <img src="./docs/logos/svelte.svg" width="14"/> Svelte bindings: via `readable`/`derived`

```ts
// router.(ts|js)

import { readable, derived } from 'svelte/store'
import { CreateHistory } from 'rutter'

import { mapValues } from 'lodash-es'

const router = new CreateHistory({
Expand Down Expand Up @@ -252,7 +366,6 @@ export const matches = derived(routerState, ({ details }) =>
<pre>{data}</pre>
</code>
</fieldset>

```

## Documentation
Expand Down
8 changes: 8 additions & 0 deletions docs/logos/vue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.