-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix sidebar behaving weirdly (#48)
- Loading branch information
1 parent
8ef069a
commit 4a65bb2
Showing
4 changed files
with
49 additions
and
95 deletions.
There are no files selected for viewing
38 changes: 11 additions & 27 deletions
38
src/.vuepress/theme/client/composables/sidebar/useSidebar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,48 @@ | ||
import { ComputedRef, Ref, computed, ref, watch } from 'vue'; | ||
import { useBreakpoints, useNavbarConfig } from '@mptheme/client/composables'; | ||
import { MyPaResolvedSidebarItem } from '@mptheme/config.types'; | ||
import { createGlobalState } from '@vueuse/core'; | ||
import { useRoute } from 'vue-router'; | ||
import { useSidebarItems } from './useSidebarItems'; | ||
|
||
let isOpen: ComputedRef<boolean>; | ||
|
||
let toggled: Ref<boolean>; | ||
|
||
type UseSidebar = () => { | ||
exists: ComputedRef<boolean>; | ||
isNavbar: ComputedRef<boolean>; | ||
isOpen: Ref<boolean>; | ||
items: ComputedRef<MyPaResolvedSidebarItem[]>; | ||
toggle: (bool?: boolean) => void; | ||
toggled: Ref<boolean>; | ||
}; | ||
|
||
const useSidebarToggled = createGlobalState(() => ref(false)); | ||
|
||
export const useSidebar: UseSidebar = () => { | ||
const toggled = useSidebarToggled(); | ||
const route = useRoute(); | ||
const items = useSidebarItems(); | ||
const navbar = useNavbarConfig(); | ||
const { lg } = useBreakpoints(); | ||
const isNavbar = computed(() => !items.value.length); | ||
|
||
toggled ??= ref(false); | ||
|
||
isOpen ??= computed(() => { | ||
const renderByView = lg.value && !isNavbar.value; | ||
|
||
return exists.value && (renderByView || toggled.value); | ||
}); | ||
const isNavbar = computed(() => !items.value.length); | ||
|
||
const sidebarItems = computed(() => { | ||
if (!items.value.length) { | ||
return navbar.value; | ||
} | ||
|
||
return items.value; | ||
}); | ||
|
||
const exists = computed(() => { | ||
return (sidebarItems.value?.length ?? 0) > 0; | ||
return items.value.length ? items.value : navbar.value; | ||
}); | ||
|
||
const exists = computed(() => (sidebarItems.value?.length ?? 0) > 0); | ||
const toggle = (bool?: boolean): void => { | ||
toggled.value = bool ?? !toggled.value; | ||
}; | ||
|
||
watch( | ||
() => route.path, | ||
[() => route.path, lg], | ||
() => toggle(false), | ||
); | ||
|
||
return { | ||
items: sidebarItems, | ||
isOpen, | ||
isNavbar, | ||
exists, | ||
toggled, | ||
isNavbar, | ||
items: sidebarItems, | ||
toggle, | ||
toggled, | ||
}; | ||
}; |
74 changes: 23 additions & 51 deletions
74
src/.vuepress/theme/client/views/layout/header/toggle-sidebar-button/ToggleSidebarButton.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,46 @@ | ||
<template> | ||
<div | ||
v-if="hasSidebar" | ||
v-if="exists" | ||
class="cursor-pointer flex lg:hidden p-3" | ||
:title="translate('toggleSidebar')" | ||
:aria-label="translate('toggleSidebar')" | ||
aria-expanded="false" | ||
role="button" | ||
tabindex="0" | ||
@click="toggle"> | ||
@click="() => toggle()"> | ||
<div | ||
role="none" | ||
class="flex flex-col h-6 items-center justify-around w-6" | ||
aria-hidden="true" | ||
tabindex="-1"> | ||
<span | ||
:class="[ | ||
burgerClasses, | ||
{ | ||
'-rotate-45 translate-y-2': toggled, | ||
}, | ||
]" /> | ||
role="none" | ||
:class="[burgerClasses, { '-rotate-45 translate-y-2': toggled }]" /> | ||
<span | ||
:class="[ | ||
burgerClasses, | ||
{ | ||
'scale-x-0': toggled, | ||
}, | ||
]" /> | ||
role="none" | ||
:class="[burgerClasses, { 'scale-x-0': toggled }]" /> | ||
<span | ||
:class="[ | ||
burgerClasses, | ||
{ | ||
'rotate-45 -translate-y-2': toggled, | ||
}, | ||
]" /> | ||
role="none" | ||
:class="[burgerClasses, { 'rotate-45 -translate-y-2': toggled }]" /> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { useSidebar, useSiteLocaleData, useTranslate } from '@mptheme/client/composables'; | ||
import { defineComponent } from 'vue'; | ||
<script setup lang="ts"> | ||
import { useSidebar, useTranslate } from '../../../../composables'; | ||
export default defineComponent({ | ||
name: 'ToggleSidebarButton', | ||
emits: ['toggle'], | ||
setup: (props, ctx) => { | ||
const sidebar = useSidebar(); | ||
const translate = useTranslate(); | ||
const { toggled, toggle, exists } = useSidebar(); | ||
return { | ||
translate, | ||
toggled: sidebar.isOpen, | ||
hasSidebar: sidebar.exists, | ||
const translate = useTranslate(); | ||
toggle() { | ||
sidebar.toggle(); | ||
ctx.emit('toggle'); | ||
}, | ||
burgerClasses: [ | ||
'bg-black', | ||
'dark:bg-white', | ||
'duration-100', | ||
'h-0.5', | ||
'inline-block', | ||
'rounded-lg', | ||
'transition-transform', | ||
'w-full', | ||
], | ||
}; | ||
}, | ||
}); | ||
const burgerClasses = [ | ||
'bg-black', | ||
'dark:bg-white', | ||
'duration-100', | ||
'h-0.5', | ||
'inline-block', | ||
'rounded-lg', | ||
'transition-transform', | ||
'w-full', | ||
]; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters