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

change: sidebar:state save in localStorage #889

Open
wants to merge 1 commit into
base: dev
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions apps/www/src/lib/registry/default/ui/sidebar/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down