@kitbag/router 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +138 -0
- package/dist/kitbag-router.d.ts +4132 -0
- package/dist/kitbag-router.js +4947 -0
- package/dist/kitbag-router.umd.cjs +4951 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present, Craig Harshbarger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# @kitbag/router
|
|
2
|
+
|
|
3
|
+
A simple and versatile mapping utility for Typescript.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.org/package/kitbag/router)
|
|
6
|
+
[](https:/unpkg.com/@kitbag/router/dist/kitbag-router)
|
|
7
|
+
[](https://app.netlify.com/sites/kitbag-router/deploys)
|
|
8
|
+
|
|
9
|
+
Get started with the [documentation](https://kitbag-router.netlify.app/)
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install Kitbag Router with your favorite package manager
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# bun
|
|
17
|
+
bun add @kitbag/router
|
|
18
|
+
# yarn
|
|
19
|
+
yarn add @kitbag/router
|
|
20
|
+
# npm
|
|
21
|
+
npm install @kitbag/router
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Define Basic Routes
|
|
25
|
+
|
|
26
|
+
Create an array of possible routes. Learn more about [defining routes](https://kitbag-router.netlify.app/core-concepts/defining-routes).
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// /routes.ts
|
|
30
|
+
import { createRoutes } from '@kitbag/router'
|
|
31
|
+
|
|
32
|
+
const Home = { template: '<div>Home</div>' }
|
|
33
|
+
const About = { template: '<div>About</div>' }
|
|
34
|
+
|
|
35
|
+
export const routes = createRoutes([
|
|
36
|
+
{ name: 'home', path: '/', component: Home },
|
|
37
|
+
{ name: 'path', path: '/about', component: About },
|
|
38
|
+
])
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Plugin
|
|
42
|
+
|
|
43
|
+
Create a router instance and pass it to the app as a plugin
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { createApp } from 'vue'
|
|
47
|
+
import { createRouter } from '@kitbag/router'
|
|
48
|
+
import { routes } from '/routes'
|
|
49
|
+
import App from './App.vue'
|
|
50
|
+
|
|
51
|
+
const router = createRouter(routes)
|
|
52
|
+
const app = createApp(App)
|
|
53
|
+
|
|
54
|
+
app.use(router)
|
|
55
|
+
app.mount('#app')
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Update Registered Router
|
|
59
|
+
|
|
60
|
+
This block utilizes [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) to provide the internal types to match the actual router you're using. You put this in main.ts right after you call `createRouter`, or you can export your router and put this interface inside of a `router.d.ts` file, anywhere that your tsconfig can find it.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
declare module '@kitbag/router' {
|
|
64
|
+
interface Register {
|
|
65
|
+
router: typeof router
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Push
|
|
71
|
+
|
|
72
|
+
To navigate to another route, you can use `router.push`. This method will update the URL for the browser and also add the URL into the history so when a user uses the back button on their browser it will behave as expected.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { createRoutes, useRouter } from '@kitbag/router'
|
|
76
|
+
|
|
77
|
+
const routes = createRoutes([
|
|
78
|
+
{
|
|
79
|
+
name: 'user',
|
|
80
|
+
path: '/user',
|
|
81
|
+
component: ...,
|
|
82
|
+
children: createRoutes([
|
|
83
|
+
{
|
|
84
|
+
name: 'profile',
|
|
85
|
+
path: '/profile',
|
|
86
|
+
component: ...,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'settings',
|
|
90
|
+
path: '/settings',
|
|
91
|
+
component: ...,
|
|
92
|
+
}
|
|
93
|
+
])
|
|
94
|
+
}
|
|
95
|
+
])
|
|
96
|
+
|
|
97
|
+
const router = useRouter(routes)
|
|
98
|
+
|
|
99
|
+
router.push('user.settings')
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The push method also accepts a plain string if you know the URL you want to go to.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
router.push('/user/settings')
|
|
106
|
+
router.push('https://github.com/kitbagjs/router')
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
This `source` argument is type safe, expecting either a Url or a valid route "key". Url is any string that starts with "http", "https", or a forward slash "/". Route key is a string of route names joined by a period `.` that lead to a non-disabled route. Additionally if using the route key, push will require params be passed in if there are any.
|
|
110
|
+
|
|
111
|
+
## RouterView
|
|
112
|
+
|
|
113
|
+
Give your route components a place to be mounted
|
|
114
|
+
|
|
115
|
+
```html {4-5}
|
|
116
|
+
<!-- App.vue -->
|
|
117
|
+
<div class="app">
|
|
118
|
+
...
|
|
119
|
+
<!-- matched route.component gets rendered here -->
|
|
120
|
+
<router-view />
|
|
121
|
+
</div>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
This component can be mounted anywhere you want route components to be mounted. Nested routes can also have a nested `RouterView` which would be responsible for rendering any children that route may have. See more about [nested routes](https://kitbag-router.netlify.app/core-concepts/defining-routes#nested-routes).
|
|
125
|
+
|
|
126
|
+
## RouterLink
|
|
127
|
+
|
|
128
|
+
Use RouterLink for navigating between routes.
|
|
129
|
+
|
|
130
|
+
```html {3-4}
|
|
131
|
+
<template>
|
|
132
|
+
...
|
|
133
|
+
<!-- router-link renders as <a> with href -->
|
|
134
|
+
<router-link :to="(resolve) => resolve('home')">Go somewhere</router-link>
|
|
135
|
+
</template>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
This component gives the router the power to change the URL without reloading the page.
|