- Install rehype-slug (proper heading anchors) + github-slugger (matching TOC) - TOC component: auto-generated from h2/h3 headings, clickable anchors - Dark mode toggle: ThemeToggle (localStorage + system default), class-based - /docs index page (lists all docs by section) - Markdown.tsx uses rehype-slug for stable heading ids - globals.css: @custom-variant dark (.dark class) for class-based dark mode - layout.tsx: nav includes ThemeToggle Note: per user instruction, installed real deps (rehype-slug, github-slugger, @types/hast) instead of hacky inline any-cast hacks.
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
const STORAGE_KEY = "mcpedia-theme";
|
|
|
|
/** Dark mode toggle. Defaults to system preference, persisted in localStorage. */
|
|
export default function ThemeToggle() {
|
|
const [dark, setDark] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
const initial =
|
|
stored === "dark" ||
|
|
(stored === null &&
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches);
|
|
setDark(initial);
|
|
apply(initial);
|
|
}, []);
|
|
|
|
function apply(isDark: boolean) {
|
|
document.documentElement.classList.toggle("dark", isDark);
|
|
}
|
|
|
|
function toggle() {
|
|
const next = !dark;
|
|
setDark(next);
|
|
localStorage.setItem(STORAGE_KEY, next ? "dark" : "light");
|
|
apply(next);
|
|
}
|
|
|
|
return (
|
|
<button
|
|
onClick={toggle}
|
|
aria-label="Toggle dark mode"
|
|
className="text-sm border border-zinc-300 dark:border-zinc-700 rounded px-2 py-1 hover:bg-zinc-100 dark:hover:bg-zinc-800"
|
|
>
|
|
{dark ? "🌙" : "☀️"}
|
|
</button>
|
|
);
|
|
}
|