Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shadcn built-in check for sidebar state #82

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function share(Request $request): array
...(new Ziggy)->toArray(),
'location' => $request->url(),
],
'sidebarOpen' => $request->cookie('sidebar_state') === 'true',
];
}
}
2 changes: 1 addition & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->encryptCookies(except: ['appearance']);
$middleware->encryptCookies(except: ['appearance', 'sidebar_state']);

$middleware->web(append: [
HandleAppearance::class,
Expand Down
16 changes: 4 additions & 12 deletions resources/js/components/AppShell.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
<script setup lang="ts">
import { SidebarProvider } from '@/components/ui/sidebar';
import { onMounted, ref } from 'vue';
import { usePage } from '@inertiajs/vue3';
import { SharedData } from '@/types';

interface Props {
variant?: 'header' | 'sidebar';
}

defineProps<Props>();

const isOpen = ref(true);

onMounted(() => {
isOpen.value = localStorage.getItem('sidebar') !== 'false';
});

const handleSidebarChange = (open: boolean) => {
isOpen.value = open;
localStorage.setItem('sidebar', String(open));
};
const isOpen = usePage<SharedData>().props.sidebarOpen;
</script>

<template>
<div v-if="variant === 'header'" class="flex min-h-screen w-full flex-col">
<slot />
</div>
<SidebarProvider v-else :default-open="isOpen" :open="isOpen" @update:open="handleSidebarChange">
<SidebarProvider v-else :default-open="isOpen">
<slot />
</SidebarProvider>
</template>
2 changes: 1 addition & 1 deletion resources/js/components/ui/sidebar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext } from 'radix-vue';
import type { ComputedRef, Ref } from 'vue';

export const SIDEBAR_COOKIE_NAME = 'sidebar:state';
export const SIDEBAR_COOKIE_NAME = 'sidebar_state';
export const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7;
export const SIDEBAR_WIDTH = '16rem';
export const SIDEBAR_WIDTH_MOBILE = '18rem';
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface SharedData extends PageProps {
quote: { message: string; author: string };
auth: Auth;
ziggy: Config & { location: string };
sidebarOpen: boolean;
}

export interface User {
Expand Down