Raw
1 "use client"
2
3 import React, { useEffect, useMemo } from "react"
4 import clsx from "clsx"
5 import {
6 getOsShortcut,
7 Kbd,
8 MainNav,
9 Tooltip,
10 useAiAssistant,
11 useIsBrowser,
12 useLayout,
13 useSidebar,
14 useSiteConfig,
15 } from ".."
16 import { ContentMenu } from "../components/ContentMenu"
17
18 export type MainContentLayoutProps = {
19 mainWrapperClasses?: string
20 contentClassName?: string
21 children: React.ReactNode
22 showContentMenu?: boolean
23 }
24
25 export const MainContentLayout = ({
26 children,
27 mainWrapperClasses,
28 contentClassName,
29 showContentMenu: initialShowContentMenu = true,
30 }: MainContentLayoutProps) => {
31 const { isBrowser } = useIsBrowser()
32 const { desktopSidebarOpen, setDesktopSidebarOpen } = useSidebar()
33 const { mainContentRef, showCollapsedNavbar } = useLayout()
34 const { frontmatter, isInProduct } = useSiteConfig()
35 const { chatOpened } = useAiAssistant()
36 const osShortcut = getOsShortcut()
37
38 useEffect(() => {
39 if (!isBrowser) {
40 return
41 }
42 const rootLayout = document.getElementById("root-layout")
43 if (desktopSidebarOpen) {
44 rootLayout?.classList.add("lg:grid-cols-[221px_1fr]")
45 } else {
46 rootLayout?.classList.remove("lg:grid-cols-[221px_1fr]")
47 }
48 }, [desktopSidebarOpen, isBrowser])
49
50 const showContentMenu = useMemo(() => {
51 return (
52 initialShowContentMenu &&
53 !frontmatter.hide_content_menu &&
54 !isInProduct &&
55 (!chatOpened || (chatOpened && !showCollapsedNavbar))
56 )
57 }, [
58 initialShowContentMenu,
59 frontmatter.hide_content_menu,
60 isInProduct,
61 chatOpened,
62 showCollapsedNavbar,
63 ])
64
65 const sidebarCollapser = (className?: string) => (
66 <Tooltip
67 render={() => (
68 <div className="flex gap-[6px] justify-center items-center">
69 <span className="text-compact-x-small-plus text-medusa-fg-base">
70 {desktopSidebarOpen ? "Hide sidebar" : "Show sidebar"}
71 </span>
72 <Kbd>{osShortcut}</Kbd>
73 <Kbd>\</Kbd>
74 </div>
75 )}
76 className={clsx(
77 "hidden lg:block absolute h-[20px] w-[3px] top-1/2 -translate-y-1/2 z-20",
78 className
79 )}
80 innerClassName="w-full h-full inline-block"
81 >
82 <button
83 className={clsx(
84 "rounded-[2px] bg-medusa-alphas-alpha-24 h-full w-full cursor-pointer",
85 "appearance-none p-0 border-none focus:outline-none active:outline-none"
86 )}
87 onClick={() => setDesktopSidebarOpen((prev) => !prev)}
88 />
89 </Tooltip>
90 )
91
92 return (
93 <div
94 className={clsx(
95 "relative max-w-full",
96 "h-full flex-1",
97 "flex flex-col",
98 "gap-docs_0.5 lg:py-docs_0.25 lg:mr-docs_0.25 scroll-m-docs_0.25",
99 !desktopSidebarOpen && "lg:ml-docs_0.25",
100 mainWrapperClasses
101 )}
102 >
103 <div
104 className={clsx(
105 "bg-medusa-bg-base",
106 "flex-col items-center",
107 "h-full w-full",
108 "overflow-y-scroll overflow-x-hidden",
109 "md:rounded-docs_DEFAULT",
110 "shadow-elevation-card-rest dark:shadow-elevation-card-rest-dark",
111 mainWrapperClasses
112 )}
113 id="main"
114 ref={mainContentRef}
115 >
116 <MainNav />
117 {/* Sidebar + content menu collapser */}
118 {sidebarCollapser("left-docs_0.25")}
119 <div
120 className={clsx(
121 "pb-docs_8 lg:pb-docs_4",
122 showContentMenu && "grid grid-cols-1 lg:mx-auto",
123 desktopSidebarOpen && "lg:grid-cols-[1fr_221px]",
124 chatOpened && showCollapsedNavbar && "pl-docs_1",
125 !isInProduct && "pt-docs_4 lg:pt-docs_6",
126 isInProduct && "pt-docs_2",
127 contentClassName
128 )}
129 id="content"
130 >
131 <div className="flex justify-center">{children}</div>
132 </div>
133 {/* Sidebar + content menu collapser */}
134 {showContentMenu && (
135 <>
136 {sidebarCollapser(
137 desktopSidebarOpen ? "right-[242px]" : "right-docs_0.25"
138 )}
139 {desktopSidebarOpen && <ContentMenu />}
140 </>
141 )}
142 </div>
143 </div>
144 )
145 }