feat(web): add sidebar collapse

This commit is contained in:
Acbox
2026-04-14 21:58:29 +08:00
parent 19d5cd606d
commit 5382c3cd27
4 changed files with 36 additions and 45 deletions
+4 -34
View File
@@ -1,11 +1,7 @@
<script setup lang="ts">
import type { SidebarProps } from '.'
import { cn } from '#/lib/utils'
import { Sheet, SheetContent } from '#/components/sheet'
import SheetDescription from '#/components/sheet/SheetDescription.vue'
import SheetHeader from '#/components/sheet/SheetHeader.vue'
import SheetTitle from '#/components/sheet/SheetTitle.vue'
import { SIDEBAR_WIDTH, SIDEBAR_WIDTH_MOBILE, useSidebar } from './utils'
import { SIDEBAR_WIDTH, useSidebar } from './utils'
defineOptions({
inheritAttrs: false,
@@ -17,7 +13,7 @@ const props = withDefaults(defineProps<SidebarProps>(), {
collapsible: 'offcanvas',
})
const { isMobile, state, openMobile, setOpenMobile } = useSidebar()
const { state } = useSidebar()
</script>
<template>
@@ -30,35 +26,9 @@ const { isMobile, state, openMobile, setOpenMobile } = useSidebar()
<slot />
</div>
<Sheet
v-else-if="isMobile"
:open="openMobile"
v-bind="$attrs"
@update:open="setOpenMobile"
>
<SheetContent
data-sidebar="sidebar"
data-slot="sidebar"
data-mobile="true"
:side="side"
class="bg-sidebar text-sidebar-foreground w-(--sidebar-width) p-0 [&>button]:hidden"
:style="{
'--sidebar-width': SIDEBAR_WIDTH_MOBILE,
}"
>
<SheetHeader class="sr-only">
<SheetTitle>Sidebar</SheetTitle>
<SheetDescription>Displays the mobile sidebar.</SheetDescription>
</SheetHeader>
<div class="flex h-full w-full flex-col">
<slot />
</div>
</SheetContent>
</Sheet>
<div
v-else
class="group peer text-sidebar-foreground hidden md:block"
class="group peer text-sidebar-foreground block"
data-slot="sidebar"
:data-state="state"
:data-collapsible="state === 'collapsed' ? collapsible : ''"
@@ -79,7 +49,7 @@ const { isMobile, state, openMobile, setOpenMobile } = useSidebar()
/>
<div
:class="cn(
'fixed inset-y-0 z-10 hidden h-svh w-(--sidebar-width) transition-[left,right,width] duration-200 ease-linear md:flex',
'fixed inset-y-0 z-10 flex h-svh w-(--sidebar-width) transition-[left,right,width] duration-200 ease-linear',
side === 'left'
? 'left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]'
: 'right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]',
@@ -40,7 +40,7 @@ function setOpenMobile(value: boolean) {
// Helper to toggle the sidebar.
function toggleSidebar() {
return isMobile.value ? setOpenMobile(!openMobile.value) : setOpen(!open.value)
return setOpen(!open.value)
}
useEventListener('keydown', (event: KeyboardEvent) => {