Skip to content

Commit

Permalink
feat(Link): allow partial query match for its active state (#2664)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
sandros94 and benjamincanac authored Nov 17, 2024
1 parent 8d85498 commit 7329900
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/content/3.components/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The Link component is a wrapper around [`<NuxtLink>`](https://nuxt.com/docs/api/
- `inactive-class` prop to set a class when the link is inactive, `active-class` is used when active.
- `exact` prop to style with `active-class` when the link is active and the route is exactly the same as the current route.
- `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.
- use `exact-query="partial"` to style with `active-class` when the link is active and the query partially match the current query.

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.

Expand Down
23 changes: 18 additions & 5 deletions src/runtime/components/Link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export interface LinkProps extends NuxtLinkProps {
active?: boolean
/** Will only be active if the current route is an exact match. */
exact?: boolean
/** Will only be active if the current route query is an exact match. */
exactQuery?: boolean
/** Allows controlling how the current route query sets the link as active. */
exactQuery?: boolean | 'partial'
/** Will only be active if the current route hash is an exact match. */
exactHash?: boolean
/** The class to apply when the link is inactive. */
Expand All @@ -94,7 +94,7 @@ extendDevtoolsMeta({ example: 'LinkExample' })

<script setup lang="ts">
import { computed } from 'vue'
import { isEqual } from 'ohash'
import { isEqual, diff } from 'ohash'
import { useForwardProps } from 'radix-vue'
import { reactiveOmit } from '@vueuse/core'
import { useRoute } from '#imports'
Expand Down Expand Up @@ -124,14 +124,27 @@ const ui = computed(() => tv({
}
}))
function isPartiallyEqual(item1: any, item2: any) {
const diffedKeys = diff(item1, item2).reduce((filtered, q) => {
if (q.type === 'added') {
filtered.push(q.key)
}
return filtered
}, [] as string[])
return isEqual(item1, item2, { excludeKeys: key => diffedKeys.includes(key) })
}
function isLinkActive({ route: linkRoute, isActive, isExactActive }: any) {
if (props.active !== undefined) {
return props.active
}
if (props.exactQuery && !isEqual(linkRoute.query, route.query)) {
return false
if (props.exactQuery === 'partial') {
if (!isPartiallyEqual(linkRoute.query, route.query)) return false
} else if (props.exactQuery === true) {
if (!isEqual(linkRoute.query, route.query)) return false
}
if (props.exactHash && linkRoute.hash !== route.hash) {
return false
}
Expand Down

0 comments on commit 7329900

Please sign in to comment.