From b2bd4ae3bb09768a14b91fa440e22cb140e49cf7 Mon Sep 17 00:00:00 2001 From: Matin Soleymani Date: Mon, 11 Nov 2024 19:15:18 +0330 Subject: [PATCH] change: sidebar:state save in localStorage --- .../src/lib/registry/default/ui/sidebar/SidebarProvider.vue | 6 +++--- apps/www/src/lib/registry/default/ui/sidebar/utils.ts | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/www/src/lib/registry/default/ui/sidebar/SidebarProvider.vue b/apps/www/src/lib/registry/default/ui/sidebar/SidebarProvider.vue index 8f06c67b2..233910d22 100644 --- a/apps/www/src/lib/registry/default/ui/sidebar/SidebarProvider.vue +++ b/apps/www/src/lib/registry/default/ui/sidebar/SidebarProvider.vue @@ -3,7 +3,7 @@ import { cn } from '@/lib/utils' import { useEventListener, useVModel } from '@vueuse/core' import { TooltipProvider } from 'radix-vue' import { computed, type HTMLAttributes, type Ref, ref } from 'vue' -import { provideSidebarContext, SIDEBAR_COOKIE_MAX_AGE, SIDEBAR_COOKIE_NAME, SIDEBAR_KEYBOARD_SHORTCUT, SIDEBAR_WIDTH, SIDEBAR_WIDTH_ICON } from './utils' +import { provideSidebarContext, SIDEBAR_STORAGE_KEY, SIDEBAR_KEYBOARD_SHORTCUT, SIDEBAR_WIDTH, SIDEBAR_WIDTH_ICON } from './utils' const props = withDefaults(defineProps<{ defaultOpen?: boolean @@ -29,8 +29,8 @@ const open = useVModel(props, 'open', emits, { function setOpen(value: boolean) { open.value = value // emits('update:open', value) - // This sets the cookie to keep the sidebar state. - document.cookie = `${SIDEBAR_COOKIE_NAME}=${open.value}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}` + // This sets the key in localStorage to keep the sidebar state. + localStorage.setItem(SIDEBAR_STORAGE_KEY , JSON.stringify(open.value)); } function setOpenMobile(value: boolean) { diff --git a/apps/www/src/lib/registry/default/ui/sidebar/utils.ts b/apps/www/src/lib/registry/default/ui/sidebar/utils.ts index 3ded66c4e..3d3a4615f 100644 --- a/apps/www/src/lib/registry/default/ui/sidebar/utils.ts +++ b/apps/www/src/lib/registry/default/ui/sidebar/utils.ts @@ -1,8 +1,7 @@ import type { ComputedRef, Ref } from 'vue' import { createContext } from 'radix-vue' -export const SIDEBAR_COOKIE_NAME = 'sidebar:state' -export const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7 +export const SIDEBAR_STORAGE_KEY = 'sidebar:state' export const SIDEBAR_WIDTH = '16rem' export const SIDEBAR_WIDTH_MOBILE = '18rem' export const SIDEBAR_WIDTH_ICON = '3rem'