Skip to content

Commit

Permalink
feat(theme): add active status to nav menu group (#820)
Browse files Browse the repository at this point in the history
Co-authored-by: Kia King Ishii <[email protected]>
  • Loading branch information
casyalex and kiaking authored Jun 21, 2022
1 parent 47f1df5 commit fdb5720
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/config/theme-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type NavItemWithLink = {
interface NavItemWithChildren {
text?: string
items: NavItemWithLink[]
activeMatch?: string
}
```

Expand Down
8 changes: 8 additions & 0 deletions src/client/theme-default/components/VPFlyout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ function onBlur() {
fill: var(--vp-c-text-2);
}
.VPFlyout.active .text {
color: var(--vp-c-brand);
}
.VPFlyout.active:hover .text {
color: var(--vp-c-brand-dark);
}
.VPFlyout:hover .menu,
.button[aria-expanded="true"] + .menu {
opacity: 1;
Expand Down
17 changes: 16 additions & 1 deletion src/client/theme-default/components/VPNavBarMenuGroup.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
<script lang="ts" setup>
import { useData } from 'vitepress'
import type { DefaultTheme } from 'vitepress/theme'
import { isActive } from '../support/utils'
import VPFlyout from './VPFlyout.vue'
defineProps<{
item: DefaultTheme.NavItemWithChildren
}>()
const { page } = useData()
</script>

<template>
<VPFlyout :button="item.text" :items="item.items" />
<VPFlyout
:class="{
VPNavBarMenuGroup: true,
active: isActive(
page.relativePath,
item.activeMatch,
!!item.activeMatch
)
}"
:button="item.text"
:items="item.items"
/>
</template>
6 changes: 6 additions & 0 deletions types/default-theme.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export namespace DefaultTheme {
export interface NavItemWithChildren {
text?: string
items: (NavItemChildren | NavItemWithLink)[]

/**
* `activeMatch` is expected to be a regex string. We can't use actual
* RegExp object here because it isn't serializable
*/
activeMatch?: string
}

// image -----------------------------------------------------------------------
Expand Down

0 comments on commit fdb5720

Please sign in to comment.