Merge pull request #53 from anshnk/main

Add Theme Toggle Component To Settings and Update Theme Management/UI
This commit is contained in:
Owski 2024-10-14 11:37:02 -05:00 committed by GitHub
commit 6c65689b1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 28 additions and 26 deletions

View file

@ -23,7 +23,6 @@
--destructive: 6 96% 59%; --destructive: 6 96% 59%;
--destructive-foreground: 0 0% 100%; --destructive-foreground: 0 0% 100%;
--ring: 215.09 100% 98.03%; --ring: 215.09 100% 98.03%;
--radius: 0.4rem; --radius: 0.4rem;
} }

View file

@ -3,6 +3,7 @@
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { z } from "zod"; import { z } from "zod";
import { ModeToggle } from "@/components/ThemeSwitch";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { import {
@ -87,6 +88,7 @@ export default function Settings() {
</div> </div>
</form> </form>
</Form> </Form>
<ModeToggle />
</div> </div>
); );
} }

View file

@ -16,6 +16,7 @@ export default function Credits() {
<li>Scaratek</li> <li>Scaratek</li>
<li>fwxe</li> <li>fwxe</li>
<li>Nebelung</li> <li>Nebelung</li>
<li>anshnk</li>
</ul> </ul>
</div> </div>
</div> </div>

View file

@ -18,9 +18,10 @@ export function ModeToggle() {
return ( return (
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
<Button variant="outline" size="icon"> <Button variant="outline">
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" /> <Sun className="h-5 w-5 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0 mr-1" />
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" /> <Moon className="absolute h-5 w-5 rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100 mr-[60px]" />
<p>Themes</p>
<span className="sr-only">Toggle theme</span> <span className="sr-only">Toggle theme</span>
</Button> </Button>
</DropdownMenuTrigger> </DropdownMenuTrigger>

View file

@ -20,7 +20,6 @@ export default function Navbar() {
<Button onClick={() => setOpen(true)} size="icon" variant="ghost"> <Button onClick={() => setOpen(true)} size="icon" variant="ghost">
<Lucide.Menu className="h-7 w-7" /> <Lucide.Menu className="h-7 w-7" />
</Button> </Button>
<ModeToggle />
{/* Wrap the logo and text in a Link */} {/* Wrap the logo and text in a Link */}
<Link href="/" className="flex items-center gap-2"> <Link href="/" className="flex items-center gap-2">
<Lucide.Radius className="h-8 w-8 rotate-180" /> <Lucide.Radius className="h-8 w-8 rotate-180" />

View file

@ -1,17 +1,17 @@
import { createContext, useContext, useEffect, useState } from "react"; import { createContext, useContext, useEffect, useState } from 'react';
type Theme = type Theme =
| "radius" | 'radius'
| "cyberpunk" | 'cyberpunk'
| "bluelight" | 'bluelight'
| "midnight" | 'midnight'
| "system"; | 'system';
const themes: Theme[] = [ const themes: Theme[] = [
"radius", 'radius',
"cyberpunk", 'cyberpunk',
"bluelight", 'bluelight',
"midnight", 'midnight',
"system", 'system',
]; ];
type ThemeProviderProps = { type ThemeProviderProps = {
children: React.ReactNode; children: React.ReactNode;
@ -26,7 +26,7 @@ type ThemeProviderState = {
const initialState: ThemeProviderState = { const initialState: ThemeProviderState = {
themes: themes, themes: themes,
theme: "system", theme: 'system',
setTheme: () => null, setTheme: () => null,
}; };
@ -34,8 +34,8 @@ const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
export function ThemeProvider({ export function ThemeProvider({
children, children,
defaultTheme = "system", defaultTheme = 'system',
storageKey = "theme", storageKey = 'theme',
...props ...props
}: ThemeProviderProps) { }: ThemeProviderProps) {
const [theme, setTheme] = useState<Theme>( const [theme, setTheme] = useState<Theme>(
@ -45,13 +45,13 @@ export function ThemeProvider({
useEffect(() => { useEffect(() => {
const root = window.document.documentElement; const root = window.document.documentElement;
themes.forEach((theme) => root.classList.remove(theme)); root.classList.remove(...themes);
if (theme === "system") { if (theme === 'system') {
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)") const systemTheme = window.matchMedia('(prefers-color-scheme: dark)')
.matches .matches
? "default" ? 'midnight'
: "bluelight"; : 'bluelight';
root.classList.add(systemTheme); root.classList.add(systemTheme);
return; return;
@ -80,7 +80,7 @@ export const useTheme = () => {
const context = useContext(ThemeProviderContext); const context = useContext(ThemeProviderContext);
if (context === undefined) if (context === undefined)
throw new Error("useTheme must be used within a ThemeProvider"); throw new Error('useTheme must be used within a ThemeProvider');
return context; return context;
}; };