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

refactor(sidebar): improve setOpen callback logic #5593

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 39 additions & 0 deletions apps/www/content/docs/components/sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,38 @@ You can change the keyboard shortcut by updating the `SIDEBAR_KEYBOARD_SHORTCUT`
const SIDEBAR_KEYBOARD_SHORTCUT = "b"
```

### Persisting State with SSR

The `SidebarProvider` supports persisting the sidebar state across page reloads and server-side rendering. It uses cookies to store the current state of the sidebar. When the sidebar state changes, a default cookie named `sidebar:state` is set with the current open/closed state. This cookie is then read on subsequent page loads to restore the sidebar state.

To persist sidebar state with SSR, set up your `SidebarProvider` in `app/layout.tsx` like this:

```tsx showLineNumbers title="app/layout.tsx"
import { cookies } from "next/headers"

import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"
import { AppSidebar } from "@/components/app-sidebar"

export async function Layout({ children }: { children: React.ReactNode }) {
const sidebarState = cookies().get("sidebar:state")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be any mention of Next 15 here?
cookies() needs to be awaited like so:

const cookieStore = await cookies();
const state = cookieStore.get('sidebar:state');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're correct. In Next.js 15, the cookies() function has become asynchronous, which is a breaking change. If you're not upgrading to Next.js 15, then you don't need to change anything.

should there be any mention of Next 15 here? cookies() needs to be awaited like so:

const cookieStore = await cookies();
const state = cookieStore.get('sidebar:state');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth putting a note on the docs about it


let defaultOpen = true
if (sidebarState) {
defaultOpen = sidebarState.value === "true"
}

return (
<SidebarProvider defaultOpen={defaultOpen}>
<AppSidebar />
<main>
<SidebarTrigger />
{children}
</main>
</SidebarProvider>
)
}
```

## Sidebar

The main `Sidebar` component used to render a collapsible sidebar.
Expand Down Expand Up @@ -1323,6 +1355,13 @@ You can find more tips on using states for styling in this [Twitter thread](http

## Changelog

### 2024-10-27 Refactor `<SidebarProvider>`

- Improved setOpen callback logic in `<SidebarProvider>`
- Simplify state handling.
- Ensure consistent cookie setting.
- Added ability for users to persist sidebar state via cookies.

### 2024-10-21

- [#5491](https://github.com/shadcn-ui/ui/pull/5491) - Moved `text-sidebar-foreground` from `<SidebarProvider>` to `<Sidebar>` component.
Expand Down
2 changes: 1 addition & 1 deletion apps/www/public/r/styles/default/sidebar.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/www/public/r/styles/new-york/sidebar.json

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions apps/www/registry/default/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,15 @@ const SidebarProvider = React.forwardRef<
const open = openProp ?? _open
const setOpen = React.useCallback(
(value: boolean | ((value: boolean) => boolean)) => {
const openState = typeof value === "function" ? value(open) : value
if (setOpenProp) {
return setOpenProp?.(
typeof value === "function" ? value(open) : value
)
setOpenProp(openState)
} else {
_setOpen(openState)
}

_setOpen(value)

// This sets the cookie to keep the sidebar state.
document.cookie = `${SIDEBAR_COOKIE_NAME}=${open}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
},
[setOpenProp, open]
)
Expand Down
11 changes: 5 additions & 6 deletions apps/www/registry/new-york/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,15 @@ const SidebarProvider = React.forwardRef<
const open = openProp ?? _open
const setOpen = React.useCallback(
(value: boolean | ((value: boolean) => boolean)) => {
const openState = typeof value === "function" ? value(open) : value
if (setOpenProp) {
return setOpenProp?.(
typeof value === "function" ? value(open) : value
)
setOpenProp(openState)
} else {
_setOpen(openState)
}

_setOpen(value)

// This sets the cookie to keep the sidebar state.
document.cookie = `${SIDEBAR_COOKIE_NAME}=${open}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
},
[setOpenProp, open]
)
Expand Down
Loading