schon/storefront/app/plugins/02.theme.ts
2026-02-27 21:59:51 +03:00

26 lines
574 B
TypeScript

export default defineNuxtPlugin((nuxtApp) => {
const { $appHelpers } = nuxtApp;
const themeCookie = useCookie<'light' | 'dark'>($appHelpers.COOKIES_THEME_KEY, {
default: () => 'light',
path: '/',
});
const currentTheme = themeCookie.value || 'light';
if (import.meta.server) {
const event = useRequestEvent();
if (event) {
event.context.theme = currentTheme;
}
}
if (import.meta.client) {
const html = document.documentElement;
if (currentTheme === 'dark') {
html.classList.add('dark');
} else {
html.classList.remove('dark');
}
}
});