Skip to content

Commit ed64b46

Browse files
authored
Change active and exact-active behavior for router-link (#136)
* add router-active-link * typo * add example * add notes about breaking changes * add section about aliases * add notes about absolute nested aliases * rename
1 parent 9a1be84 commit ed64b46

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed
+298
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
- Start Date: 2020-02-28
2+
- Target Major Version: Router v4
3+
- Reference Issues: https://github.com/vuejs/vue-router/issues/2040
4+
- Implementation PR:
5+
6+
# Summary
7+
8+
Change how `router-link-active` is applied to align more with the router concepts. Right now, a link is _active_ if the generated url is a partial version of the current location. This creates a few issues:
9+
10+
- Aliases are not (necessarily) matched
11+
- While being on a child with a leading slash, a link to its parent won't show as active because the urls are different
12+
- Query is used to make the link _active_
13+
14+
# Motivation
15+
16+
The issues mentioned above are hard or impossible to implement in userland whereas implementing the current behavior is very straightforward:
17+
18+
```vue
19+
<router-link v-slot="{ route }">
20+
partial path: {{ $route.path.startsWith(route.path) }}
21+
<br />
22+
partial path + partial query: {{ $route.path.startsWith(route.path) && includesQuery($route.query, route.query) }}
23+
</router-link>
24+
```
25+
26+
```js
27+
// this is only necessary if we care about matching the query
28+
function includesQuery(outter, inner) {
29+
for (let key in inner) {
30+
let innerValue = inner[key]
31+
let outterValue = outter[key]
32+
if (typeof innerValue === 'string') {
33+
if (innerValue !== outterValue) return false
34+
} else {
35+
if (
36+
!Array.isArray(outterValue) ||
37+
outterValue.length !== innerValue.length ||
38+
innerValue.some((value, i) => value !== outterValue[i])
39+
)
40+
return false
41+
}
42+
}
43+
44+
return true
45+
}
46+
```
47+
48+
They also make more sense from a router perspective because links will be active based on the route record being active or not. This is specially important for nested routes and aliases which currently are not active if they do not share a part of the current url with the router-link location.
49+
It also makes more sense from a navigation perspective for an active link not to trigger a new navigation (except in the case of nested routes where it could be a navigation).
50+
51+
# Detailed design
52+
53+
The active behavior should be more connected to the _Matcher_ part of the router, this means it should be related to what is being rendered by the current url, which is part of the _Route Record_. Therefore, the `query` and `hash` should not influence this. It also seems to be something [people are really interested in](https://github.com/vuejs/vue-router/issues/2040) but **I don't know if people actually benefit from active working with `query`**.
54+
55+
## `router-link-exact-active`
56+
57+
This change also affects the behavior of _exact active_. The class `router-link-exact-active` is applied only if the current route is **exactly** the same as the one from the link, from a _Matcher_ perspective. This means that `query` and `hash` are not relevant (same as _active_).
58+
59+
This new _active_ behavior completely eliminates the caveat of the `exact` prop for the _root link_ (`/`) and makes https://github.com/vuejs/rfcs/pull/36 unecessary without introducing an inconsistency.
60+
61+
## Nested Routes
62+
63+
It's worth noting that nested routes will match only if the params relevant to the rendered route are the same.
64+
65+
E.g., given these routes:
66+
67+
```js
68+
const routes = [
69+
{
70+
path: '/parent/:id',
71+
children: [
72+
// empty child
73+
{ path: '' },
74+
// child with id
75+
{ path: 'child/:id' },
76+
{ path: 'child-second/:id' }
77+
]
78+
}
79+
]
80+
```
81+
82+
If the current route is `/parent/1/child/2`, these links will be active:
83+
84+
| url | active | exact active |
85+
| ------------------------ | ------ | ------------ |
86+
| /parent/1/child/2 |||
87+
| /parent/1/child/3 |||
88+
| /parent/1/child-second/2 |||
89+
| /parent/1 |||
90+
| /parent/2 |||
91+
| /parent/2/child/2 |||
92+
| /parent/2/child-second/2 |||
93+
94+
## Unrelated but similiar routes
95+
96+
Routes that are unrelated from a record point of view but share a common path are no longer active.
97+
98+
E.g., given these routes:
99+
100+
```js
101+
const routes = [
102+
{ path: '/movies' },
103+
{ path: '/movies/new' },
104+
{ path: '/movies/search' }
105+
]
106+
```
107+
108+
If the current route is `/movies/new`, these links will be active:
109+
110+
| url | active | exact active |
111+
| -------------- | ------ | ------------ |
112+
| /movies |||
113+
| /movies/new |||
114+
| /movies/search |||
115+
116+
Note: **This behavior is different from actual behavior**.
117+
118+
It's worth noting, it is possible to nest them to still benefit from links being _active_:
119+
120+
```js
121+
const routes = [
122+
{
123+
path: '/movies',
124+
// we need this to render the children (see note below)
125+
component: { render: () => h('RouterView') },
126+
children: [
127+
{ path: 'new' },
128+
// different child
129+
{ path: 'search' }
130+
]
131+
}
132+
]
133+
```
134+
135+
If the current route is `/movies/new`, these links will be active:
136+
137+
| url | active | exact active |
138+
| -------------- | ------ | ------------ |
139+
| /movies |||
140+
| /movies/new |||
141+
| /movies/search |||
142+
143+
Note: To make this easier to use, we could maybe allow `component` to be absent and internally behave as if there where a `component` option that renders a `RouterView` component
144+
145+
## Alias
146+
147+
Given that an alias is only a different `path` while keeping everything else on a record, it makes sense for aliases to be active when the `path` they are aliasing is matched and vice versa.
148+
149+
E.g., given these routes:
150+
151+
```js
152+
const routes = [{ path: '/movies', alias: ['/films'] }]
153+
```
154+
155+
If the current route is `/movies` **or** `/films`, both links will be active:
156+
157+
| url | active | exact active |
158+
| ------- | ------ | ------------ |
159+
| /movies |||
160+
| /films |||
161+
162+
### Nested aliases
163+
164+
The behavior is similar when dealing with nested children of an aliased route.
165+
166+
E.g., given these routes:
167+
168+
```js
169+
const routes = [
170+
{
171+
path: '/parent/:id',
172+
alias: '/p/:id',
173+
children: [
174+
// empty child
175+
{ path: '' },
176+
// child with id
177+
{ path: 'child/:id', alias: 'c/:id' }
178+
]
179+
}
180+
]
181+
```
182+
183+
If the current route is `/parent/1/child/2`, `/p/1/child/2`, `/p/1/c/2`, or, `/parent/1/c/2` these links will be active:
184+
185+
| url | active | exact active |
186+
| ----------------- | ------ | ------------ |
187+
| /parent/1/child/2 |||
188+
| /parent/1/c/2 |||
189+
| /p/1/child/2 |||
190+
| /p/1/c/2 |||
191+
| /p/1/child/3 |||
192+
| /parent/1/child/3 |||
193+
| /parent/1 |||
194+
| /p/1 |||
195+
| /parent/2 |||
196+
| /p/2 |||
197+
198+
### Absolute nested aliases
199+
200+
Nested children can have an absolute `path` by making it start with `/`, in this scenario the same rules apply. Given these routes:
201+
202+
E.g., given these routes:
203+
204+
```js
205+
const routes = [
206+
{
207+
path: '/parent/:id',
208+
alias: '/p/:id',
209+
name: 'parent',
210+
children: [
211+
// empty child
212+
{ path: '', alias: ['alias', '/p_:id'], name: 'child' },
213+
// child with absolute path. we need to add an `id` because the parent needs it
214+
{ path: '/p_:id/absolute-a', alias: 'as-absolute-a' },
215+
// same as above but the alias is absolute
216+
{ path: 'as-absolute-b', alias: '/p_:id/absolute-b' }
217+
]
218+
}
219+
]
220+
```
221+
222+
If the current route is `/p_1/absolute-a`, `/p/1/as-absolute-a`, or, `/parent/1/as-absolute-a`, these links will be active:
223+
224+
| url | active | exact active |
225+
| -------------------- | ------ | ------------ |
226+
| /p/1/as-absolute-a |||
227+
| /p_1/absolute-a |||
228+
| /parent/1/absolute-a |||
229+
| /parent/2/absolute-a |||
230+
| /parent/1/absolute-b |||
231+
| /p/1/absolute-b |||
232+
| /p_1/absolute-b |||
233+
| /parent/1 |||
234+
| /p/1 |||
235+
| /parent/1/alias |||
236+
| /p/1/alias |||
237+
| /p_1 |||
238+
| /parent/2 |||
239+
| /p/2 |||
240+
241+
Notice how the empty `path` record is _active_ but not _exact active_ differently from the other child `/p/1/absolute-b`. All its aliases are _active_ as well because they are aliases of an empty `path`. If it was the other way around: the `path` wasn't empty but one of the aliases was an empty `path`, then **none** of them would be active because the original `path` takes precedence over aliases.
242+
243+
### Named nested routes
244+
245+
If the url is resolved through the `name` of the parent, in this case `parent`, **it will not include the empty path child route**. This is important because they both resolve to the same url but when used in `router-link`'s `to` prop, they would yield different results when it comes to being _active_ and/or _exact active_. This is consistent with what they render being different and the rest of the active behavior.
246+
247+
E.g., given the routes from the previous example, **if the current location is `/parent/1` and, both the parent and child views are rendering, meaning we are effectively at `{ name: 'child' }` and not at `{ name: 'parent' }`**, here is a similar table to the ones before but also including `to`:
248+
249+
| `to`'s value | resolved url | active | exact active |
250+
| ----------------------------------------- | ------------------------- | ------ | ------------ |
251+
| `{ name: 'parent', params: { id: '1' } }` | `/parent/1` (parent) |||
252+
| `'/parent/1'` | `/parent/1` (child) |||
253+
| `{ name: 'child', params: { id: '1' } }` | `/parent/1` (child) |||
254+
| `'/p_1'` | `/p_1` (child) |||
255+
| `'/parent/1/alias'` | `/parent/1/alias` (child) |||
256+
257+
But **if the current location is `{ name: 'parent' }`**, it will still yield the same url, `/parent/1`, but a different table:
258+
259+
| `to`'s value | resolved url | active | exact active |
260+
| ----------------------------------------- | ------------------------- | ------ | ------------ |
261+
| `{ name: 'parent', params: { id: '1' } }` | `/parent/1` (parent) |||
262+
| `'/parent/1'` | `/parent/1` (child) |||
263+
| `{ name: 'child', params: { id: '1' } }` | `/parent/1` (child) |||
264+
| `'/p_1'` | `/p_1` (child) |||
265+
| `'/parent/1/alias'` | `/parent/1/alias` (child) |||
266+
267+
## Repeated params
268+
269+
With repeating params like
270+
271+
- `/articles/:id+`
272+
- `/articles/:id*`
273+
274+
**All params** must match, with the same exact order for a link **to be both**, _active_ and _exact active_.
275+
276+
## `exact` prop
277+
278+
Before these changes, `exact` worked by matching the whole location. Its main purpose was to get around the `/` caveat but it also checked `query` and `hash`.
279+
Because of this, with the new active behavior, the [`exact` prop](https://router.vuejs.org/api/#exact) only purpose would be for a link to display `router-link-active` only if `router-link-exact-active` is also present. But this isn't really useful anymore as we can directly target the element using the `router-link-exact-active` class.
280+
Because of this, I think the `exact` prop can be removed from `router-link`. This outdates https://github.com/vuejs/rfcs/pull/37 while still introducing the behavior of `exact` for the path section of the loction like explained above.
281+
282+
Some users will probably have to change the class used in CSS from `router-link-exact-active` to `router-link-active` to adapt to this change.
283+
284+
# Drawbacks
285+
286+
- This is not backwards compatible. It's probably worth adding `exact-path` in Vue Router v3.
287+
- Users using the `exact` prop will have to rely on the `router-link-exact-active` or use the `exact-active-class` prop.
288+
- The function `includesQuery` must be added by the user.
289+
290+
# Alternatives
291+
292+
- Leaving the `exact` prop to only apply `router-link-active` when `router-link-exact-active` as also applied.
293+
- Keep _active_ behavior of doing an inclusive match of `query` and `hash` instead of just relying on the params.
294+
- Changing _active_ behavior to only apply to the `path` section of a route (this includes params) and ignore `query` and `hash`.
295+
296+
# Adoption strategy
297+
298+
- Add `exact-path` to mitigate existing problems in v3

0 commit comments

Comments
 (0)