-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add theme toggle functionality with dark and light mode support
- Imported ThemeToggle component in AppSidebar - Added ThemeToggle to SidebarFooter in AppSidebar - Initialized theme based on localStorage and system preferences in main.tsx to support dark and light modes
- Loading branch information
1 parent
76d4f42
commit c673fc9
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Moon, Sun } from "lucide-react"; | ||
import { Button } from "@/components/ui/button"; | ||
import { useTheme } from "@/hooks/use-theme"; | ||
|
||
export function ThemeToggle() { | ||
const { theme, setTheme } = useTheme(); | ||
|
||
return ( | ||
<Button | ||
variant="ghost" | ||
size="icon" | ||
onClick={() => setTheme(theme === "light" ? "dark" : "light")} | ||
> | ||
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" /> | ||
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" /> | ||
<span className="sr-only">Toggle theme</span> | ||
</Button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
type Theme = "dark" | "light" | "system"; | ||
|
||
function useTheme() { | ||
const [theme, setTheme] = useState<Theme>( | ||
() => (localStorage.getItem("theme") as Theme) || "system" | ||
); | ||
|
||
useEffect(() => { | ||
const media = window.matchMedia("(prefers-color-scheme: dark)"); | ||
|
||
function applyTheme() { | ||
const root = window.document.documentElement; | ||
const systemTheme = media.matches ? "dark" : "light"; | ||
const activeTheme = theme === "system" ? systemTheme : theme; | ||
|
||
root.classList.remove("light", "dark"); | ||
root.classList.add(activeTheme); | ||
localStorage.setItem("theme", theme); | ||
} | ||
|
||
applyTheme(); | ||
media.addEventListener("change", applyTheme); | ||
return () => media.removeEventListener("change", applyTheme); | ||
}, [theme]); | ||
|
||
return { theme, setTheme } as const; | ||
} | ||
|
||
export { useTheme }; | ||
export type { Theme }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters