Spaces:
Running

Bot / frontend /src /shared /components /ThemeSelector.tsx
ddosvn's picture
Initial deploy of Panel ThTuanIT full stack and Lighthouse-optimized frontend
7e79d39 verified
Raw History Blame Contribute Delete
4.74 kB
'use client';
import React, { useEffect, useRef, useState } from 'react';
import { THEMES, ThemeId } from '@/shared/constants/themes';
import { useTheme } from '@/shared/hooks/useTheme';
import { Check, ChevronDown, Palette } from 'lucide-react';
interface ThemeSelectorProps {
align?: 'left' | 'right';
compact?: boolean;
}
export function ThemeSelector({ align = 'right', compact = false }: ThemeSelectorProps) {
const { theme, themeDef, setTheme } = useTheme();
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
const onPointerDown = (e: MouseEvent) => {
if (rootRef.current && !rootRef.current.contains(e.target as Node)) {
setOpen(false);
}
};
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') setOpen(false);
};
document.addEventListener('mousedown', onPointerDown);
document.addEventListener('keydown', onKeyDown);
return () => {
document.removeEventListener('mousedown', onPointerDown);
document.removeEventListener('keydown', onKeyDown);
};
}, [open]);
const handleSelect = (next: ThemeId) => {
setTheme(next);
setOpen(false);
};
return (
<div ref={rootRef} className="relative">
<button
type="button"
onClick={() => setOpen((prev) => !prev)}
aria-label="Chọn giao diện"
aria-expanded={open}
aria-haspopup="listbox"
className={`inline-flex items-center gap-2 rounded-xl border border-line bg-surface-1 hover:bg-surface-2 text-ink-1 text-xs font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent ${
compact ? 'min-h-[38px] px-2.5 py-1.5' : 'min-h-[42px] px-3 py-2'
}`}
>
<span
className="w-2.5 h-2.5 rounded-full flex-shrink-0 border border-line-strong"
style={{ backgroundColor: themeDef.swatchHex }}
aria-hidden="true"
/>
<Palette className="w-3.5 h-3.5 text-ink-2 sm:hidden" aria-hidden="true" />
<span className="hidden sm:inline">{themeDef.shortLabel}</span>
<ChevronDown
className={`w-3.5 h-3.5 text-ink-3 transition-transform ${open ? 'rotate-180' : ''}`}
aria-hidden="true"
/>
</button>
{open && (
<div
role="listbox"
aria-label="Danh sách giao diện"
className={`absolute top-full mt-2 w-64 rounded-2xl border border-line bg-surface-1 shadow-2xl p-1.5 z-50 ${
align === 'left' ? 'left-0' : 'right-0'
}`}
>
<div className="px-2.5 py-1.5 text-[11px] font-mono uppercase tracking-wider text-ink-3">
5 Giao diện hệ thống
</div>
<div className="space-y-1">
{THEMES.map((item) => {
const active = item.id === theme;
return (
<button
key={item.id}
type="button"
role="option"
aria-selected={active}
onClick={() => handleSelect(item.id)}
className={`w-full flex items-start gap-2.5 px-2.5 py-2 rounded-xl text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent ${
active
? 'bg-accent/10 border border-accent/40 text-ink-1'
: 'border border-transparent hover:bg-surface-2 text-ink-2'
}`}
>
<span
className="mt-0.5 w-6 h-6 rounded-lg border border-line-strong flex items-center justify-center flex-shrink-0"
style={{ backgroundColor: item.surfaceHex }}
aria-hidden="true"
>
<span
className="w-2.5 h-2.5 rounded-full"
style={{ backgroundColor: item.swatchHex }}
/>
</span>
<span className="flex-1 min-w-0">
<span className="flex items-center justify-between gap-2">
<span className="text-xs font-semibold text-ink-1 truncate">{item.name}</span>
{active && (
<Check className="w-3.5 h-3.5 text-accent flex-shrink-0" aria-hidden="true" />
)}
</span>
<span className="block text-[11px] text-ink-3 leading-snug mt-0.5">
{item.description}
</span>
</span>
</button>
);
})}
</div>
</div>
)}
</div>
);
}