mirror of
https://github.com/FranP-code/inbox-negotiator.git
synced 2025-10-13 00:42:26 +00:00
Add ModeToggle component and integrate it into Navbar; enhance dark mode handling in Layout
This commit is contained in:
97
src/components/ModeToggle.tsx
Normal file
97
src/components/ModeToggle.tsx
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import { Moon, Sun, Laptop } from "lucide-react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
DropdownMenu,
|
||||||
|
DropdownMenuContent,
|
||||||
|
DropdownMenuItem,
|
||||||
|
DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
|
||||||
|
const THEME_KEY = "theme";
|
||||||
|
|
||||||
|
type Theme = "light" | "dark" | "system";
|
||||||
|
|
||||||
|
function getSystemTheme(): Theme {
|
||||||
|
if (typeof window !== "undefined" && window.matchMedia) {
|
||||||
|
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
||||||
|
? "dark"
|
||||||
|
: "light";
|
||||||
|
}
|
||||||
|
return "light";
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStoredTheme(): Theme | null {
|
||||||
|
if (typeof localStorage !== "undefined") {
|
||||||
|
const t = localStorage.getItem(THEME_KEY);
|
||||||
|
if (t === "light" || t === "dark" || t === "system") return t;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setStoredTheme(theme: Theme | null) {
|
||||||
|
if (typeof localStorage !== "undefined") {
|
||||||
|
if (theme) {
|
||||||
|
localStorage.setItem(THEME_KEY, theme);
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem(THEME_KEY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyTheme(theme: Theme) {
|
||||||
|
const root = document.documentElement;
|
||||||
|
if (theme === "system") {
|
||||||
|
root.classList.toggle("dark", getSystemTheme() === "dark");
|
||||||
|
} else {
|
||||||
|
root.classList.toggle("dark", theme === "dark");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ModeToggle() {
|
||||||
|
const [theme, setTheme] = React.useState<Theme>(
|
||||||
|
() => getStoredTheme() || "system"
|
||||||
|
);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const stored = getStoredTheme();
|
||||||
|
if (stored) setTheme(stored);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setStoredTheme(theme === "system" ? null : theme);
|
||||||
|
applyTheme(theme);
|
||||||
|
}, [theme]);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (theme === "system") {
|
||||||
|
const mq = window.matchMedia("(prefers-color-scheme: dark)");
|
||||||
|
const handler = () => applyTheme("system");
|
||||||
|
mq.addEventListener("change", handler);
|
||||||
|
return () => mq.removeEventListener("change", handler);
|
||||||
|
}
|
||||||
|
}, [theme]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="outline" size="icon">
|
||||||
|
<Sun className="h-[1.2rem] w-[1.2rem] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||||
|
<Moon className="absolute h-[1.2rem] w-[1.2rem] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||||
|
<span className="sr-only">Toggle theme</span>
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => setTheme("light")}>
|
||||||
|
Light
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem onClick={() => setTheme("dark")}>
|
||||||
|
Dark
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem onClick={() => setTheme("system")}>
|
||||||
|
System
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
} from '@/components/ui/dropdown-menu';
|
} from '@/components/ui/dropdown-menu';
|
||||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
|
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
|
||||||
import { BarChart3, LogOut, User as UserIcon } from 'lucide-react';
|
import { BarChart3, LogOut, User as UserIcon } from 'lucide-react';
|
||||||
|
import { ModeToggle } from './ModeToggle';
|
||||||
|
|
||||||
export function Navbar() {
|
export function Navbar() {
|
||||||
const [user, setUser] = useState<User | null>(null);
|
const [user, setUser] = useState<User | null>(null);
|
||||||
@@ -39,21 +40,22 @@ export function Navbar() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav className="border-b bg-white">
|
<nav className="border-b bg-white dark:bg-background">
|
||||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||||
<div className="flex justify-between items-center h-16">
|
<div className="flex justify-between items-center h-16">
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<BarChart3 className="h-8 w-8 text-primary" />
|
<BarChart3 className="h-8 w-8 text-primary" />
|
||||||
<a href="/" className="text-xl font-bold text-gray-900">
|
<a href="/" className="text-xl font-bold text-gray-900 dark:text-foreground">
|
||||||
InboxNegotiator
|
InboxNegotiator
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-4">
|
<div className="flex items-center gap-4">
|
||||||
|
<ModeToggle />
|
||||||
{user ? (
|
{user ? (
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<Button variant="ghost\" className="relative h-8 w-8 rounded-full">
|
<Button variant="ghost" className="relative h-8 w-8 rounded-full">
|
||||||
<Avatar className="h-8 w-8">
|
<Avatar className="h-8 w-8">
|
||||||
<AvatarFallback className="text-xs">
|
<AvatarFallback className="text-xs">
|
||||||
{getInitials(user.email || '')}
|
{getInitials(user.email || '')}
|
||||||
|
|||||||
@@ -8,17 +8,36 @@ const { title } = Astro.props;
|
|||||||
|
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="description" content="Astro description" />
|
<meta name="description" content="Astro description" />
|
||||||
<meta name="viewport" content="width=device-width" />
|
<meta name="viewport" content="width=device-width" />
|
||||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
<meta name="generator" content={Astro.generator} />
|
<meta name="generator" content={Astro.generator} />
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
</head>
|
<script is:inline>
|
||||||
<body>
|
// shadcn dark mode script for Astro
|
||||||
<slot />
|
const themeKey = 'theme';
|
||||||
</body>
|
function getThemePreference() {
|
||||||
|
if (typeof localStorage !== 'undefined' && localStorage.getItem(themeKey)) {
|
||||||
|
return localStorage.getItem(themeKey);
|
||||||
|
}
|
||||||
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||||
|
}
|
||||||
|
const isDark = getThemePreference() === 'dark';
|
||||||
|
document.documentElement.classList[isDark ? 'add' : 'remove']('dark');
|
||||||
|
if (typeof localStorage !== 'undefined') {
|
||||||
|
const observer = new MutationObserver(() => {
|
||||||
|
const isDark = document.documentElement.classList.contains('dark');
|
||||||
|
localStorage.setItem(themeKey, isDark ? 'dark' : 'light');
|
||||||
|
});
|
||||||
|
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<slot />
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
<style is:global>
|
<style is:global>
|
||||||
:root {
|
:root {
|
||||||
|
|||||||
Reference in New Issue
Block a user