Skip to content

Commit af23177

Browse files
committed
fix mobile sidebar bugs
1 parent a634c9e commit af23177

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

src/layouts/SidebarLayout.res

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,64 @@ module Sidebar = {
151151
}
152152
}
153153

154+
// the height of the navbars above is fluid across pages, and it's easy to get it wrong
155+
// so we calculate it dynamically here
156+
let sidebarTopOffset = isOpen
157+
? {
158+
let mobileNavbarHeight =
159+
Nullable.make(document->WebAPI.Document.getElementById("mobile-navbar"))
160+
->Nullable.map(el => el.clientHeight)
161+
->Nullable.getOr(0)
162+
let docNavbarHeight =
163+
Nullable.make(document->WebAPI.Document.getElementById("doc-navbar"))
164+
->Nullable.map(el => el.clientHeight)
165+
->Nullable.getOr(0)
166+
let mainNavbarHeight =
167+
Nullable.make(document->WebAPI.Document.getElementById("main-navbar"))
168+
->Nullable.map(el => el.clientHeight)
169+
->Nullable.getOr(0)
170+
171+
mobileNavbarHeight + docNavbarHeight + mainNavbarHeight
172+
}
173+
: 0
174+
175+
// If a user changes screen size we want to close the sidebar
176+
React.useEffect(() => {
177+
let handleResize = () => {
178+
if isOpen {
179+
toggle()
180+
}
181+
}
182+
183+
addEventListener(Resize, handleResize)
184+
Some(() => removeEventListener(Resize, handleResize))
185+
}, [isOpen])
186+
154187
<>
155188
<div
156189
id="sidebar"
190+
style={{
191+
top: isOpen ? `${sidebarTopOffset->Int.toString}px` : "0px",
192+
}}
157193
className={(
158-
isOpen ? "fixed left-0 h-full z-20 " : "hidden"
159-
) ++ " min-w-3xs max-w-3xs lg:max-w-xs md:block h-auto md:relative overflow-y-visible px-4 md:pl-0 pt-2 bg-white md:mt-0"}
194+
isOpen ? "fixed left-0 h-full w-full" : "hidden"
195+
) ++ " z-20 min-w-3xs md:max-w-3xs lg:max-w-xs md:block h-auto md:relative overflow-y-visible px-4 md:pl-0 pt-2 bg-white md:mt-0 border-r border-gray-20"}
160196
>
161197
<aside
162198
id="sidebar-content"
163-
className="h-full relative top-0 block md:top-28 md:sticky border-r border-gray-20 overflow-y-auto pb-24 max-h-[calc(100vh-7rem)] px-4"
199+
className="h-full relative top-0 block md:top-28 md:sticky overflow-y-auto pb-24 max-h-[calc(100vh-7rem)] px-4"
164200
>
165201
<button
202+
style={{
203+
// This is needed to make sure it's clickable
204+
zIndex: "20",
205+
}}
166206
onClick={evt => {
167207
ReactEvent.Mouse.preventDefault(evt)
208+
Console.debug("Sidebar toggle")
168209
toggle()
169210
}}
170-
className="md:hidden h-16 flex pt-2 right-4 absolute"
211+
className="md:hidden h-16 flex pt-2 right-4 absolute cursor-pointer"
171212
>
172213
<Icon.Close />
173214
</button>
@@ -297,6 +338,7 @@ let make = (
297338

298339
let handleDrawerButtonClick = React.useCallback(evt => {
299340
ReactEvent.Mouse.preventDefault(evt)
341+
Console.debug("drawer button clicked")
300342
toggleSidebar()
301343
}, [])
302344

@@ -352,7 +394,7 @@ let make = (
352394
theme}
353395
>
354396
sidebar
355-
<main className="px-4 md:px-0 md:pt-4 lg:px-4 lg:pl-16 lg:mr-8 mb-32 max-w-svw">
397+
<main className="px-4 md:pt-4 lg:px-4 lg:pl-16 lg:mr-8 mb-32 max-w-svw">
356398
// width of the right content part
357399
<div
358400
id="mobile-navbar"

0 commit comments

Comments
 (0)