feat(theme): expose `useSidebar` (#2496)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/2554/head
烽宁 1 year ago committed by GitHub
parent 88bbd23102
commit c4909e4298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,17 +1,26 @@
import { dirname, resolve } from 'path' import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { defineConfig } from 'vitest/config' import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
const dir = dirname(fileURLToPath(import.meta.url)) const dir = dirname(fileURLToPath(import.meta.url))
export default defineConfig({ export default defineConfig({
plugins: [vue()],
resolve: { resolve: {
alias: { alias: [
'@siteData': resolve(dir, './shims.ts'), { find: '@siteData', replacement: resolve(dir, './shims.ts') },
client: resolve(dir, '../../src/client'), { find: 'client', replacement: resolve(dir, '../../src/client') },
node: resolve(dir, '../../src/node'), { find: 'node', replacement: resolve(dir, '../../src/node') },
vitepress: resolve(dir, '../../src/client') {
} find: /^vitepress$/,
replacement: resolve(dir, '../../src/client/index.js')
},
{
find: /^vitepress\/theme$/,
replacement: resolve(dir, '../../src/client/theme-default/index.js')
}
]
}, },
test: { test: {
globals: true globals: true

@ -180,3 +180,36 @@ export default {
} }
} }
``` ```
## `useSidebar` <Badge type="info" text="composable" />
Returns sidebar-related data. The returned object has the following type:
```ts
export interface Sidebar {
isOpen: Ref<boolean>
sidebar: ComputedRef<DefaultTheme.SidebarItem[]>
sidebarGroups: ComputedRef<DefaultTheme.SidebarItem[]>
hasSidebar: ComputedRef<boolean>
hasAside: ComputedRef<boolean>
leftAside: ComputedRef<boolean>
isSidebarEnabled: ComputedRef<boolean>
open: () => void
close: () => void
toggle: () => void
}
```
**Example:**
```vue
<script setup>
import { useSidebar } from 'vitepress/theme'
const { hasSidebar } = useSidebar()
</script>
<template>
<div v-if="hasSidebar">Only show when sidebar exists</div>
</template>
```

@ -1,15 +1,16 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, provide, useSlots, watch } from 'vue'
import { useRoute } from 'vitepress' import { useRoute } from 'vitepress'
import { useData } from './composables/data' import { useSidebar } from 'vitepress/theme'
import { useSidebar, useCloseSidebarOnEscape } from './composables/sidebar' import { computed, provide, useSlots, watch } from 'vue'
import VPSkipLink from './components/VPSkipLink.vue'
import VPBackdrop from './components/VPBackdrop.vue' import VPBackdrop from './components/VPBackdrop.vue'
import VPNav from './components/VPNav.vue'
import VPLocalNav from './components/VPLocalNav.vue'
import VPSidebar from './components/VPSidebar.vue'
import VPContent from './components/VPContent.vue' import VPContent from './components/VPContent.vue'
import VPFooter from './components/VPFooter.vue' import VPFooter from './components/VPFooter.vue'
import VPLocalNav from './components/VPLocalNav.vue'
import VPNav from './components/VPNav.vue'
import VPSidebar from './components/VPSidebar.vue'
import VPSkipLink from './components/VPSkipLink.vue'
import { useData } from './composables/data'
import { useCloseSidebarOnEscape } from './composables/sidebar'
const { const {
isOpen: isSidebarOpen, isOpen: isSidebarOpen,

@ -1,10 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { useSidebar } from 'vitepress/theme'
import NotFound from '../NotFound.vue'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { useSidebar } from '../composables/sidebar'
import VPPage from './VPPage.vue'
import VPHome from './VPHome.vue'
import VPDoc from './VPDoc.vue' import VPDoc from './VPDoc.vue'
import NotFound from '../NotFound.vue' import VPHome from './VPHome.vue'
import VPPage from './VPPage.vue'
const { page, frontmatter } = useData() const { page, frontmatter } = useData()
const { hasSidebar } = useSidebar() const { hasSidebar } = useSidebar()

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { useRoute } from 'vitepress' import { useRoute } from 'vitepress'
import { useSidebar } from 'vitepress/theme'
import { computed } from 'vue' import { computed } from 'vue'
import { useSidebar } from '../composables/sidebar'
import VPDocAside from './VPDocAside.vue' import VPDocAside from './VPDocAside.vue'
import VPDocFooter from './VPDocFooter.vue' import VPDocFooter from './VPDocFooter.vue'
import VPDocOutlineDropdown from './VPDocOutlineDropdown.vue' import VPDocOutlineDropdown from './VPDocOutlineDropdown.vue'

@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { useSidebar } from 'vitepress/theme'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { useSidebar } from '../composables/sidebar'
const { theme } = useData() const { theme } = useData()
const { hasSidebar } = useSidebar() const { hasSidebar } = useSidebar()

@ -1,10 +1,10 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useWindowScroll } from '@vueuse/core' import { useWindowScroll } from '@vueuse/core'
import { computed, shallowRef } from 'vue'
import { onContentUpdated } from 'vitepress' import { onContentUpdated } from 'vitepress'
import { useSidebar } from 'vitepress/theme'
import { computed, shallowRef } from 'vue'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { getHeaders, type MenuItem } from '../composables/outline' import { getHeaders, type MenuItem } from '../composables/outline'
import { useSidebar } from '../composables/sidebar'
import VPLocalNavOutlineDropdown from './VPLocalNavOutlineDropdown.vue' import VPLocalNavOutlineDropdown from './VPLocalNavOutlineDropdown.vue'
import VPIconAlignLeft from './icons/VPIconAlignLeft.vue' import VPIconAlignLeft from './icons/VPIconAlignLeft.vue'

@ -1,15 +1,15 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed } from 'vue'
import { useWindowScroll } from '@vueuse/core' import { useWindowScroll } from '@vueuse/core'
import { useSidebar } from '../composables/sidebar' import { useSidebar } from 'vitepress/theme'
import VPNavBarTitle from './VPNavBarTitle.vue' import { computed } from 'vue'
import VPNavBarSearch from './VPNavBarSearch.vue'
import VPNavBarMenu from './VPNavBarMenu.vue'
import VPNavBarTranslations from './VPNavBarTranslations.vue'
import VPNavBarAppearance from './VPNavBarAppearance.vue' import VPNavBarAppearance from './VPNavBarAppearance.vue'
import VPNavBarSocialLinks from './VPNavBarSocialLinks.vue'
import VPNavBarExtra from './VPNavBarExtra.vue' import VPNavBarExtra from './VPNavBarExtra.vue'
import VPNavBarHamburger from './VPNavBarHamburger.vue' import VPNavBarHamburger from './VPNavBarHamburger.vue'
import VPNavBarMenu from './VPNavBarMenu.vue'
import VPNavBarSearch from './VPNavBarSearch.vue'
import VPNavBarSocialLinks from './VPNavBarSocialLinks.vue'
import VPNavBarTitle from './VPNavBarTitle.vue'
import VPNavBarTranslations from './VPNavBarTranslations.vue'
defineProps<{ defineProps<{
isScreenOpen: boolean isScreenOpen: boolean

@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { useSidebar } from 'vitepress/theme'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { useSidebar } from '../composables/sidebar'
import { useLangs } from '../composables/langs' import { useLangs } from '../composables/langs'
import { normalizeLink } from '../support/utils' import { normalizeLink } from '../support/utils'
import VPImage from './VPImage.vue' import VPImage from './VPImage.vue'

@ -1,7 +1,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { clearAllBodyScrollLocks, disableBodyScroll } from 'body-scroll-lock'
import { useSidebar } from 'vitepress/theme'
import { ref, watchPostEffect } from 'vue' import { ref, watchPostEffect } from 'vue'
import { disableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock'
import { useSidebar } from '../composables/sidebar'
import VPSidebarItem from './VPSidebarItem.vue' import VPSidebarItem from './VPSidebarItem.vue'
const { sidebarGroups, hasSidebar } = useSidebar() const { sidebarGroups, hasSidebar } = useSidebar()

@ -1,6 +1,6 @@
import { computed } from 'vue'
import { useMediaQuery } from '@vueuse/core' import { useMediaQuery } from '@vueuse/core'
import { useSidebar } from './sidebar' import { useSidebar } from 'vitepress/theme'
import { computed } from 'vue'
export function useAside() { export function useAside() {
const { hasSidebar } = useSidebar() const { hasSidebar } = useSidebar()

@ -23,6 +23,8 @@ export { default as VPTeamPageTitle } from './components/VPTeamPageTitle.vue'
export { default as VPTeamPageSection } from './components/VPTeamPageSection.vue' export { default as VPTeamPageSection } from './components/VPTeamPageSection.vue'
export { default as VPTeamMembers } from './components/VPTeamMembers.vue' export { default as VPTeamMembers } from './components/VPTeamMembers.vue'
export { useSidebar } from './composables/sidebar'
const theme: Theme = { const theme: Theme = {
Layout, Layout,
enhanceApp: ({ app }) => { enhanceApp: ({ app }) => {

3
theme.d.ts vendored

@ -14,9 +14,10 @@ export const VPTeamMembers: DefineComponent
declare const theme: { declare const theme: {
Layout: DefineComponent Layout: DefineComponent
NotFound: DefineComponent
enhanceApp: (ctx: EnhanceAppContext) => void enhanceApp: (ctx: EnhanceAppContext) => void
} }
export default theme export default theme
export type { DefaultTheme } from './types/default-theme.js' export type { DefaultTheme } from './types/default-theme.js'
export const useSidebar: () => DefaultTheme.SideBar

@ -209,6 +209,22 @@ export namespace DefaultTheme {
collapsed?: boolean collapsed?: boolean
} }
/**
* ReturnType of `useSidebar`
*/
export interface Sidebar {
isOpen: Ref<boolean>
sidebar: ComputedRef<SidebarItem[]>
sidebarGroups: ComputedRef<SidebarItem[]>
hasSidebar: ComputedRef<boolean>
hasAside: ComputedRef<boolean>
leftAside: ComputedRef<boolean>
isSidebarEnabled: ComputedRef<boolean>
open: () => void
close: () => void
toggle: () => void
}
// edit link ----------------------------------------------------------------- // edit link -----------------------------------------------------------------
export interface EditLink { export interface EditLink {

Loading…
Cancel
Save