-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.ts
212 lines (185 loc) · 4.31 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import '../global.css'
import {
RouteLocationNormalizedLoaded,
createRouter,
createWebHistory,
useRoute,
loadRouteLocation,
} from 'vue-router'
import {
createApp,
readonly,
ref,
watchEffect,
computed,
defineComponent,
} from 'vue'
const users = readonly([
{ name: 'John' },
{ name: 'Jessica' },
{ name: 'James' },
])
const historyState = ref(history.state || {})
async function showUserModal(id: number) {
// add backgroundView state to the location so we can render a different view from the one
const backgroundView = router.currentRoute.value.fullPath
await router.push({
name: 'user',
params: { id },
state: { backgroundView },
})
}
function closeUserModal() {
history.back()
}
const Home = defineComponent({
template: `<div>
<h1>Home</h1>
<p>Select a user</p>
<ul>
<li v-for="(user, id) in users">
<router-link :to="{ name: 'user', params: { id }}">{{ user.name }}</router-link>
- <button @click="showUserModal(id)">Details</button>
</li>
</ul>
<router-view />
<dialog ref="modal" id="dialog">
<div>
<div v-if="userId">
<p>
User #{{ userId }}
<br>
Name: {{ users[userId].name }}
</p>
<router-link to="/about">Go somewhere else</router-link>
<br>
<button @click="closeUserModal">Close</button>
</div>
</div>
</dialog>
</div>`,
setup() {
const modal = ref<HTMLDialogElement | HTMLElement>()
const route = useRoute()
const userId = computed(() => route.params.id)
watchEffect(
() => {
const el = modal.value
if (!el) return
const show = historyState.value.backgroundView
console.log('show modal?', show)
if (show) {
if ('show' in el) el.show()
else el.setAttribute('open', '')
} else {
if ('close' in el) el.close()
else el.removeAttribute('open')
}
},
{ flush: 'post' }
)
return {
modal,
historyState,
showUserModal,
closeUserModal,
userId,
users,
}
},
})
const About = defineComponent({
template: `<div>
<h1>About</h1>
<button @click="back">Back</button>
<span> | </span>
<router-link to="/">Back home</router-link>
</div>`,
methods: {
back: () => history.back(),
},
})
const Child = defineComponent({
template: `<div class="child">child</div>`,
})
const UserDetails = defineComponent({
template: `<div>
<h1>User #{{ id }}</h1>
<p>
Name: {{ users[id].name }}
</p>
<router-link to="/">Back home</router-link>
</div>`,
props: {
id: {
type: String,
required: true,
},
},
data: () => ({ users }),
})
const webHistory = createWebHistory('/modal')
const router = createRouter({
history: webHistory,
routes: [
{
path: '/',
component: Home,
children: [
// to check that displaying the modal doesn't change this
{ path: '', component: Child },
],
},
{ path: '/about', component: About },
{
path: '/users/:id',
props: true,
name: 'user',
component: UserDetails,
},
],
})
router.afterEach(() => {
historyState.value = history.state
})
router.beforeEach((to, from) => {
console.log('---')
console.log('going from', from.fullPath, 'to', to.fullPath)
console.log('state:', window.history.state)
console.log('---')
})
router.beforeResolve(async to => {
if (historyState.value && historyState.value.backgroundView) {
await loadRouteLocation(router.resolve(historyState.value.backgroundView))
}
})
// avoid navigating to non existent users
router.beforeEach(to => {
if (to.name !== 'user') return
const { id } = to.params
return (
typeof id === 'string' && !Number.isNaN(Number(id)) && !!users[Number(id)]
)
})
const app = createApp({
setup() {
const route = useRoute()
const routeWithModal = computed(() => {
if (historyState.value.backgroundView) {
return router.resolve(
historyState.value.backgroundView
) as RouteLocationNormalizedLoaded
} else {
return route
}
})
return { routeWithModal }
},
template: `
<router-view :route="routeWithModal"></router-view>
`,
})
app.use(router)
window.vm = app.mount('#app')
// @ts-expect-error
window.router = router