({ children }: { children: ReactNode })
| 44 | * hidden and inert there, so its links never take focus off screen. |
| 45 | */ |
| 46 | export 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 | }} |
nothing calls this directly
no test coverage detected