-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeToggle.jsx
More file actions
54 lines (48 loc) · 1.81 KB
/
Copy pathThemeToggle.jsx
File metadata and controls
54 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useEffect, useState } from 'react';
const STORAGE_KEY = 'vestal-theme';
function initialTheme() {
if (typeof document === 'undefined') return 'dark';
return document.documentElement.dataset.theme === 'light' ? 'light' : 'dark';
}
export default function ThemeToggle({ className = '' }) {
const [theme, setTheme] = useState(initialTheme);
useEffect(() => {
document.documentElement.dataset.theme = theme;
try {
localStorage.setItem(STORAGE_KEY, theme);
} catch {
/* storage unavailable — theme still applies for this session */
}
}, [theme]);
const dark = theme === 'dark';
return (
<button
type="button"
onClick={() => setTheme(dark ? 'light' : 'dark')}
className={`flex h-9 w-9 items-center justify-center rounded-md border border-line text-fog transition-colors hover:border-fog/60 hover:text-cream ${className}`}
aria-label={dark ? 'Switch to light theme' : 'Switch to dark theme'}
title={dark ? 'Switch to light theme' : 'Switch to dark theme'}
>
{dark ? (
<svg width="15" height="15" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<circle cx="8" cy="8" r="3.25" stroke="currentColor" strokeWidth="1.5" />
<path
d="M8 1v1.8M8 13.2V15M15 8h-1.8M2.8 8H1M12.95 3.05l-1.27 1.27M4.32 11.68l-1.27 1.27M12.95 12.95l-1.27-1.27M4.32 4.32 3.05 3.05"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
/>
</svg>
) : (
<svg width="15" height="15" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path
d="M13.5 9.5A5.5 5.5 0 0 1 6.5 2.5a5.5 5.5 0 1 0 7 7z"
stroke="currentColor"
strokeWidth="1.5"
strokeLinejoin="round"
/>
</svg>
)}
</button>
);
}