|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { TopAppBar, IconButton } from "soda-material"; |
| 3 | +import { useWindowSizeType } from "soda-material/dist/hooks/use-media-query"; |
| 4 | +import { mdiMenu, mdiGithub } from "@mdi/js"; |
| 5 | +import type { NavSection } from "../App.tsx"; |
| 6 | + |
| 7 | +interface LayoutProps { |
| 8 | + navSections: NavSection[]; |
| 9 | + currentHash: string; |
| 10 | + onNavigate: (hash: string) => void; |
| 11 | + children: React.ReactNode; |
| 12 | +} |
| 13 | + |
| 14 | +function Sidebar({ |
| 15 | + navSections, |
| 16 | + currentHash, |
| 17 | + onNavigate, |
| 18 | +}: Pick<LayoutProps, "navSections" | "currentHash" | "onNavigate">) { |
| 19 | + return ( |
| 20 | + <nav |
| 21 | + style={{ |
| 22 | + display: "flex", |
| 23 | + flexDirection: "column", |
| 24 | + padding: "1.5rem 0 2rem", |
| 25 | + gap: 0, |
| 26 | + }} |
| 27 | + > |
| 28 | + {navSections.map((section) => ( |
| 29 | + <div key={section.title} style={{ marginBottom: "1.25rem" }}> |
| 30 | + {/* Section label */} |
| 31 | + <div |
| 32 | + style={{ |
| 33 | + padding: "0 1.5rem 0.4rem", |
| 34 | + fontSize: 11, |
| 35 | + fontWeight: 700, |
| 36 | + letterSpacing: "0.1em", |
| 37 | + textTransform: "uppercase", |
| 38 | + color: "var(--md-sys-color-on-surface-variant)", |
| 39 | + opacity: 0.6, |
| 40 | + }} |
| 41 | + > |
| 42 | + {section.title} |
| 43 | + </div> |
| 44 | + |
| 45 | + {/* Nav items */} |
| 46 | + {section.items.map((item) => { |
| 47 | + const active = currentHash === item.hash; |
| 48 | + return ( |
| 49 | + <a |
| 50 | + key={item.hash} |
| 51 | + href={item.hash} |
| 52 | + onClick={(e) => { |
| 53 | + e.preventDefault(); |
| 54 | + onNavigate(item.hash); |
| 55 | + }} |
| 56 | + style={{ |
| 57 | + display: "block", |
| 58 | + padding: "0.45rem 1.5rem", |
| 59 | + fontSize: 14, |
| 60 | + fontWeight: active ? 600 : 400, |
| 61 | + color: active |
| 62 | + ? "var(--md-sys-color-primary)" |
| 63 | + : "var(--md-sys-color-on-surface-variant)", |
| 64 | + textDecoration: "none", |
| 65 | + borderLeft: `2px solid ${active ? "var(--md-sys-color-primary)" : "transparent"}`, |
| 66 | + background: active ? "var(--md-sys-color-primary-container)" : "transparent", |
| 67 | + transition: "color 150ms, background 150ms, border-color 150ms", |
| 68 | + }} |
| 69 | + onMouseEnter={(e) => { |
| 70 | + if (!active) { |
| 71 | + (e.currentTarget as HTMLAnchorElement).style.color = |
| 72 | + "var(--md-sys-color-on-surface)"; |
| 73 | + (e.currentTarget as HTMLAnchorElement).style.background = |
| 74 | + "var(--md-sys-color-surface-container)"; |
| 75 | + } |
| 76 | + }} |
| 77 | + onMouseLeave={(e) => { |
| 78 | + if (!active) { |
| 79 | + (e.currentTarget as HTMLAnchorElement).style.color = |
| 80 | + "var(--md-sys-color-on-surface-variant)"; |
| 81 | + (e.currentTarget as HTMLAnchorElement).style.background = "transparent"; |
| 82 | + } |
| 83 | + }} |
| 84 | + > |
| 85 | + {item.label} |
| 86 | + </a> |
| 87 | + ); |
| 88 | + })} |
| 89 | + </div> |
| 90 | + ))} |
| 91 | + </nav> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +export default function Layout({ navSections, currentHash, onNavigate, children }: LayoutProps) { |
| 96 | + const [drawerOpen, setDrawerOpen] = useState(false); |
| 97 | + const sizeType = useWindowSizeType(); |
| 98 | + const isExpanded = sizeType === "expanded"; |
| 99 | + |
| 100 | + const handleNavClick = (hash: string) => { |
| 101 | + location.hash = hash; |
| 102 | + onNavigate(hash); |
| 103 | + setDrawerOpen(false); |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <div |
| 108 | + style={{ |
| 109 | + display: "flex", |
| 110 | + height: "100vh", |
| 111 | + overflow: "hidden", |
| 112 | + background: "var(--md-sys-color-surface)", |
| 113 | + }} |
| 114 | + > |
| 115 | + {/* Desktop sidebar */} |
| 116 | + {isExpanded && ( |
| 117 | + <aside |
| 118 | + style={{ |
| 119 | + width: 240, |
| 120 | + flexShrink: 0, |
| 121 | + display: "flex", |
| 122 | + flexDirection: "column", |
| 123 | + borderRight: "1px solid var(--md-sys-color-outline-variant)", |
| 124 | + overflowY: "auto", |
| 125 | + }} |
| 126 | + > |
| 127 | + {/* Logo */} |
| 128 | + <div |
| 129 | + style={{ |
| 130 | + padding: "1.25rem 1.5rem 0.75rem", |
| 131 | + fontSize: 15, |
| 132 | + fontWeight: 700, |
| 133 | + letterSpacing: "-0.01em", |
| 134 | + color: "var(--md-sys-color-on-surface)", |
| 135 | + borderBottom: "1px solid var(--md-sys-color-outline-variant)", |
| 136 | + }} |
| 137 | + > |
| 138 | + module-tsx |
| 139 | + </div> |
| 140 | + <Sidebar |
| 141 | + navSections={navSections} |
| 142 | + currentHash={currentHash} |
| 143 | + onNavigate={handleNavClick} |
| 144 | + /> |
| 145 | + </aside> |
| 146 | + )} |
| 147 | + |
| 148 | + {/* Mobile drawer overlay */} |
| 149 | + {!isExpanded && drawerOpen && ( |
| 150 | + <> |
| 151 | + <div |
| 152 | + onClick={() => setDrawerOpen(false)} |
| 153 | + style={{ |
| 154 | + position: "fixed", |
| 155 | + inset: 0, |
| 156 | + background: "rgba(0,0,0,0.32)", |
| 157 | + zIndex: 100, |
| 158 | + }} |
| 159 | + /> |
| 160 | + <aside |
| 161 | + style={{ |
| 162 | + position: "fixed", |
| 163 | + top: 0, |
| 164 | + left: 0, |
| 165 | + bottom: 0, |
| 166 | + width: 280, |
| 167 | + background: "var(--md-sys-color-surface)", |
| 168 | + borderRight: "1px solid var(--md-sys-color-outline-variant)", |
| 169 | + overflowY: "auto", |
| 170 | + zIndex: 101, |
| 171 | + }} |
| 172 | + > |
| 173 | + <div |
| 174 | + style={{ |
| 175 | + padding: "1.25rem 1.5rem 0.75rem", |
| 176 | + fontSize: 15, |
| 177 | + fontWeight: 700, |
| 178 | + letterSpacing: "-0.01em", |
| 179 | + color: "var(--md-sys-color-on-surface)", |
| 180 | + borderBottom: "1px solid var(--md-sys-color-outline-variant)", |
| 181 | + }} |
| 182 | + > |
| 183 | + module-tsx |
| 184 | + </div> |
| 185 | + <Sidebar |
| 186 | + navSections={navSections} |
| 187 | + currentHash={currentHash} |
| 188 | + onNavigate={handleNavClick} |
| 189 | + /> |
| 190 | + </aside> |
| 191 | + </> |
| 192 | + )} |
| 193 | + |
| 194 | + {/* Main column */} |
| 195 | + <div style={{ flex: 1, display: "flex", flexDirection: "column", overflow: "hidden" }}> |
| 196 | + <TopAppBar |
| 197 | + leadingNavigationIcon={ |
| 198 | + !isExpanded ? ( |
| 199 | + <IconButton path={mdiMenu} onClick={() => setDrawerOpen((v) => !v)} /> |
| 200 | + ) : undefined |
| 201 | + } |
| 202 | + trailingIcon={ |
| 203 | + <IconButton |
| 204 | + path={mdiGithub} |
| 205 | + onClick={() => window.open("https://github.com/YieldRay/module-tsx", "_blank")} |
| 206 | + /> |
| 207 | + } |
| 208 | + > |
| 209 | + module-tsx |
| 210 | + </TopAppBar> |
| 211 | + <main className="sd-scrollbar" style={{ flex: 1, overflowY: "auto" }}> |
| 212 | + <div style={{ maxWidth: 860, margin: "0 auto", padding: "2rem 1.5rem 4rem" }}> |
| 213 | + {children} |
| 214 | + </div> |
| 215 | + </main> |
| 216 | + </div> |
| 217 | + </div> |
| 218 | + ); |
| 219 | +} |
0 commit comments