Skip to content

Commit b8d4ef5

Browse files
committed
fix: add mobile sidebar state
1 parent 61dffb5 commit b8d4ef5

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/components/ui/sidebar.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ const SIDEBAR_KEYBOARD_SHORTCUT = "b"
3535
type SidebarContextProps = {
3636
state: "expanded" | "collapsed"
3737
open: boolean
38-
setOpen: (open: boolean) => void
38+
setOpen: React.Dispatch<React.SetStateAction<boolean>>
3939
openMobile: boolean
40-
setOpenMobile: (open: boolean) => void
40+
setOpenMobile: React.Dispatch<React.SetStateAction<boolean>>
4141
isMobile: boolean
4242
toggleSidebar: () => void
4343
}
@@ -57,6 +57,8 @@ function SidebarProvider({
5757
defaultOpen = true,
5858
open: openProp,
5959
onOpenChange: setOpenProp,
60+
openMobile: openMobileProp,
61+
setOpenMobile: setOpenMobileProp,
6062
className,
6163
style,
6264
children,
@@ -67,11 +69,26 @@ function SidebarProvider({
6769
defaultOpen?: boolean
6870
open?: boolean
6971
onOpenChange?: (open: boolean) => void
72+
openMobile?: boolean
73+
setOpenMobile?: (open: boolean) => void
7074
shortcut?: string
7175
noLayout?: boolean
7276
}) {
7377
const isMobile = useIsMobile()
74-
const [openMobile, setOpenMobile] = React.useState(false)
78+
const [_openMobile, _setOpenMobile] = React.useState(false)
79+
80+
const openMobile = openMobileProp ?? _openMobile
81+
const setOpenMobile = React.useCallback(
82+
(value: boolean | ((value: boolean) => boolean)) => {
83+
const mobileOpenState = typeof value === "function" ? value(openMobile) : value
84+
if (setOpenMobileProp) {
85+
setOpenMobileProp(mobileOpenState)
86+
} else {
87+
_setOpenMobile(mobileOpenState)
88+
}
89+
},
90+
[setOpenMobileProp, openMobile]
91+
)
7592

7693
// This is the internal state of the sidebar.
7794
// We use openProp and setOpenProp for control from outside the component.

src/layouts/app-layout/AppLayout.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function AppLayout({
2727
<SidebarProvider
2828
open={sidebarOpen}
2929
onOpenChange={sidebarOpenChange}
30-
// openMobile={sidebarOpenMobile}
31-
// setOpenMobile={setSidebarOpenMobile}
30+
openMobile={sidebarOpenMobile}
31+
setOpenMobile={setSidebarOpenMobile}
3232
className="min-h-screen"
3333
>
3434
<SidebarLeft />
@@ -39,12 +39,10 @@ export function AppLayout({
3939
<SidebarProvider
4040
open={rightPanelOpen}
4141
onOpenChange={rightPanelOpenChange}
42-
// noLayout
4342
className='w-auto'
4443
shortcut='p'
45-
// openMobile={rightPanelOpenMobile}
46-
// setOpenMobile={setRightPanelOpenMobile}
47-
// noLayout
44+
openMobile={rightPanelOpenMobile}
45+
setOpenMobile={setRightPanelOpenMobile}
4846
>
4947
<SidebarRight />
5048
</SidebarProvider>

0 commit comments

Comments
 (0)