Skip to content

Commit 03e24f4

Browse files
authoredNov 17, 2024··
feat(Link): allow partial query match for activeClass (#2663)
1 parent d0e626c commit 03e24f4

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed
 

‎docs/content/2.components/link.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The Link component is a wrapper around [`<NuxtLink>`](https://nuxt.com/docs/api/
1414
- `inactive-class` prop to set a class when the link is inactive, `active-class` is used when active.
1515
- `exact` prop to style with `active-class` when the link is active and the route is exactly the same as the current route.
1616
- `exact-query` and `exact-hash` props to style with `active-class` when the link is active and the query or hash is exactly the same as the current query or hash.
17+
- use `exact-query="partial"` to style with `active-class` when the link is active and the query partially match the current query.
1718

1819
The incentive behind this is to provide the same API as NuxtLink back in Nuxt 2 / Vue 2. You can read more about it in the Vue Router [migration from Vue 2](https://router.vuejs.org/guide/migration/#removal-of-the-exact-prop-in-router-link) guide.
1920

‎src/runtime/components/elements/Link.vue

+17-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
</template>
3333

3434
<script lang="ts">
35-
import { isEqual } from 'ohash'
36-
import { defineComponent } from 'vue'
35+
import { isEqual, diff } from 'ohash'
36+
import { type PropType, defineComponent } from 'vue'
3737
import { nuxtLinkProps } from '../../utils'
3838
3939
export default defineComponent({
@@ -61,7 +61,7 @@ export default defineComponent({
6161
default: false
6262
},
6363
exactQuery: {
64-
type: Boolean,
64+
type: [Boolean, String] as PropType<boolean | 'partial'>,
6565
default: false
6666
},
6767
exactHash: {
@@ -74,9 +74,21 @@ export default defineComponent({
7474
}
7575
},
7676
setup(props) {
77+
function isPartiallyEqual(item1, item2) {
78+
const diffedKeys = diff(item1, item2).reduce((filtered, q) => {
79+
if (q.type === 'added') {
80+
filtered.push(q.key)
81+
}
82+
return filtered
83+
}, [])
84+
return isEqual(item1, item2, { excludeKeys: key => diffedKeys.includes(key) })
85+
}
86+
7787
function resolveLinkClass(route, $route, { isActive, isExactActive }: { isActive: boolean, isExactActive: boolean }) {
78-
if (props.exactQuery && !isEqual(route.query, $route.query)) {
79-
return props.inactiveClass
88+
if (props.exactQuery === 'partial') {
89+
if (!isPartiallyEqual(route.query, $route.query)) return props.inactiveClass
90+
} else if (props.exactQuery === true) {
91+
if (!isEqual(route.query, $route.query)) return props.inactiveClass
8092
}
8193
if (props.exactHash && route.hash !== $route.hash) {
8294
return props.inactiveClass

0 commit comments

Comments
 (0)
Please sign in to comment.