MCPcopy Create free account
hub / github.com/arktypeio/arktype / Drawer

Function Drawer

ark/docs/components/shell/MobileNav.tsx:46–122  ·  view source on GitHub ↗
({ children }: { children: ReactNode })

Source from the content-addressed store, hash-verified

44 * hidden and inert there, so its links never take focus off screen.
45 */
46export const Drawer = ({ children }: { children: ReactNode }) => {
47 const open = useDrawerOpen()
48 const phone = useIsPhone()
49 const modal = phone && open
50 const pathname = usePathname()
51 const closeButton = useRef<HTMLButtonElement>(null)
52 const drawer = useRef<HTMLElement>(null)
53 const refocusMenuButton = useRef(false)
54
55 const close = () => {
56 refocusMenuButton.current = true
57 setDrawerOpen(false)
58 }
59
60 useEffect(() => setDrawerOpen(false), [pathname])
61
62 useEffect(() => {
63 if (!phone) setDrawerOpen(false)
64 }, [phone])
65
66 useEffect(() => {
67 document.documentElement.toggleAttribute("data-drawer-open", open)
68 if (!open) return
69 closePopups()
70 drawer.current
71 ?.querySelector(".side-nav [aria-current='page']")
72 ?.scrollIntoView({ block: "nearest" })
73 closeButton.current?.focus({ preventScroll: true })
74 }, [open])
75
76 useEffect(() => {
77 if (!modal) return
78 const background = [
79 ...document.querySelectorAll<HTMLElement>(backgroundSelector)
80 ]
81 for (const element of background) element.inert = true
82 return () => {
83 for (const element of background) element.inert = false
84 if (!refocusMenuButton.current) return
85 refocusMenuButton.current = false
86 document.getElementById(menuButtonId)?.focus({ preventScroll: true })
87 }
88 }, [modal])
89
90 return (
91 <>
92 <aside
93 ref={drawer}
94 id="drawer"
95 className="docs-sidebar"
96 aria-label="Docs navigation"
97 role={modal ? "dialog" : undefined}
98 aria-modal={modal || undefined}
99 data-open={open || undefined}
100 inert={phone && !open}
101 onKeyDown={e => {
102 if (e.key === "Escape" && open) close()
103 }}

Callers

nothing calls this directly

Calls 5

useDrawerOpenFunction · 0.90
setDrawerOpenFunction · 0.90
closePopupsFunction · 0.90
useIsPhoneFunction · 0.85
closeFunction · 0.70

Tested by

no test coverage detected