Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

feat: change theme toggler (#1539)

Aakashbff6122a079f
diff --git a/apps/web/src/components/Navigation/index.tsx b/apps/web/src/components/Navigation/index.tsx
index d33ed64a..c334f33e 100644
--- a/apps/web/src/components/Navigation/index.tsx
+++ b/apps/web/src/components/Navigation/index.tsx
@@ -6,7 +6,7 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@repo/ui/components/dropdown-menu';
-import { Play, Settings, Settings2, User } from '@repo/ui/icons';
+import { Play, Settings, Settings2, User, Palette } from '@repo/ui/icons';
import Link from 'next/link';
import { Suspense } from 'react';
import { isAdminOrModerator } from '~/utils/auth-guards';
@@ -22,6 +22,7 @@ import { auth } from '~/server/auth';
import { NotificationLink } from './notification-link';
import { getNotificationCount } from './navigation.actions';
import { Avatar, AvatarFallback, AvatarImage } from '@repo/ui/components/avatar';
+import { ThemeButton } from './theme-button';
export function getAdminUrl() {
// reference for vercel.com
@@ -58,11 +59,22 @@ export async function Navigation() {
<div className="ml-4 hidden items-center gap-4 md:flex">{TopSectionLinks}</div>
<div className="flex flex-col gap-5 pl-4 md:hidden">
{TopSectionLinks}
+ {!session?.user && (
+ <div className="flex items-center gap-2">
+ <span>Theme</span>
+ <ThemeButton />
+ </div>
+ )}
+
{session?.user ? (
<>
<hr />
<NavLink title="Profile" href={`/@${session.user.name}`} />
<NavLink title="Settings" href="/settings" />
+ <div className="flex items-center gap-2">
+ <span>Theme</span>
+ <ThemeButton />
+ </div>
{isAdminOrMod ? <NavLink title="Admin" href={getAdminUrl()} /> : null}
{isAdminOrMod ? (
<NavLink title="Challenge Playground" href="/challenge-playground" />
@@ -167,6 +179,14 @@ async function LoginButton({
<span>Settings</span>
</DropdownMenuItem>
</Link>
+ <DropdownMenuSeparator />
+ <div className="flex items-center justify-between rounded-lg px-2 py-0.5 text-sm ">
+ <div className="flex items-center">
+ <Palette className="mr-2 h-4 w-4" />
+ <span>Theme</span>
+ </div>
+ <ThemeButton />
+ </div>
{isAdminOrMod ? (
<a className="block" href={getAdminUrl()}>
<DropdownMenuItem className="focus:bg-accent rounded-lg p-2 duration-300 focus:outline-none dark:hover:bg-neutral-700/50">
diff --git a/apps/web/src/components/Navigation/theme-button.tsx b/apps/web/src/components/Navigation/theme-button.tsx
index 0b1005c9..8e710afd 100644
--- a/apps/web/src/components/Navigation/theme-button.tsx
+++ b/apps/web/src/components/Navigation/theme-button.tsx
@@ -1,41 +1,53 @@
'use client';
import { useEffect, useState } from 'react';
import { useTheme } from 'next-themes';
-import { Moon, Sun } from '@repo/ui/icons';
+import { Laptop, Moon, Sun } from '@repo/ui/icons';
+import { cn } from '@repo/ui/cn';
+import { motion } from 'framer-motion';
+
+const themes = ['system', 'light', 'dark'] as const;
export function ThemeButton() {
const [mounted, setMounted] = useState(false);
- const { setTheme, resolvedTheme } = useTheme();
+ const { setTheme, theme } = useTheme();
useEffect(() => {
setMounted(true);
}, []);
+ if (!mounted) return null;
+
return (
- <>
- {mounted ? (
- <button
- aria-label="theme button"
- className="group rounded-lg p-2 focus:outline-[#2563EB]"
- onClick={() => {
- setTheme(resolvedTheme === 'dark' ? 'light' : 'dark');
- }}
- type="button"
- >
- {resolvedTheme === 'dark' && (
- <Moon
- aria-hidden="true"
- className="h-5 w-5 duration-150 group-hover:scale-110 group-hover:fill-black dark:group-hover:fill-white"
- />
- )}
- {resolvedTheme === 'light' && (
- <Sun
- aria-hidden="true"
- className="h-5 w-5 duration-150 group-hover:scale-110 group-hover:fill-black dark:group-hover:fill-white"
- />
- )}
- </button>
- ) : null}
- </>
+ <div className={cn('flex items-center rounded-full border p-0.5')}>
+ {themes.map((t) => {
+ const isActive = theme === t;
+ return (
+ <motion.button
+ key={t}
+ className={cn('rounded-full p-1.5', isActive && 'bg-secondary')}
+ onClick={() => setTheme(t)}
+ aria-label="theme button"
+ whileHover={{ scale: 1.1 }}
+ whileTap={{ scale: 0.95 }}
+ layout
+ transition={{ type: 'spring', stiffness: 300, damping: 20 }}
+ >
+ {t === 'system' && <Laptop aria-hidden="true" className="size-4" />}
+ {t === 'light' && (
+ <Sun
+ aria-hidden="true"
+ className={cn('size-4', isActive && 'fill-black dark:fill-white')}
+ />
+ )}
+ {t === 'dark' && (
+ <Moon
+ aria-hidden="true"
+ className={cn('size-4', isActive && 'fill-black dark:fill-white')}
+ />
+ )}
+ </motion.button>
+ );
+ })}
+ </div>
);
}
diff --git a/packages/ui/src/icons.ts b/packages/ui/src/icons.ts
index 74994581..bd1585cd 100644
--- a/packages/ui/src/icons.ts
+++ b/packages/ui/src/icons.ts
@@ -31,6 +31,7 @@ export {
Heart,
GitBranch,
Github,
+ Laptop,
Loader,
Loader2,
Lock,
@@ -53,6 +54,7 @@ export {
Settings,
Settings2,
Shapes,
+ Palette,
Share,
Shield,
ShieldAlert,