Skip to content

Commit 0ca25ca

Browse files
Sepushbrc-dd
andauthored
refactor(nav,layout): use InjectionKey (#4880)
--------- Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent af95bcc commit 0ca25ca

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

src/client/theme-default/Layout.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import VPNav from './components/VPNav.vue'
88
import VPSidebar from './components/VPSidebar.vue'
99
import VPSkipLink from './components/VPSkipLink.vue'
1010
import { useData } from './composables/data'
11-
import { registerWatchers } from './composables/layout'
11+
import { layoutInfoInjectionKey, registerWatchers } from './composables/layout'
1212
import { useSidebarControl } from './composables/sidebar'
1313
1414
const {
@@ -24,7 +24,7 @@ const { frontmatter } = useData()
2424
const slots = useSlots()
2525
const heroImageSlotExists = computed(() => !!slots['home-hero-image'])
2626
27-
provide('hero-image-slot-exists', heroImageSlotExists)
27+
provide(layoutInfoInjectionKey, { heroImageSlotExists })
2828
</script>
2929

3030
<template>

src/client/theme-default/components/VPHero.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
2-
import { type Ref, inject } from 'vue'
32
import type { DefaultTheme } from 'vitepress/theme'
3+
import { inject } from 'vue'
4+
import { layoutInfoInjectionKey } from '../composables/layout'
45
import VPButton from './VPButton.vue'
56
import VPImage from './VPImage.vue'
67
@@ -20,7 +21,7 @@ defineProps<{
2021
actions?: HeroAction[]
2122
}>()
2223
23-
const heroImageSlotExists = inject('hero-image-slot-exists') as Ref<boolean>
24+
const { heroImageSlotExists } = inject(layoutInfoInjectionKey)!
2425
</script>
2526

2627
<template>

src/client/theme-default/components/VPNav.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { inBrowser } from 'vitepress'
33
import { computed, provide, watchEffect } from 'vue'
44
import { useData } from '../composables/data'
5-
import { useNav } from '../composables/nav'
5+
import { navInjectionKey, useNav } from '../composables/nav'
66
import VPNavBar from './VPNavBar.vue'
77
import VPNavScreen from './VPNavScreen.vue'
88
@@ -13,7 +13,7 @@ const hasNavbar = computed(() => {
1313
return frontmatter.value.navbar !== false
1414
})
1515
16-
provide('close-screen', closeScreen)
16+
provide(navInjectionKey, { closeScreen })
1717
1818
watchEffect(() => {
1919
if (inBrowser) {

src/client/theme-default/components/VPNavScreenMenuGroupLink.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<script lang="ts" setup>
22
import type { DefaultTheme } from 'vitepress/theme'
33
import { inject } from 'vue'
4+
import { navInjectionKey } from '../composables/nav'
45
import VPLink from './VPLink.vue'
56
67
defineProps<{
78
item: DefaultTheme.NavItemWithLink
89
}>()
910
10-
const closeScreen = inject('close-screen') as () => void
11+
const { closeScreen } = inject(navInjectionKey)!
1112
</script>
1213

1314
<template>

src/client/theme-default/components/VPNavScreenMenuLink.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<script lang="ts" setup>
22
import type { DefaultTheme } from 'vitepress/theme'
33
import { inject } from 'vue'
4+
import { navInjectionKey } from '../composables/nav'
45
import VPLink from './VPLink.vue'
56
67
defineProps<{
78
item: DefaultTheme.NavItemWithLink
89
}>()
910
10-
const closeScreen = inject('close-screen') as () => void
11+
const { closeScreen } = inject(navInjectionKey)!
1112
</script>
1213

1314
<template>

src/client/theme-default/composables/layout.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { inBrowser, onContentUpdated, useRoute } from 'vitepress'
22
import type { DefaultTheme, useLayout as expected } from 'vitepress/theme'
3-
import { computed, shallowReadonly, shallowRef, watch } from 'vue'
3+
import {
4+
computed,
5+
shallowReadonly,
6+
shallowRef,
7+
watch,
8+
type ComputedRef,
9+
type InjectionKey
10+
} from 'vue'
411
import { getSidebar, getSidebarGroups } from '../support/sidebar'
512
import { useData } from './data'
613
import { getHeaders } from './outline'
@@ -102,3 +109,10 @@ export function registerWatchers({ closeSidebar }: RegisterWatchersOptions) {
102109

103110
useCloseSidebarOnEscape(closeSidebar)
104111
}
112+
113+
export interface LayoutInfo {
114+
heroImageSlotExists: ComputedRef<boolean>
115+
}
116+
117+
export const layoutInfoInjectionKey: InjectionKey<LayoutInfo> =
118+
Symbol('layout-info')

src/client/theme-default/composables/nav.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useRoute } from 'vitepress'
2-
import { ref, watch } from 'vue'
2+
import { ref, watch, type InjectionKey } from 'vue'
33

44
export function useNav() {
55
const isScreenOpen = ref(false)
@@ -35,3 +35,9 @@ export function useNav() {
3535
toggleScreen
3636
}
3737
}
38+
39+
export interface NavExposedMethods {
40+
closeScreen: () => void
41+
}
42+
43+
export const navInjectionKey: InjectionKey<NavExposedMethods> = Symbol('nav')

0 commit comments

Comments
 (0)