@venix-sistemas/jade 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +350 -254
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +32 -35
  4. package/dist/runtime/components/VenixThemeSwitcher.d.vue.ts +1 -1
  5. package/dist/runtime/components/VenixThemeSwitcher.vue +242 -99
  6. package/dist/runtime/components/VenixThemeSwitcher.vue.d.ts +1 -1
  7. package/dist/runtime/composables/useThemeColors.d.ts +6 -0
  8. package/dist/runtime/composables/useThemeColors.js +24 -9
  9. package/dist/runtime/composables/useThemeCookies.d.ts +9 -2
  10. package/dist/runtime/composables/useThemeCookies.js +24 -6
  11. package/dist/runtime/composables/useThemeLocale.d.ts +5 -0
  12. package/dist/runtime/composables/useThemeLocale.js +12 -18
  13. package/dist/runtime/composables/useThemeSeasonal.d.ts +2 -2
  14. package/dist/runtime/composables/useThemeSeasonal.js +1 -1
  15. package/dist/runtime/composables/useVenixTheme.d.ts +20 -0
  16. package/dist/runtime/composables/useVenixTheme.js +174 -112
  17. package/dist/runtime/nitro/theme-init.js +8 -5
  18. package/dist/runtime/plugins/theme-init.server.js +7 -12
  19. package/dist/runtime/plugins/vuetify-theme.js +8 -11
  20. package/dist/runtime/scripts/theme-init.d.ts +23 -10
  21. package/dist/runtime/scripts/theme-init.js +42 -62
  22. package/dist/shared/constants.d.ts +18 -3
  23. package/dist/shared/constants.js +6 -1
  24. package/dist/shared/css/colors.js +2 -2
  25. package/dist/shared/css/cursor.js +9 -2
  26. package/dist/shared/css/index.d.ts +1 -7
  27. package/dist/shared/css/index.js +9 -9
  28. package/dist/shared/css/transition.d.ts +1 -1
  29. package/dist/shared/theme.json +302 -192
  30. package/dist/shared/types/color-options.d.ts +26 -2
  31. package/dist/shared/types/colors.d.ts +64 -7
  32. package/dist/shared/types/index.d.ts +1 -1
  33. package/dist/shared/types/runtime-config.d.ts +4 -2
  34. package/dist/shared/utils/icon-refs.d.ts +7 -3
  35. package/dist/shared/utils/icon-refs.js +1 -1
  36. package/dist/shared/utils/icon.d.ts +6 -2
  37. package/dist/shared/utils/icon.js +8 -3
  38. package/dist/shared/utils/index.d.ts +5 -3
  39. package/dist/shared/utils/index.js +12 -5
  40. package/dist/shared/utils/inline-script.d.ts +6 -0
  41. package/dist/shared/utils/inline-script.js +3 -0
  42. package/dist/shared/utils/load.d.ts +0 -1
  43. package/dist/shared/utils/load.js +7 -16
  44. package/dist/shared/utils/merge-themes.d.ts +41 -0
  45. package/dist/shared/utils/merge-themes.js +106 -0
  46. package/dist/shared/utils/normalize.d.ts +0 -10
  47. package/dist/shared/utils/normalize.js +0 -12
  48. package/dist/shared/utils/theme-config.d.ts +13 -4
  49. package/dist/shared/utils/theme-config.js +28 -13
  50. package/dist/shared/utils/theme-resolve.d.ts +19 -19
  51. package/dist/shared/utils/theme-resolve.js +25 -42
  52. package/dist/shared/utils/theme-resolver.d.ts +46 -0
  53. package/dist/shared/utils/theme-resolver.js +139 -0
  54. package/package.json +13 -10
  55. package/README-default.md +0 -84
  56. package/dist/runtime/plugin.d.ts +0 -2
  57. package/dist/runtime/plugin.js +0 -3
@@ -2,31 +2,49 @@ import { useCookie } from "#app";
2
2
  import {
3
3
  THEME_PREFERENCE_COOKIE,
4
4
  THEME_RESOLVED_COOKIE,
5
+ THEME_MODE_COOKIE,
6
+ THEME_COOKIE_OPTIONS,
5
7
  DEFAULT_LOCALE_COOKIE_NAME
6
8
  } from "../../shared/constants";
7
9
  import { hasCookieConsent } from "../../shared/utils/consent";
8
- export const useThemeCookies = (localeCookieName = DEFAULT_LOCALE_COOKIE_NAME) => {
9
- const preferenceCookie = useCookie(THEME_PREFERENCE_COOKIE);
10
- const resolvedCookie = useCookie(THEME_RESOLVED_COOKIE);
11
- const localeCookie = useCookie(localeCookieName);
10
+ let warnedMissingConsent = false;
11
+ export const useThemeCookies = (options = {}) => {
12
+ const { localeCookieName = DEFAULT_LOCALE_COOKIE_NAME, requireConsent = true } = options;
13
+ const preferenceCookie = useCookie(THEME_PREFERENCE_COOKIE, THEME_COOKIE_OPTIONS);
14
+ const resolvedCookie = useCookie(THEME_RESOLVED_COOKIE, THEME_COOKIE_OPTIONS);
15
+ const modeCookie = useCookie(THEME_MODE_COOKIE, THEME_COOKIE_OPTIONS);
16
+ const localeCookie = useCookie(localeCookieName, THEME_COOKIE_OPTIONS);
12
17
  const removeCookie = (name) => {
13
18
  if (typeof document !== "undefined") {
14
19
  document.cookie = `${name}=; path=/; max-age=0; SameSite=Lax`;
15
20
  }
16
21
  };
17
- const persistIfConsented = (preference, resolved) => {
18
- if (!hasCookieConsent()) return;
22
+ const canPersist = () => !requireConsent || hasCookieConsent();
23
+ const persistIfConsented = (preference, mode, resolved) => {
24
+ if (!canPersist()) {
25
+ if (import.meta.dev && import.meta.client && !warnedMissingConsent) {
26
+ warnedMissingConsent = true;
27
+ console.warn(
28
+ "[jade] Theme change not saved: no cookie consent recorded, so it will be lost on reload. Call `persistence.grant()`, record consent from your cookie banner, or set `venixTheme.color.requireConsent: false` if this app has no consent banner."
29
+ );
30
+ }
31
+ return;
32
+ }
19
33
  preferenceCookie.value = preference;
34
+ modeCookie.value = mode;
20
35
  resolvedCookie.value = resolved;
21
36
  };
22
37
  const disablePersistence = () => {
23
38
  removeCookie(THEME_PREFERENCE_COOKIE);
24
39
  removeCookie(THEME_RESOLVED_COOKIE);
40
+ removeCookie(THEME_MODE_COOKIE);
25
41
  };
26
42
  return {
27
43
  preferenceCookie,
28
44
  resolvedCookie,
45
+ modeCookie,
29
46
  localeCookie,
47
+ canPersist,
30
48
  persistIfConsented,
31
49
  disablePersistence
32
50
  };
@@ -4,6 +4,11 @@ export interface UseThemeLocaleOptions {
4
4
  enabled?: boolean;
5
5
  forcedLocale?: string;
6
6
  defaultLocale?: string;
7
+ /**
8
+ * Whether the locale cookie may be written — `useThemeCookies().canPersist`,
9
+ * so it honors `color.requireConsent` like the theme cookies.
10
+ */
11
+ canPersist?: () => boolean;
7
12
  }
8
13
  export declare const useThemeLocale: (theme: ThemeConfig, localeCookie: Ref<string | undefined>, options?: UseThemeLocaleOptions) => {
9
14
  currentLocale: Ref<string, string>;
@@ -2,30 +2,23 @@ import { watch } from "vue";
2
2
  import { useRequestHeaders, useState, useRoute } from "#app";
3
3
  import { DEFAULT_LOCALE } from "../../shared/constants";
4
4
  import { normalizeLocale, extractFirstLocale } from "../../shared/utils/normalize";
5
+ import { extractLocales } from "../../shared/utils/load";
5
6
  import { hasCookieConsent } from "../../shared/utils/consent";
6
7
  export const useThemeLocale = (theme, localeCookie, options = {}) => {
7
- const { enabled = true, forcedLocale, defaultLocale } = options;
8
+ const { enabled = true, forcedLocale, defaultLocale, canPersist = hasCookieConsent } = options;
8
9
  const headers = useRequestHeaders(["accept-language"]);
9
10
  const route = useRoute();
10
11
  const fallbackLocale = defaultLocale || DEFAULT_LOCALE;
11
12
  const detectLocale = () => {
12
- const getAvailableLocales = () => {
13
- const availableLocales = /* @__PURE__ */ new Set();
14
- Object.values(theme.colors.themes).forEach((themeConfig) => {
15
- if (themeConfig.translations) {
16
- Object.keys(themeConfig.translations).forEach((locale) => {
17
- availableLocales.add(normalizeLocale(locale));
18
- });
19
- }
20
- });
21
- return Array.from(availableLocales);
22
- };
13
+ const availableLocales = new Set(extractLocales(theme));
23
14
  const findLocaleInAvailable = (requestedLocale) => {
24
- const availableLocales = getAvailableLocales();
25
15
  const normalized = normalizeLocale(requestedLocale);
26
- if (availableLocales.includes(normalized)) return normalized;
16
+ if (availableLocales.has(normalized)) return normalized;
27
17
  const baseLocale = normalized.split("-")[0];
28
- return availableLocales.find((locale) => locale.split("-")[0] === baseLocale) || null;
18
+ for (const locale of availableLocales) {
19
+ if (locale.split("-")[0] === baseLocale) return locale;
20
+ }
21
+ return null;
29
22
  };
30
23
  const findBestLocale = (requestedLocale) => findLocaleInAvailable(requestedLocale) || fallbackLocale;
31
24
  if (forcedLocale) return findBestLocale(forcedLocale);
@@ -74,9 +67,10 @@ export const useThemeLocale = (theme, localeCookie, options = {}) => {
74
67
  return fallback || "";
75
68
  };
76
69
  const setLocale = (locale) => {
77
- currentLocale.value = normalizeLocale(locale);
78
- if (enabled && !forcedLocale && hasCookieConsent()) {
79
- localeCookie.value = normalizeLocale(locale);
70
+ const normalized = normalizeLocale(locale);
71
+ currentLocale.value = normalized;
72
+ if (enabled && !forcedLocale && canPersist()) {
73
+ localeCookie.value = normalized;
80
74
  }
81
75
  };
82
76
  return {
@@ -1,4 +1,4 @@
1
- import type { ThemeConfig } from '../../shared/types/index.js';
1
+ import type { ThemeConfig, ThemeMode } from '../../shared/types/index.js';
2
2
  export declare const useThemeSeasonal: (theme: ThemeConfig) => {
3
- getActiveSeasonalTheme: (prefersDark?: boolean) => string | null;
3
+ getActiveSeasonalTheme: (mode?: ThemeMode) => string | null;
4
4
  };
@@ -1,6 +1,6 @@
1
1
  import { getActiveSeasonalTheme } from "../../shared/utils/theme-resolve";
2
2
  export const useThemeSeasonal = (theme) => {
3
- const getActiveSeasonalThemeFor = (prefersDark) => getActiveSeasonalTheme(theme.colors.themes, prefersDark);
3
+ const getActiveSeasonalThemeFor = (mode) => getActiveSeasonalTheme(theme.colors.groups, mode);
4
4
  return {
5
5
  getActiveSeasonalTheme: getActiveSeasonalThemeFor
6
6
  };
@@ -1,3 +1,4 @@
1
+ import type { ThemeMode, ThemeModePreference } from '../../shared/types/index.js';
1
2
  export declare const useVenixTheme: () => {
2
3
  theme: {
3
4
  value: import("vue").ComputedRef<string>;
@@ -6,7 +7,9 @@ export declare const useVenixTheme: () => {
6
7
  value: string;
7
8
  name: string;
8
9
  icon: import("../../shared/utils/icon.js").ResolvedThemeIcon | null;
10
+ custom: boolean;
9
11
  } | undefined>;
12
+ set: (name: string) => void;
10
13
  toggle: (forceTheme?: string) => void;
11
14
  isSeasonalActive: import("vue").ComputedRef<boolean>;
12
15
  activeSeasonalTheme: import("vue").ComputedRef<string | null>;
@@ -17,6 +20,23 @@ export declare const useVenixTheme: () => {
17
20
  value: string;
18
21
  name: string;
19
22
  icon: import("../../shared/utils/icon.js").ResolvedThemeIcon | null;
23
+ custom: boolean;
24
+ }[]>;
25
+ mode: {
26
+ value: import("vue").ComputedRef<ThemeMode>;
27
+ preference: import("vue").Ref<ThemeModePreference, ThemeModePreference>;
28
+ data: import("vue").ComputedRef<{
29
+ value: "light" | "dark" | "system";
30
+ name: string;
31
+ icon: import("../../shared/utils/icon.js").ResolvedThemeIcon | null;
32
+ } | undefined>;
33
+ set: (mode: ThemeModePreference) => void;
34
+ toggle: () => void;
35
+ };
36
+ modes: import("vue").ComputedRef<{
37
+ value: "light" | "dark" | "system";
38
+ name: string;
39
+ icon: import("../../shared/utils/icon.js").ResolvedThemeIcon | null;
20
40
  }[]>;
21
41
  locale: {
22
42
  current: import("vue").Ref<string, string>;
@@ -1,20 +1,22 @@
1
- import { computed, watch, onMounted, onUnmounted, getCurrentInstance } from "vue";
2
- import { useRuntimeConfig, useState } from "#app";
3
- import { DEFAULT_THEME, DEFAULT_LOCALE, COOKIE_PREFERENCES_UPDATED_EVENT, COOKIE_CONSENT_STORAGE_KEY, THEME_APPLIED_EVENT } from "../../shared/constants";
1
+ import { computed, effectScope, watch } from "vue";
2
+ import { onNuxtReady, useNuxtApp, useRuntimeConfig, useState } from "#app";
3
+ import { DEFAULT_LOCALE, COOKIE_PREFERENCES_UPDATED_EVENT, COOKIE_CONSENT_STORAGE_KEY, THEME_APPLIED_EVENT, SYSTEM_THEME } from "../../shared/constants";
4
4
  import { useThemeCookies } from "./useThemeCookies.js";
5
5
  import { useThemeLocale } from "./useThemeLocale.js";
6
6
  import { useThemeSeasonal } from "./useThemeSeasonal.js";
7
7
  import { useThemeColors } from "./useThemeColors.js";
8
8
  import { hasCookieConsent } from "../../shared/utils/consent";
9
9
  import { buildResolvedTheme } from "../../shared/utils/theme-config";
10
- import { resolveThemePreference } from "../../shared/utils/theme-resolve";
11
- export const useVenixTheme = () => {
10
+ import { findVariantOwner, isModePreference, isSelectableGroup, normalizeThemeSelection, resolveMode, resolveThemeSelection } from "../../shared/utils/theme-resolve";
11
+ function createThemeCore() {
12
12
  const config = useRuntimeConfig();
13
13
  const themeConfig = config.public.venixTheme;
14
14
  const theme = buildResolvedTheme(themeConfig);
15
- const defaultTheme = theme.colors.defaultColor || DEFAULT_THEME;
16
15
  const shouldApplyColors = themeConfig?.applyColors !== false && theme.colors.defaults !== false;
17
- const cookies = useThemeCookies(themeConfig?.localeCookie);
16
+ const cookies = useThemeCookies({
17
+ localeCookieName: themeConfig?.localeCookie,
18
+ requireConsent: themeConfig?.requireConsent !== false
19
+ });
18
20
  const hasConsent = useState(
19
21
  "venix-cookie-consent",
20
22
  () => typeof window !== "undefined" && hasCookieConsent()
@@ -22,27 +24,22 @@ export const useVenixTheme = () => {
22
24
  const locale = useThemeLocale(theme, cookies.localeCookie, {
23
25
  enabled: themeConfig?.enabled?.translation !== false,
24
26
  forcedLocale: themeConfig?.locale,
25
- defaultLocale: themeConfig?.defaultLocale || DEFAULT_LOCALE
27
+ defaultLocale: themeConfig?.defaultLocale || DEFAULT_LOCALE,
28
+ canPersist: cookies.canPersist
26
29
  });
27
- if (themeConfig?.enabled?.manageHtmlLang) {
28
- if (typeof document !== "undefined") {
29
- document.documentElement.lang = locale.currentLocale.value;
30
- }
31
- watch(locale.currentLocale, (newLocale) => {
32
- if (typeof document !== "undefined") document.documentElement.lang = newLocale;
33
- });
34
- }
35
30
  const seasonal = useThemeSeasonal(theme);
36
31
  const colors = useThemeColors(theme, locale.translate, themeConfig?.iconFormat || "svg");
37
- const initialPreference = cookies.preferenceCookie.value || defaultTheme;
38
- const preference = useState("venix-theme-preference", () => initialPreference || "system");
39
- const getResolvedTheme = (pref) => {
40
- const prefersDark = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)").matches : true;
41
- return resolveThemePreference(pref, theme.colors.themes, defaultTheme, prefersDark);
42
- };
43
- const apply = (pref) => {
32
+ const readCookieSelection = () => normalizeThemeSelection(cookies.preferenceCookie.value, cookies.modeCookie.value, theme.colors);
33
+ const initialSelection = readCookieSelection();
34
+ const preference = useState("venix-theme-preference", () => initialSelection.theme);
35
+ const modePreference = useState("venix-theme-mode", () => initialSelection.mode);
36
+ const currentSelection = () => ({ theme: preference.value, mode: modePreference.value });
37
+ const environmentVersion = useState("venix-theme-environment", () => 0);
38
+ const getPrefersDark = () => typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)").matches : true;
39
+ const getResolvedTheme = (selection = currentSelection()) => resolveThemeSelection(selection, theme.colors, getPrefersDark());
40
+ const apply = (selection) => {
44
41
  if (!shouldApplyColors) return;
45
- const resolved = getResolvedTheme(pref);
42
+ const resolved = getResolvedTheme(selection);
46
43
  if (typeof document !== "undefined") {
47
44
  const html = document.documentElement;
48
45
  html.classList.remove(...colors.themeValues.value);
@@ -52,10 +49,10 @@ export const useVenixTheme = () => {
52
49
  window.dispatchEvent(new CustomEvent(THEME_APPLIED_EVENT, { detail: resolved }));
53
50
  }
54
51
  };
55
- const applyWithTransition = (pref) => {
52
+ const applyWithTransition = (selection) => {
56
53
  const canAnimate = typeof document !== "undefined" && typeof document.startViewTransition === "function" && !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
57
54
  if (!canAnimate) {
58
- apply(pref);
55
+ apply(selection);
59
56
  return;
60
57
  }
61
58
  const body = document.body;
@@ -65,122 +62,187 @@ export const useVenixTheme = () => {
65
62
  body.style.setProperty("cursor", currentCursor, "important");
66
63
  }
67
64
  const restoreCursor = () => body.style.removeProperty("cursor");
68
- const transition = document.startViewTransition(() => apply(pref));
65
+ const transition = document.startViewTransition(() => apply(selection));
69
66
  transition.ready.catch(() => {
70
67
  });
71
68
  transition.finished.then(restoreCursor).catch(restoreCursor);
72
69
  };
73
- if (typeof document !== "undefined" && shouldApplyColors) {
74
- if (cookies.preferenceCookie.value && cookies.preferenceCookie.value !== preference.value) {
75
- preference.value = cookies.preferenceCookie.value;
76
- }
77
- const currentTheme = document.documentElement.getAttribute("data-theme");
78
- if (currentTheme) {
79
- if (!cookies.preferenceCookie.value) {
80
- preference.value = currentTheme;
81
- }
82
- } else {
83
- apply(preference.value);
84
- }
85
- }
86
70
  const syncPersistence = () => {
87
71
  hasConsent.value = hasCookieConsent();
88
- if (hasConsent.value) {
89
- cookies.persistIfConsented(preference.value, getResolvedTheme(preference.value));
72
+ if (cookies.canPersist()) {
73
+ cookies.persistIfConsented(preference.value, modePreference.value, getResolvedTheme());
90
74
  } else {
91
75
  cookies.disablePersistence();
92
76
  }
93
77
  };
94
- const grantPersistence = () => {
78
+ const setConsent = (granted) => {
95
79
  if (typeof localStorage === "undefined") return;
96
- localStorage.setItem(COOKIE_CONSENT_STORAGE_KEY, JSON.stringify({ functionality: true }));
80
+ localStorage.setItem(COOKIE_CONSENT_STORAGE_KEY, JSON.stringify({ functionality: granted }));
97
81
  syncPersistence();
98
82
  window.dispatchEvent(new Event(COOKIE_PREFERENCES_UPDATED_EVENT));
99
83
  };
100
- const revokePersistence = () => {
101
- if (typeof localStorage === "undefined") return;
102
- localStorage.setItem(COOKIE_CONSENT_STORAGE_KEY, JSON.stringify({ functionality: false }));
103
- syncPersistence();
104
- window.dispatchEvent(new Event(COOKIE_PREFERENCES_UPDATED_EVENT));
84
+ const setTheme = (name) => {
85
+ if (name === SYSTEM_THEME || isSelectableGroup(theme.colors, name)) preference.value = name;
105
86
  };
106
- const instance = getCurrentInstance();
107
- const runOnClientMount = (fn) => {
108
- if (instance) {
109
- onMounted(fn);
110
- } else if (typeof window !== "undefined") {
111
- queueMicrotask(fn);
112
- }
87
+ const setMode = (mode) => {
88
+ if (isModePreference(mode)) modePreference.value = mode;
113
89
  };
114
- runOnClientMount(() => {
115
- hasConsent.value = hasCookieConsent();
116
- if (!shouldApplyColors) return;
117
- const mq = window.matchMedia("(prefers-color-scheme: dark)");
118
- const handleSystemThemeChange = () => {
119
- if (preference.value === "system") applyWithTransition("system");
120
- };
121
- mq.addEventListener("change", handleSystemThemeChange);
122
- const checkSeasonalChange = () => {
123
- const now = /* @__PURE__ */ new Date();
124
- const msToMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime() - now.getTime();
125
- return setTimeout(() => {
126
- if (preference.value === "system") applyWithTransition("system");
127
- checkSeasonalChange();
128
- }, msToMidnight + 1e3);
129
- };
130
- const midnightTimeout = checkSeasonalChange();
131
- window.addEventListener(COOKIE_PREFERENCES_UPDATED_EVENT, syncPersistence);
132
- if (instance) {
133
- onUnmounted(() => {
134
- mq.removeEventListener("change", handleSystemThemeChange);
135
- clearTimeout(midnightTimeout);
136
- window.removeEventListener(COOKIE_PREFERENCES_UPDATED_EVENT, syncPersistence);
137
- });
138
- }
139
- });
140
- watch(preference, (newPref) => {
141
- if (!shouldApplyColors) return;
142
- applyWithTransition(newPref);
143
- cookies.persistIfConsented(newPref, getResolvedTheme(newPref));
90
+ const resolvedTheme = computed(() => {
91
+ void environmentVersion.value;
92
+ return getResolvedTheme(currentSelection());
144
93
  });
94
+ const resolvedMode = computed(
95
+ () => theme.colors.themes[resolvedTheme.value]?.dark === false ? "light" : "dark"
96
+ );
97
+ const toggleMode = () => {
98
+ modePreference.value = resolvedMode.value === "dark" ? "light" : "dark";
99
+ };
145
100
  const toggle = (forceTheme) => {
146
- const newTheme = forceTheme ?? (preference.value === "light" ? "dark" : "light");
147
- preference.value = newTheme;
148
- cookies.persistIfConsented(newTheme, getResolvedTheme(newTheme));
101
+ if (forceTheme === void 0) {
102
+ toggleMode();
103
+ return;
104
+ }
105
+ if (forceTheme === SYSTEM_THEME) {
106
+ preference.value = SYSTEM_THEME;
107
+ modePreference.value = "system";
108
+ return;
109
+ }
110
+ if (isSelectableGroup(theme.colors, forceTheme)) {
111
+ preference.value = forceTheme;
112
+ return;
113
+ }
114
+ const owner = findVariantOwner(theme.colors, forceTheme);
115
+ if (owner) {
116
+ preference.value = owner.theme;
117
+ modePreference.value = owner.mode;
118
+ }
149
119
  };
150
120
  const data = computed(
151
121
  () => colors.themes.value.find((t) => t.value === preference.value) ?? colors.themes.value[0]
152
122
  );
153
- const isSeasonalActive = computed(() => {
154
- if (preference.value !== "system") return false;
155
- return seasonal.getActiveSeasonalTheme() !== null;
156
- });
123
+ const modeData = computed(
124
+ () => colors.modes.value.find((m) => m.value === modePreference.value) ?? colors.modes.value[0]
125
+ );
157
126
  const activeSeasonalTheme = computed(() => {
158
- if (preference.value !== "system") return null;
159
- return seasonal.getActiveSeasonalTheme();
127
+ void environmentVersion.value;
128
+ if (preference.value !== SYSTEM_THEME) return null;
129
+ return seasonal.getActiveSeasonalTheme(resolveMode(modePreference.value, getPrefersDark()));
130
+ });
131
+ const isSeasonalActive = computed(() => activeSeasonalTheme.value !== null);
132
+ return {
133
+ themeConfig,
134
+ theme,
135
+ shouldApplyColors,
136
+ cookies,
137
+ hasConsent,
138
+ locale,
139
+ colors,
140
+ preference,
141
+ modePreference,
142
+ environmentVersion,
143
+ readCookieSelection,
144
+ currentSelection,
145
+ getResolvedTheme,
146
+ apply,
147
+ applyWithTransition,
148
+ syncPersistence,
149
+ setConsent,
150
+ setTheme,
151
+ setMode,
152
+ resolvedTheme,
153
+ resolvedMode,
154
+ toggleMode,
155
+ toggle,
156
+ data,
157
+ modeData,
158
+ activeSeasonalTheme,
159
+ isSeasonalActive
160
+ };
161
+ }
162
+ function installThemeEffects(core) {
163
+ const { themeConfig, shouldApplyColors, cookies, hasConsent, locale, preference, modePreference, environmentVersion } = core;
164
+ if (themeConfig?.enabled?.manageHtmlLang) {
165
+ document.documentElement.lang = locale.currentLocale.value;
166
+ watch(locale.currentLocale, (newLocale) => {
167
+ document.documentElement.lang = newLocale;
168
+ });
169
+ }
170
+ if (shouldApplyColors) {
171
+ if (cookies.preferenceCookie.value || cookies.modeCookie.value) {
172
+ const fromCookies = core.readCookieSelection();
173
+ if (fromCookies.theme !== preference.value) preference.value = fromCookies.theme;
174
+ if (fromCookies.mode !== modePreference.value) modePreference.value = fromCookies.mode;
175
+ }
176
+ if (!document.documentElement.getAttribute("data-theme")) {
177
+ core.apply(core.currentSelection());
178
+ }
179
+ watch([preference, modePreference], () => {
180
+ const selection = core.currentSelection();
181
+ core.applyWithTransition(selection);
182
+ cookies.persistIfConsented(selection.theme, selection.mode, core.getResolvedTheme(selection));
183
+ });
184
+ }
185
+ onNuxtReady(() => {
186
+ hasConsent.value = hasCookieConsent();
187
+ window.addEventListener(COOKIE_PREFERENCES_UPDATED_EVENT, core.syncPersistence);
188
+ if (!shouldApplyColors) return;
189
+ window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
190
+ environmentVersion.value++;
191
+ if (modePreference.value === "system") core.applyWithTransition(core.currentSelection());
192
+ });
193
+ const scheduleSeasonalCheck = () => {
194
+ const now = /* @__PURE__ */ new Date();
195
+ const msToMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime() - now.getTime();
196
+ setTimeout(() => {
197
+ environmentVersion.value++;
198
+ if (preference.value === SYSTEM_THEME) core.applyWithTransition(core.currentSelection());
199
+ scheduleSeasonalCheck();
200
+ }, msToMidnight + 1e3);
201
+ };
202
+ scheduleSeasonalCheck();
160
203
  });
204
+ }
205
+ export const useVenixTheme = () => {
206
+ const core = createThemeCore();
207
+ if (import.meta.client) {
208
+ const nuxtApp = useNuxtApp();
209
+ if (!nuxtApp._venixThemeEffects) {
210
+ nuxtApp._venixThemeEffects = true;
211
+ effectScope(true).run(() => installThemeEffects(createThemeCore()));
212
+ }
213
+ }
161
214
  return {
162
215
  theme: {
163
- value: computed(() => getResolvedTheme(preference.value)),
164
- preference,
165
- data,
166
- toggle,
167
- isSeasonalActive,
168
- activeSeasonalTheme,
169
- shouldApplyColors,
170
- colors: theme.colors
216
+ value: core.resolvedTheme,
217
+ preference: core.preference,
218
+ data: core.data,
219
+ set: core.setTheme,
220
+ toggle: core.toggle,
221
+ isSeasonalActive: core.isSeasonalActive,
222
+ activeSeasonalTheme: core.activeSeasonalTheme,
223
+ shouldApplyColors: core.shouldApplyColors,
224
+ colors: core.theme.colors
225
+ },
226
+ themes: core.colors.themes,
227
+ mode: {
228
+ value: core.resolvedMode,
229
+ preference: core.modePreference,
230
+ data: core.modeData,
231
+ set: core.setMode,
232
+ toggle: core.toggleMode
171
233
  },
172
- themes: colors.themes,
234
+ modes: core.colors.modes,
173
235
  locale: {
174
- current: locale.currentLocale,
175
- set: locale.setLocale,
176
- translate: locale.translate
236
+ current: core.locale.currentLocale,
237
+ set: core.locale.setLocale,
238
+ translate: core.locale.translate
177
239
  },
178
240
  persistence: {
179
- hasConsent,
180
- grant: grantPersistence,
181
- revoke: revokePersistence,
182
- enable: () => cookies.persistIfConsented(preference.value, getResolvedTheme(preference.value)),
183
- disable: cookies.disablePersistence
241
+ hasConsent: core.hasConsent,
242
+ grant: () => core.setConsent(true),
243
+ revoke: () => core.setConsent(false),
244
+ enable: () => core.cookies.persistIfConsented(core.preference.value, core.modePreference.value, core.getResolvedTheme()),
245
+ disable: core.cookies.disablePersistence
184
246
  }
185
247
  };
186
248
  };
@@ -1,11 +1,14 @@
1
1
  import { defineNitroPlugin, useRuntimeConfig } from "nitropack/runtime";
2
- import initTheme from "../scripts/theme-init.js";
2
+ import { createInitThemeOptions, renderInitThemeScript } from "../scripts/theme-init.js";
3
3
  import { buildResolvedTheme } from "../../shared/utils/theme-config";
4
4
  export default defineNitroPlugin((nitroApp) => {
5
- const theme = buildResolvedTheme(useRuntimeConfig().public.venixTheme);
6
- const inlineScript = `<script>(${initTheme.toString()})(${JSON.stringify(theme)})<\/script>`;
5
+ const themeConfig = useRuntimeConfig().public.venixTheme;
6
+ const theme = buildResolvedTheme(themeConfig);
7
+ const scriptBody = renderInitThemeScript(theme, createInitThemeOptions(themeConfig?.requireConsent !== false));
7
8
  const hookRenderHtml = nitroApp.hooks.hook;
8
- hookRenderHtml("render:html", (html) => {
9
- html.head.unshift(inlineScript);
9
+ hookRenderHtml("render:html", (html, { event }) => {
10
+ const nonce = event?.context?.security?.nonce;
11
+ const nonceAttr = nonce ? ` nonce="${nonce.replace(/"/g, "&quot;")}"` : "";
12
+ html.head.unshift(`<script${nonceAttr}>${scriptBody}<\/script>`);
10
13
  });
11
14
  });
@@ -1,30 +1,25 @@
1
1
  import { defineNuxtPlugin, useRuntimeConfig, useCookie, useHead } from "#app";
2
- import { THEME_PREFERENCE_COOKIE, THEME_RESOLVED_COOKIE, DEFAULT_LOCALE_COOKIE_NAME, DEFAULT_LOCALE, DEFAULT_THEME } from "../../shared/constants";
2
+ import { THEME_PREFERENCE_COOKIE, THEME_RESOLVED_COOKIE, THEME_MODE_COOKIE, THEME_COOKIE_OPTIONS, DEFAULT_LOCALE_COOKIE_NAME, DEFAULT_LOCALE } from "../../shared/constants";
3
3
  import { useThemeLocale } from "../composables/useThemeLocale.js";
4
4
  import { buildResolvedTheme } from "../../shared/utils/theme-config";
5
- import { isColorTheme, resolveThemePreference } from "../../shared/utils/theme-resolve";
5
+ import { resolveServerTheme } from "../../shared/utils/theme-resolve";
6
6
  export default defineNuxtPlugin({
7
7
  name: "venix-theme-init-server",
8
8
  enforce: "pre",
9
9
  setup() {
10
10
  const config = useRuntimeConfig();
11
11
  const themeConfig = config.public.venixTheme;
12
- const resolvedCookie = useCookie(THEME_RESOLVED_COOKIE);
13
- const preferenceCookie = useCookie(THEME_PREFERENCE_COOKIE);
12
+ const resolvedCookie = useCookie(THEME_RESOLVED_COOKIE, THEME_COOKIE_OPTIONS);
13
+ const preferenceCookie = useCookie(THEME_PREFERENCE_COOKIE, THEME_COOKIE_OPTIONS);
14
+ const modeCookie = useCookie(THEME_MODE_COOKIE, THEME_COOKIE_OPTIONS);
14
15
  const theme = buildResolvedTheme(themeConfig);
15
- const defaultTheme = theme.colors.defaultColor || DEFAULT_THEME;
16
- let resolvedTheme = defaultTheme;
17
- if (resolvedCookie.value && isColorTheme(theme.colors.themes[resolvedCookie.value])) {
18
- resolvedTheme = resolvedCookie.value;
19
- } else if (preferenceCookie.value) {
20
- resolvedTheme = resolveThemePreference(preferenceCookie.value, theme.colors.themes, defaultTheme, true);
21
- }
16
+ const resolvedTheme = resolveServerTheme(preferenceCookie.value, modeCookie.value, resolvedCookie.value, theme.colors);
22
17
  const htmlAttrs = {
23
18
  "data-theme": resolvedTheme,
24
19
  "class": resolvedTheme
25
20
  };
26
21
  if (themeConfig?.enabled?.manageHtmlLang) {
27
- const localeCookie = useCookie(themeConfig?.localeCookie || DEFAULT_LOCALE_COOKIE_NAME);
22
+ const localeCookie = useCookie(themeConfig?.localeCookie || DEFAULT_LOCALE_COOKIE_NAME, THEME_COOKIE_OPTIONS);
28
23
  const locale = useThemeLocale(theme, localeCookie, {
29
24
  enabled: themeConfig?.enabled?.translation !== false,
30
25
  forcedLocale: themeConfig?.locale,
@@ -1,6 +1,7 @@
1
1
  import { defineNuxtPlugin, useRuntimeConfig, useCookie } from "#app";
2
- import { THEME_PREFERENCE_COOKIE, THEME_RESOLVED_COOKIE, THEME_APPLIED_EVENT, DEFAULT_THEME } from "../../shared/constants";
2
+ import { THEME_PREFERENCE_COOKIE, THEME_RESOLVED_COOKIE, THEME_MODE_COOKIE, THEME_COOKIE_OPTIONS, THEME_APPLIED_EVENT } from "../../shared/constants";
3
3
  import { buildResolvedTheme } from "../../shared/utils/theme-config";
4
+ import { resolveServerTheme } from "../../shared/utils/theme-resolve";
4
5
  const VUETIFY_THEME_STYLESHEET_ID = "vuetify-theme-stylesheet";
5
6
  const EARLY_THEME_STYLESHEET_ID = "venix-vuetify-theme-early";
6
7
  function applyVuetifyTheme(vuetify, name) {
@@ -31,26 +32,22 @@ export default defineNuxtPlugin({
31
32
  setup(nuxtApp) {
32
33
  const config = useRuntimeConfig();
33
34
  const themeConfig = config.public.venixTheme;
34
- const resolvedCookie = useCookie(THEME_RESOLVED_COOKIE);
35
- const preferenceCookie = useCookie(THEME_PREFERENCE_COOKIE);
35
+ const resolvedCookie = useCookie(THEME_RESOLVED_COOKIE, THEME_COOKIE_OPTIONS);
36
+ const preferenceCookie = useCookie(THEME_PREFERENCE_COOKIE, THEME_COOKIE_OPTIONS);
37
+ const modeCookie = useCookie(THEME_MODE_COOKIE, THEME_COOKIE_OPTIONS);
36
38
  const theme = buildResolvedTheme(themeConfig);
37
- const defaultTheme = theme.colors.defaultColor || DEFAULT_THEME;
38
39
  const hookVuetify = nuxtApp.hook;
39
40
  hookVuetify("vuetify:before-create", ({ vuetifyOptions }) => {
40
41
  const themes = vuetifyOptions?.theme?.themes;
41
42
  if (!vuetifyOptions?.theme || !themes) return;
42
- let resolved = defaultTheme;
43
- if (resolvedCookie.value && themes[resolvedCookie.value]) {
44
- resolved = resolvedCookie.value;
45
- } else if (preferenceCookie.value && themes[preferenceCookie.value]) {
46
- resolved = preferenceCookie.value;
47
- }
48
- vuetifyOptions.theme.defaultTheme = resolved;
43
+ const resolved = import.meta.client && window.__VENIX_INITIAL_THEME__ || resolveServerTheme(preferenceCookie.value, modeCookie.value, resolvedCookie.value, theme.colors);
44
+ if (themes[resolved]) vuetifyOptions.theme.defaultTheme = resolved;
49
45
  });
50
46
  if (import.meta.client) {
51
47
  hookVuetify("vuetify:ready", (vuetify) => {
52
48
  if (!nuxtApp.payload.serverRendered) writeEarlyThemeStylesheet(nuxtApp, vuetify);
53
49
  window.addEventListener(THEME_APPLIED_EVENT, ((event) => {
50
+ if (vuetify.theme.themes && !vuetify.theme.themes.value[event.detail]) return;
54
51
  applyVuetifyTheme(vuetify, event.detail);
55
52
  }));
56
53
  });