ifixkart-admin/components/theme/ThemeCustomizer.tsx

325 lines
13 KiB
TypeScript

'use client';
import { useEffect, useState, type ReactNode } from 'react';
import { Settings, Sun, Moon, RotateCcw, X, Check } from 'lucide-react';
import { useTheme } from 'next-themes';
import {
DEFAULT_THEME,
LAYOUT_OPTIONS,
SIDEBAR_BACKGROUNDS,
SIDEBAR_COLORS,
SIDEBAR_SIZES,
THEME_COLORS,
TOPBAR_COLORS,
ThemeLayout,
ThemeSettings,
applyTheme,
clearAppliedTheme,
loadTheme,
saveTheme,
} from '@/lib/themeCustomizer';
interface ThemeCustomizerProps {
onChange?: (settings: ThemeSettings) => void;
}
function Section({ title, children }: { title: string; children: ReactNode }) {
return (
<section className="rounded-lg border border-border bg-card overflow-hidden">
<h3 className="px-3 py-2.5 text-[14px] font-semibold text-foreground border-b border-border">{title}</h3>
<div className="p-3">{children}</div>
</section>
);
}
function PreviewCard({
image,
label,
selected,
onClick,
}: {
image: string;
label: string;
selected: boolean;
onClick: () => void;
}) {
return (
<button type="button" onClick={onClick} className="text-center cursor-pointer">
<span className={`relative block rounded-md border-2 overflow-hidden bg-[#eef3f6] ${selected ? 'border-primary' : 'border-transparent'}`}>
<img src={image} alt="" className="w-full h-auto block" />
{selected && (
<span className="absolute top-1.5 right-1.5 w-4 h-4 rounded-full bg-primary text-white flex items-center justify-center">
<Check className="w-2.5 h-2.5" />
</span>
)}
</span>
<span className="mt-1.5 block text-[12px] leading-4 text-foreground">{label}</span>
</button>
);
}
function Swatch({
color,
selected,
label,
onClick,
}: {
color: string;
selected: boolean;
label: string;
onClick: () => void;
}) {
return (
<button
type="button"
title={label}
aria-label={label}
onClick={onClick}
className={`w-8 h-8 rounded-full border-2 cursor-pointer ${selected ? 'border-foreground' : 'border-transparent'}`}
style={{ background: color || 'var(--card)', boxShadow: 'inset 0 0 0 1px var(--border)' }}
/>
);
}
export function ThemeCustomizer({ onChange }: ThemeCustomizerProps) {
const { setTheme, resolvedTheme } = useTheme();
const [open, setOpen] = useState(false);
const [settings, setSettings] = useState<ThemeSettings>(DEFAULT_THEME);
const [ready, setReady] = useState(false);
useEffect(() => {
const saved = loadTheme();
const initial = saved
? { ...DEFAULT_THEME, ...saved }
: { ...DEFAULT_THEME, mode: resolvedTheme === 'dark' ? 'dark' as const : 'light' as const };
setSettings(initial);
if (saved) {
applyTheme(initial);
setTheme(initial.mode);
onChange?.(initial);
}
setReady(true);
// Apply a saved theme once. The current look stays until the user changes it.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (!ready || !resolvedTheme) return;
const mode = resolvedTheme === 'dark' ? 'dark' : 'light';
setSettings((current) => (current.mode === mode ? current : { ...current, mode }));
}, [ready, resolvedTheme]);
const update = (patch: Partial<ThemeSettings>) => {
const next = { ...settings, ...patch };
setSettings(next);
saveTheme(next);
applyTheme(next);
if (patch.mode) setTheme(patch.mode);
onChange?.(next);
};
const chooseLayout = (id: string) => {
if (id === 'rtl') {
update({ rtl: true });
return;
}
update({ layout: id as ThemeLayout, rtl: false });
};
const reset = () => {
localStorage.removeItem('ifixkart.themeCustomizer');
clearAppliedTheme();
setTheme('system');
setSettings(DEFAULT_THEME);
onChange?.(DEFAULT_THEME);
setOpen(false);
};
if (!ready) return null;
return (
<>
{!open && (
<button
type="button"
onClick={() => setOpen(true)}
className="fixed right-0 top-1/2 -translate-y-1/2 z-30 w-10 h-10 rounded-l-lg bg-primary text-white shadow-md flex items-center justify-center cursor-pointer"
aria-label="Open theme customizer"
>
<Settings className="w-4 h-4" />
</button>
)}
{open && (
<>
<button type="button" className="fixed inset-0 z-40 bg-black/40" aria-label="Close theme customizer" onClick={() => setOpen(false)} />
<aside className="fixed top-0 right-0 z-50 h-screen w-[380px] max-w-[92vw] bg-muted/40 border-l border-border shadow-xl flex flex-col">
<div className="px-4 py-3 bg-[#1b2559] text-white flex items-start justify-between gap-3">
<div>
<h2 className="text-[15px] font-semibold">Theme Customizer</h2>
<p className="text-[12px] text-white/70 mt-0.5">Choose your themes and layouts</p>
</div>
<button type="button" onClick={() => setOpen(false)} className="w-8 h-8 rounded-md text-white hover:bg-white/10 flex items-center justify-center cursor-pointer" aria-label="Close">
<X className="w-4 h-4" />
</button>
</div>
<div className="flex-1 overflow-y-auto p-3 space-y-3">
<Section title="Select Layouts">
<div className="grid grid-cols-3 gap-3">
{LAYOUT_OPTIONS.map((item) => (
<PreviewCard
key={item.id}
image={item.image}
label={item.label}
selected={item.id === 'rtl' ? settings.rtl : settings.layout === item.id}
onClick={() => chooseLayout(item.id)}
/>
))}
</div>
</Section>
<Section title="Sidebar Color">
<div className="flex flex-wrap items-center gap-2">
{SIDEBAR_COLORS.map((item) => (
<Swatch
key={item.id}
color={item.bg || 'var(--card)'}
label={item.label}
selected={settings.sidebarId === item.id}
onClick={() => update({ sidebarId: item.id })}
/>
))}
<label className="w-8 h-8 rounded-full border border-border overflow-hidden cursor-pointer" title="Custom sidebar">
<input
type="color"
value={settings.sidebarCustom}
onChange={(event) => update({ sidebarId: 'custom', sidebarCustom: event.target.value })}
className="w-10 h-10 -m-1 cursor-pointer border-0 bg-transparent"
aria-label="Custom sidebar color"
/>
</label>
</div>
</Section>
<Section title="Color Mode">
<div className="grid grid-cols-2 gap-2">
<button type="button" onClick={() => update({ mode: 'light' })} className={`h-14 px-3 rounded-lg border-2 text-[13px] font-medium inline-flex items-center gap-2 cursor-pointer ${settings.mode === 'light' ? 'border-primary bg-card' : 'border-border bg-card'}`}>
<span className="w-8 h-8 rounded-md bg-[#fff4d6] text-[#e0a100] inline-flex items-center justify-center"><Sun className="w-4 h-4" /></span>
Light Mode
</button>
<button type="button" onClick={() => update({ mode: 'dark' })} className={`h-14 px-3 rounded-lg border-2 text-[13px] font-medium inline-flex items-center gap-2 cursor-pointer ${settings.mode === 'dark' ? 'border-primary bg-card' : 'border-border bg-card'}`}>
<span className="w-8 h-8 rounded-md bg-muted text-foreground inline-flex items-center justify-center"><Moon className="w-4 h-4" /></span>
Dark Mode
</button>
</div>
</Section>
<Section title="Sidebar Size">
<div className="grid grid-cols-3 gap-3">
{SIDEBAR_SIZES.map((item) => (
<PreviewCard
key={item.id}
image={item.image}
label={item.label}
selected={settings.layout === item.id}
onClick={() => chooseLayout(item.id)}
/>
))}
</div>
</Section>
<Section title="Top Bar Color">
<div className="flex flex-wrap items-center gap-2">
{TOPBAR_COLORS.map((item) => (
<Swatch
key={item.id}
color={item.bg || 'var(--card)'}
label={item.label}
selected={settings.topbarId === item.id}
onClick={() => update({ topbarId: item.id })}
/>
))}
<label className="w-8 h-8 rounded-full border border-border overflow-hidden cursor-pointer" title="Custom top bar">
<input
type="color"
value={settings.topbarCustom}
onChange={(event) => update({ topbarId: 'custom', topbarCustom: event.target.value })}
className="w-10 h-10 -m-1 cursor-pointer border-0 bg-transparent"
aria-label="Custom top bar color"
/>
</label>
</div>
</Section>
<Section title="Sidebar Background">
<div className="flex flex-wrap gap-2">
{SIDEBAR_BACKGROUNDS.map((item) => (
<button
key={item.id}
type="button"
onClick={() => update({ sidebarBg: item.id })}
className={`relative w-14 h-14 rounded-md overflow-hidden border-2 cursor-pointer ${settings.sidebarBg === item.id ? 'border-primary' : 'border-transparent'}`}
aria-label={item.id}
>
<img src={item.image} alt="" className="w-full h-full object-cover" />
{settings.sidebarBg === item.id && (
<span className="absolute top-1 right-1 w-4 h-4 rounded-full bg-primary text-white flex items-center justify-center">
<Check className="w-2.5 h-2.5" />
</span>
)}
</button>
))}
</div>
</Section>
<Section title="Theme Colors">
<div className="flex flex-wrap items-center gap-2">
{THEME_COLORS.map((item) => (
<Swatch
key={item.id}
color={item.value}
label={item.label}
selected={settings.colorId === item.id}
onClick={() => update({ colorId: item.id, color: item.value })}
/>
))}
<label className="w-8 h-8 rounded-full border border-border overflow-hidden cursor-pointer" title="Custom color">
<input
type="color"
value={settings.color}
onChange={(event) => update({ colorId: 'custom', color: event.target.value })}
className="w-10 h-10 -m-1 cursor-pointer border-0 bg-transparent"
aria-label="Custom theme color"
/>
</label>
</div>
</Section>
<Section title="Layout Width">
<div className="grid grid-cols-2 gap-2">
{(['fluid', 'boxed'] as const).map((id) => (
<button
key={id}
type="button"
onClick={() => update({ width: id })}
className={`h-9 px-3 rounded-lg border text-[13px] font-semibold capitalize cursor-pointer ${settings.width === id ? 'border-primary bg-primary/10 text-primary' : 'border-border bg-card text-foreground'}`}
>
{id}
</button>
))}
</div>
</Section>
</div>
<div className="border-t border-border p-3 bg-card">
<button type="button" onClick={reset} className="w-full h-9 px-3 crm-radius-control border border-border bg-card text-foreground text-[13px] font-semibold inline-flex items-center justify-center gap-1.5 hover:bg-muted cursor-pointer">
<RotateCcw className="w-3.5 h-3.5" /> Reset
</button>
</div>
</aside>
</>
)}
</>
);
}