Add dark/light toggle to admin sidebar and topbar.
Local Build and Deploy / deploy (push) Successful in 55s
Local Build and Deploy / deploy (push) Successful in 55s
Reuse ThemeSwitcher with an admin variant (Sun/Moon) next to the staff profile and in the housekeeping header. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
363c9b3d56
commit
1e3eb86495
2 files changed
+31
-4
No files matched your search
@@ -6,6 +6,7 @@ import type { ReactNode } from "react";
|
|||||||
import { AdminHubChrome } from "@/components/admin/admin-hub-chrome";
|
import { AdminHubChrome } from "@/components/admin/admin-hub-chrome";
|
||||||
import { AdminMobileWrapper } from "@/components/admin/admin-mobile-wrapper";
|
import { AdminMobileWrapper } from "@/components/admin/admin-mobile-wrapper";
|
||||||
import { AdminNavLink } from "@/components/admin/admin-nav-link";
|
import { AdminNavLink } from "@/components/admin/admin-nav-link";
|
||||||
|
import { ThemeSwitcher } from "@/components/theme-switcher";
|
||||||
import { requireStaff } from "@/lib/admin/guard";
|
import { requireStaff } from "@/lib/admin/guard";
|
||||||
import { ADMIN_NAV_GROUPS } from "@/lib/admin-nav";
|
import { ADMIN_NAV_GROUPS } from "@/lib/admin-nav";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
@@ -73,6 +74,7 @@ async function Sidebar({
|
|||||||
{t("rankLabel", { rank: staff.rank })}
|
{t("rankLabel", { rank: staff.rank })}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
<ThemeSwitcher variant="admin" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav className="flex-1 overflow-y-auto px-3 py-4">
|
<nav className="flex-1 overflow-y-auto px-3 py-4">
|
||||||
@@ -141,6 +143,7 @@ async function Topbar({
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
|
<ThemeSwitcher variant="admin" />
|
||||||
<span className="text-xs text-[var(--admin-text-muted)]">
|
<span className="text-xs text-[var(--admin-text-muted)]">
|
||||||
<strong className="text-[var(--admin-text)]">{staff.username}</strong>
|
<strong className="text-[var(--admin-text)]">{staff.username}</strong>
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { Moon, Sun } from "lucide-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Light/dark toggle. The class lives on <html>; theme-init.js applies the
|
* Light/dark toggle. The class lives on <html>; theme-init.js applies the
|
||||||
* saved choice before paint. We sync React state after mount so SSR hydration
|
* saved choice before paint. We sync React state after mount so SSR hydration
|
||||||
* does not desync from the real DOM class (which forced a double-click before).
|
* does not desync from the real DOM class.
|
||||||
*/
|
*/
|
||||||
export function ThemeSwitcher() {
|
export function ThemeSwitcher({
|
||||||
|
variant = "nav",
|
||||||
|
}: {
|
||||||
|
variant?: "nav" | "admin";
|
||||||
|
}) {
|
||||||
const [dark, setDark] = useState(false);
|
const [dark, setDark] = useState(false);
|
||||||
const [ready, setReady] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
|
|
||||||
@@ -27,13 +32,32 @@ export function ThemeSwitcher() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const label = dark ? "Switch to light mode" : "Switch to dark mode";
|
||||||
|
const title = dark ? "Light mode" : "Dark mode";
|
||||||
|
|
||||||
|
if (variant === "admin") {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={toggle}
|
||||||
|
aria-label={label}
|
||||||
|
title={title}
|
||||||
|
suppressHydrationWarning
|
||||||
|
className="flex-none grid place-items-center w-9 h-9 rounded-xl border border-[var(--admin-border)] text-[var(--admin-sidebar-text-readable)] hover:bg-[var(--admin-accent)]/15 hover:text-[var(--admin-accent)] transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--admin-accent)]"
|
||||||
|
style={{ visibility: ready ? "visible" : "hidden" }}
|
||||||
|
>
|
||||||
|
{dark ? <Sun size={16} /> : <Moon size={16} />}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={toggle}
|
onClick={toggle}
|
||||||
className="nav-item cursor-pointer text-lg leading-none px-2"
|
className="nav-item cursor-pointer text-lg leading-none px-2"
|
||||||
aria-label={dark ? "Switch to light mode" : "Switch to dark mode"}
|
aria-label={label}
|
||||||
title={dark ? "Light mode" : "Dark mode"}
|
title={title}
|
||||||
suppressHydrationWarning
|
suppressHydrationWarning
|
||||||
style={{ visibility: ready ? "visible" : "hidden" }}
|
style={{ visibility: ready ? "visible" : "hidden" }}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in new issue
Block a user