conflict-nuxt-4/app/plugins/themeLoader.client.ts
2026-02-12 11:24:27 +03:30

60 lines
1.6 KiB
TypeScript
Executable File

// themeLoader.client.ts
import majles from '~/assets/majles/theme.json'
import monir from '~/assets/monir/theme.json'
export const themes = {
majles,
monir
}
type ThemeKey = keyof typeof themes
type ThemeConfig = typeof themes[ThemeKey]
// تابع برای تبدیل HEX به RGB (بدون پرانتز و کاما)
function hexToRgb(hex: string): string {
// حذف # از ابتدا
hex = hex.replace('#', '');
// اگر کوتاه باشد (مثل #fff)
if (hex.length === 3) {
hex = hex.split('').map(char => char + char).join('');
}
// تبدیل به RGB
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
return `${r} ${g} ${b}`;
}
export default defineNuxtPlugin(() => {
const host = window.location.hostname.replace("www.", "");
let key: ThemeKey = "monir"; // default
if (host.includes("monir")) key = "monir";
else if (host.includes("majles")) key = "majles";
const theme: ThemeConfig = themes[key];
// ست کردن CSS Variables اصلی
Object.entries(theme).forEach(([key, value]) => {
if (typeof value === "string") {
document.documentElement.style.setProperty(`--theme-${key}`, value);
// تبدیل رنگ‌ها به RGB برای Tailwind
if (['primary', 'secondary', 'accent', 'background', 'text'].includes(key)) {
const rgb = hexToRgb(value);
document.documentElement.style.setProperty(`--color-${key}`, rgb);
}
}
});
return {
provide: {
themeKey: key,
theme,
},
};
});