perplexica/ui/components/theme/Switcher.tsx

73 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-05-24 10:20:15 +00:00
'use client';
import { useTheme } from 'next-themes';
import { SunIcon, MoonIcon, MonitorIcon } from 'lucide-react';
import { useCallback, useEffect, useState } from 'react';
import { cn } from '@/lib/utils';
2024-05-24 10:20:15 +00:00
type Theme = 'dark' | 'light' | 'system';
interface ThemeSwitcherProps {
size?: number | string;
className?: string;
}
export function ThemeSwitcher({ size, className }: ThemeSwitcherProps) {
2024-05-24 10:20:15 +00:00
const [mounted, setMounted] = useState(false);
const { theme, setTheme } = useTheme();
const isTheme = useCallback((t: Theme) => t === theme, [theme]);
const handleThemeSwitch = (theme: Theme) => {
setTheme(theme);
};
useEffect(() => {
setMounted(true);
}, []);
useEffect(() => {
if (isTheme('system')) {
const preferDarkScheme = window.matchMedia(
'(prefers-color-scheme: dark)',
);
const detectThemeChange = (event: MediaQueryListEvent) => {
const theme: Theme = event.matches ? 'dark' : 'light';
setTheme(theme);
};
preferDarkScheme.addEventListener('change', detectThemeChange);
return () => {
preferDarkScheme.removeEventListener('change', detectThemeChange);
};
}
}, [isTheme, setTheme, theme]);
// Avoid Hydration Mismatch
if (!mounted) {
return null;
}
return isTheme('dark') ? (
<SunIcon
className={cn('cursor-pointer', className)}
size={size}
2024-05-24 10:20:15 +00:00
onClick={() => handleThemeSwitch('light')}
/>
) : isTheme('light') ? (
<MoonIcon
className={cn('cursor-pointer', className)}
size={size}
2024-05-24 10:20:15 +00:00
onClick={() => handleThemeSwitch('dark')}
/>
) : (
<MonitorIcon
className={cn('cursor-pointer', className)}
size={size}
2024-05-24 10:20:15 +00:00
onClick={() => handleThemeSwitch('system')}
/>
);
}