|
1
|
import { PropsWithChildren, useEffect, useState } from "react" |
|
2
|
import { useLocation } from "react-router-dom" |
|
3
|
import { SidebarContext } from "./sidebar-context" |
|
4
|
|
|
5
|
export const SidebarProvider = ({ children }: PropsWithChildren) => { |
|
6
|
const [desktop, setDesktop] = useState(true) |
|
7
|
const [mobile, setMobile] = useState(false) |
|
8
|
|
|
9
|
const { pathname } = useLocation() |
|
10
|
|
|
11
|
const toggle = (view: "desktop" | "mobile") => { |
|
12
|
if (view === "desktop") { |
|
13
|
setDesktop(!desktop) |
|
14
|
} else { |
|
15
|
setMobile(!mobile) |
|
16
|
} |
|
17
|
} |
|
18
|
|
|
19
|
// close the mobile sidebar on route change |
|
20
|
// this is to prevent the sidebar from staying open |
|
21
|
// when navigating to a new page |
|
22
|
useEffect(() => { |
|
23
|
setMobile(false) |
|
24
|
}, [pathname]) |
|
25
|
|
|
26
|
return ( |
|
27
|
<SidebarContext.Provider value={{ desktop, mobile, toggle }}> |
|
28
|
{children} |
|
29
|
</SidebarContext.Provider> |
|
30
|
) |
|
31
|
} |