Raw
1 "use client"
2
3 import React, { Suspense, useMemo, useRef } from "react"
4 import { useSidebar } from "@/providers/Sidebar"
5 import clsx from "clsx"
6 import { Loading } from "@/components/Loading"
7 import { SidebarItem } from "./Item"
8 // @ts-expect-error can't install the types package because it doesn't support React v19
9 import { CSSTransition, SwitchTransition } from "react-transition-group"
10 import { SidebarTop } from "./Top"
11 import { useClickOutside } from "@/hooks/use-click-outside"
12 import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut"
13 import useResizeObserver from "@react-hook/resize-observer"
14 import { isSidebarItemLink } from "../../utils/sidebar-utils"
15
16 export type SidebarProps = {
17 className?: string
18 }
19
20 export const Sidebar = ({ className = "" }: SidebarProps) => {
21 const sidebarWrapperRef = useRef(null)
22 const sidebarTopRef = useRef<HTMLDivElement>(null)
23 const {
24 sidebars,
25 shownSidebar,
26 mobileSidebarOpen,
27 setMobileSidebarOpen,
28 isSidebarStatic,
29 sidebarRef,
30 desktopSidebarOpen,
31 setDesktopSidebarOpen,
32 setSidebarTopHeight,
33 sidebarHistory,
34 } = useSidebar()
35 useClickOutside({
36 elmRef: sidebarWrapperRef,
37 onClickOutside: () => {
38 if (mobileSidebarOpen) {
39 setMobileSidebarOpen(false)
40 }
41 },
42 })
43 useKeyboardShortcut({
44 metakey: true,
45 shortcutKeys: ["\\"],
46 action: () => {
47 setDesktopSidebarOpen((prev) => !prev)
48 },
49 })
50
51 useResizeObserver(sidebarTopRef as React.RefObject<HTMLElement>, () => {
52 setSidebarTopHeight(sidebarTopRef.current?.clientHeight || 0)
53 })
54
55 const sidebarItems = useMemo(() => {
56 return shownSidebar && "items" in shownSidebar
57 ? shownSidebar.items
58 : shownSidebar?.children || []
59 }, [shownSidebar])
60
61 return (
62 <>
63 {mobileSidebarOpen && (
64 <div
65 className={clsx(
66 "lg:hidden bg-medusa-bg-overlay opacity-70",
67 "fixed top-0 left-0 w-full h-full z-[45]"
68 )}
69 data-testid="mobile-sidebar-overlay"
70 ></div>
71 )}
72 <aside
73 className={clsx(
74 "bg-medusa-bg-base lg:bg-transparent block",
75 "fixed -left-full top-0 h-[calc(100%-16px)] transition-[left] lg:relative lg:h-auto",
76 "max-w-sidebar-xs sm:max-w-sidebar-sm md:max-w-sidebar-md lg:max-w-sidebar-lg",
77 "xl:max-w-sidebar-xl xxl:max-w-sidebar-xxl xxxl:max-w-sidebar-xxxl",
78 "w-sidebar-xs sm:w-auto",
79 mobileSidebarOpen && [
80 "!left-docs_0.5 !top-docs_0.5 z-50 shadow-elevation-modal dark:shadow-elevation-modal-dark",
81 "rounded",
82 "lg:!left-0 lg:!top-0 lg:shadow-none",
83 ],
84 desktopSidebarOpen && "lg:left-0",
85 !desktopSidebarOpen && "lg:!absolute lg:!-left-full",
86 className
87 )}
88 style={{
89 animationFillMode: "forwards",
90 }}
91 ref={sidebarWrapperRef}
92 >
93 <ul className={clsx("h-full w-full", "flex flex-col")}>
94 <SwitchTransition>
95 <CSSTransition
96 key={
97 sidebarHistory.length
98 ? sidebarHistory[sidebarHistory.length - 1]
99 : sidebars[0].sidebar_id
100 }
101 nodeRef={sidebarRef}
102 classNames={{
103 enter: "animate-fadeInLeft animate-fast",
104 exit: "animate-fadeOutLeft animate-fast",
105 }}
106 timeout={200}
107 >
108 <div
109 className={clsx(
110 "overflow-y-scroll clip",
111 "pb-docs_0.75 flex-1 max-h-screen"
112 )}
113 ref={sidebarRef}
114 id="sidebar"
115 >
116 <SidebarTop ref={sidebarTopRef} />
117 <div className="pt-docs_0.75">
118 {!sidebarItems.length && !isSidebarStatic && (
119 <Loading className="px-docs_0.75" />
120 )}
121 {sidebarItems.map((item, index) => {
122 const itemKey =
123 item.type === "separator"
124 ? index
125 : isSidebarItemLink(item)
126 ? `${item.path}-${index}`
127 : `${item.title}-${index}`
128 return (
129 <Suspense
130 fallback={
131 <Loading
132 count={1}
133 className="!mb-0 !px-docs_0.5"
134 barClassName="h-[20px]"
135 />
136 }
137 key={itemKey}
138 >
139 <SidebarItem
140 item={item}
141 hasNextItems={index !== sidebarItems.length - 1}
142 />
143 </Suspense>
144 )
145 })}
146 </div>
147 </div>
148 </CSSTransition>
149 </SwitchTransition>
150 </ul>
151 </aside>
152 </>
153 )
154 }