Skip to content

Commit 488c900

Browse files
committed
added support for system wide theme preferance
1 parent 622c5f7 commit 488c900

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

apps/dashboard/components/layout/theme-toggle.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { MoonIcon, SunIcon } from '@phosphor-icons/react';
3+
import { MonitorIcon, MoonIcon, SunIcon } from '@phosphor-icons/react';
44
import { useTheme } from 'next-themes';
55
import { Button } from '@/components/ui/button';
66
import { cn } from '@/lib/utils';
@@ -10,11 +10,18 @@ type ThemeTogglerProps = {
1010
};
1111

1212
export function ThemeToggle({ className }: ThemeTogglerProps) {
13-
const { resolvedTheme, setTheme } = useTheme();
13+
const { theme, setTheme } = useTheme();
1414

1515
const switchTheme = () => {
16-
setTheme(resolvedTheme === 'dark' ? 'light' : 'dark');
16+
if (theme === 'system') {
17+
setTheme('light');
18+
} else if (theme === 'light') {
19+
setTheme('dark');
20+
} else {
21+
setTheme('system');
22+
}
1723
};
24+
1825
return (
1926
<Button
2027
aria-label="Toggle theme"
@@ -27,12 +34,26 @@ export function ThemeToggle({ className }: ThemeTogglerProps) {
2734
variant="ghost"
2835
>
2936
<SunIcon
30-
className="dark:-rotate-90 h-5 w-5 rotate-0 scale-100 not-dark:text-primary transition-all duration-300 dark:scale-0"
37+
className={cn(
38+
'h-5 w-5 transition-all duration-300',
39+
theme === 'light' ? 'scale-100 rotate-0' : 'scale-0 -rotate-90'
40+
)}
3141
size={32}
3242
weight="duotone"
3343
/>
3444
<MoonIcon
35-
className="absolute h-5 w-5 rotate-90 scale-0 not-dark:text-primary transition-all duration-300 dark:rotate-0 dark:scale-100"
45+
className={cn(
46+
'absolute h-5 w-5 transition-all duration-300',
47+
theme === 'dark' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
48+
)}
49+
size={32}
50+
weight="duotone"
51+
/>
52+
<MonitorIcon
53+
className={cn(
54+
'absolute h-5 w-5 transition-all duration-300',
55+
theme === 'system' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
56+
)}
3657
size={32}
3758
weight="duotone"
3859
/>

apps/docs/components/theme-toggle.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { MoonIcon, SunIcon } from '@phosphor-icons/react';
3+
import { MonitorIcon, MoonIcon, SunIcon } from '@phosphor-icons/react';
44
import { useTheme } from 'next-themes';
55
import { Button } from '@/components/ui/button';
66
import { cn } from '@/lib/utils';
@@ -10,10 +10,16 @@ type ThemeToggleProps = {
1010
};
1111

1212
export function ThemeToggle({ className }: ThemeToggleProps) {
13-
const { resolvedTheme, setTheme } = useTheme();
13+
const { theme, setTheme } = useTheme();
1414

1515
const switchTheme = () => {
16-
setTheme(resolvedTheme === 'dark' ? 'light' : 'dark');
16+
if (theme === 'system') {
17+
setTheme('light');
18+
} else if (theme === 'light') {
19+
setTheme('dark');
20+
} else {
21+
setTheme('system');
22+
}
1723
};
1824

1925
const toggleTheme = () => {
@@ -32,12 +38,26 @@ export function ThemeToggle({ className }: ThemeToggleProps) {
3238
variant="ghost"
3339
>
3440
<SunIcon
35-
className="dark:-rotate-90 h-4 w-4 rotate-0 scale-100 transition-all dark:scale-0"
41+
className={cn(
42+
'h-4 w-4 transition-all duration-300',
43+
theme === 'light' ? 'scale-100 rotate-0' : 'scale-0 -rotate-90'
44+
)}
3645
size={16}
3746
weight="duotone"
3847
/>
3948
<MoonIcon
40-
className="absolute h-4 w-4 rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100"
49+
className={cn(
50+
'absolute h-4 w-4 transition-all duration-300',
51+
theme === 'dark' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
52+
)}
53+
size={16}
54+
weight="duotone"
55+
/>
56+
<MonitorIcon
57+
className={cn(
58+
'absolute h-4 w-4 transition-all duration-300',
59+
theme === 'system' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
60+
)}
4161
size={16}
4262
weight="duotone"
4363
/>

0 commit comments

Comments
 (0)