@venix-sistemas/jade 1.1.0 → 1.3.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 (64) hide show
  1. package/README.md +555 -228
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +89 -13
  4. package/dist/runtime/components/VenixThemeSwitcher.d.vue.ts +3 -1
  5. package/dist/runtime/components/VenixThemeSwitcher.vue +592 -106
  6. package/dist/runtime/components/VenixThemeSwitcher.vue.d.ts +3 -1
  7. package/dist/runtime/composables/useThemeColors.d.ts +8 -2
  8. package/dist/runtime/composables/useThemeColors.js +24 -9
  9. package/dist/runtime/composables/useThemeCookies.d.ts +17 -2
  10. package/dist/runtime/composables/useThemeCookies.js +44 -7
  11. package/dist/runtime/composables/useThemeLocale.d.ts +7 -2
  12. package/dist/runtime/composables/useThemeLocale.js +2 -2
  13. package/dist/runtime/composables/useThemeSeasonal.d.ts +3 -3
  14. package/dist/runtime/composables/useThemeSeasonal.js +1 -1
  15. package/dist/runtime/composables/useVenixTheme.d.ts +40 -0
  16. package/dist/runtime/composables/useVenixTheme.js +249 -112
  17. package/dist/runtime/nitro/theme-init.js +8 -5
  18. package/dist/runtime/plugins/overlay-scrollbars.client.js +89 -0
  19. package/dist/runtime/plugins/theme-init.server.js +7 -12
  20. package/dist/runtime/plugins/vuetify-theme.js +8 -11
  21. package/dist/runtime/scripts/theme-init.d.ts +33 -11
  22. package/dist/runtime/scripts/theme-init.js +59 -88
  23. package/dist/shared/constants.d.ts +72 -3
  24. package/dist/shared/constants.js +26 -1
  25. package/dist/shared/css/cursor-svg.d.ts +10 -0
  26. package/dist/shared/css/cursor-svg.js +28 -0
  27. package/dist/shared/css/cursor.js +86 -34
  28. package/dist/shared/css/index.d.ts +1 -7
  29. package/dist/shared/css/index.js +5 -5
  30. package/dist/shared/css/scrollbar.js +61 -7
  31. package/dist/shared/css/transition.d.ts +1 -1
  32. package/dist/shared/css/typography.js +13 -0
  33. package/dist/shared/theme.json +308 -192
  34. package/dist/shared/types/color-options.d.ts +26 -2
  35. package/dist/shared/types/colors.d.ts +64 -12
  36. package/dist/shared/types/cursor.d.ts +34 -4
  37. package/dist/shared/types/index.d.ts +3 -3
  38. package/dist/shared/types/runtime-config.d.ts +5 -2
  39. package/dist/shared/types/scrollbar.d.ts +11 -0
  40. package/dist/shared/types/theme.d.ts +7 -0
  41. package/dist/shared/types/typography.d.ts +2 -1
  42. package/dist/shared/utils/icon-refs.d.ts +7 -3
  43. package/dist/shared/utils/icon.d.ts +4 -2
  44. package/dist/shared/utils/icon.js +6 -2
  45. package/dist/shared/utils/index.d.ts +5 -4
  46. package/dist/shared/utils/index.js +12 -6
  47. package/dist/shared/utils/inline-script.d.ts +6 -0
  48. package/dist/shared/utils/inline-script.js +3 -0
  49. package/dist/shared/utils/load.d.ts +2 -3
  50. package/dist/shared/utils/load.js +6 -12
  51. package/dist/shared/utils/merge-themes.d.ts +39 -9
  52. package/dist/shared/utils/merge-themes.js +101 -5
  53. package/dist/shared/utils/normalize.d.ts +0 -11
  54. package/dist/shared/utils/normalize.js +0 -12
  55. package/dist/shared/utils/theme-config.d.ts +15 -6
  56. package/dist/shared/utils/theme-config.js +28 -16
  57. package/dist/shared/utils/theme-resolve.d.ts +23 -22
  58. package/dist/shared/utils/theme-resolve.js +35 -66
  59. package/dist/shared/utils/theme-resolver.d.ts +47 -0
  60. package/dist/shared/utils/theme-resolver.js +140 -0
  61. package/package.json +22 -10
  62. package/README-default.md +0 -84
  63. package/dist/runtime/plugin.js +0 -3
  64. /package/dist/runtime/{plugin.d.ts → plugins/overlay-scrollbars.client.d.ts} +0 -0
@@ -1,20 +1,22 @@
1
- import { ref, 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, SCROLLBAR_ATTRIBUTE, CURSOR_ATTRIBUTE, FONT_ATTRIBUTE } 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,28 +24,47 @@ 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 environmentVersion = ref(0);
40
- const getResolvedTheme = (pref) => {
41
- const prefersDark = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)").matches : true;
42
- return resolveThemePreference(pref, theme.colors.themes, defaultTheme, prefersDark);
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 requireConsent = themeConfig?.requireConsent !== false;
38
+ const scrollbarAvailable = themeConfig?.enabled?.scrollbar !== false;
39
+ const cursorAvailable = themeConfig?.enabled?.cursor !== false;
40
+ const fontAvailable = themeConfig?.enabled?.customFont === true;
41
+ const scrollbarEnabled = useState("venix-scrollbar", () => cookies.scrollbarCookie.value !== "off");
42
+ const cursorEnabled = useState("venix-cursor", () => cookies.cursorCookie.value !== "off");
43
+ const fontEnabled = useState("venix-font", () => cookies.fontCookie.value !== "off");
44
+ const applyToggles = () => {
45
+ if (typeof document === "undefined") return;
46
+ const html = document.documentElement;
47
+ const flags = [
48
+ [SCROLLBAR_ATTRIBUTE, scrollbarEnabled.value],
49
+ [CURSOR_ATTRIBUTE, cursorEnabled.value],
50
+ [FONT_ATTRIBUTE, fontEnabled.value]
51
+ ];
52
+ for (const [attribute, enabled] of flags) {
53
+ if (enabled) html.removeAttribute(attribute);
54
+ else html.setAttribute(attribute, "off");
55
+ }
43
56
  };
44
- const apply = (pref) => {
57
+ const persistToggles = () => cookies.persistTogglesIfConsented({
58
+ scrollbar: scrollbarEnabled.value,
59
+ cursor: cursorEnabled.value,
60
+ font: fontEnabled.value
61
+ });
62
+ const environmentVersion = useState("venix-theme-environment", () => 0);
63
+ const getPrefersDark = () => typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)").matches : true;
64
+ const getResolvedTheme = (selection = currentSelection()) => resolveThemeSelection(selection, theme.colors, getPrefersDark());
65
+ const apply = (selection) => {
45
66
  if (!shouldApplyColors) return;
46
- const resolved = getResolvedTheme(pref);
67
+ const resolved = getResolvedTheme(selection);
47
68
  if (typeof document !== "undefined") {
48
69
  const html = document.documentElement;
49
70
  html.classList.remove(...colors.themeValues.value);
@@ -53,41 +74,34 @@ export const useVenixTheme = () => {
53
74
  window.dispatchEvent(new CustomEvent(THEME_APPLIED_EVENT, { detail: resolved }));
54
75
  }
55
76
  };
56
- const applyWithTransition = (pref) => {
77
+ const applyWithTransition = (selection) => {
57
78
  const canAnimate = typeof document !== "undefined" && typeof document.startViewTransition === "function" && !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
58
79
  if (!canAnimate) {
59
- apply(pref);
80
+ apply(selection);
60
81
  return;
61
82
  }
62
83
  const body = document.body;
63
- const hasCustomCursor = theme.customCursor?.enabled !== false && !!theme.customCursor?.cursors;
64
- const currentCursor = hasCustomCursor ? getComputedStyle(body).cursor : null;
65
- if (currentCursor && currentCursor !== "auto") {
66
- body.style.setProperty("cursor", currentCursor, "important");
67
- }
84
+ const hasCustomCursor = cursorAvailable && cursorEnabled.value && window.matchMedia("(any-pointer: fine)").matches;
85
+ const pinCursor = () => {
86
+ body.style.removeProperty("cursor");
87
+ const cursor = getComputedStyle(body).cursor;
88
+ if (cursor && cursor !== "auto") body.style.setProperty("cursor", cursor, "important");
89
+ };
90
+ if (hasCustomCursor) pinCursor();
68
91
  const restoreCursor = () => body.style.removeProperty("cursor");
69
- const transition = document.startViewTransition(() => apply(pref));
92
+ const transition = document.startViewTransition(() => {
93
+ apply(selection);
94
+ if (hasCustomCursor) pinCursor();
95
+ });
70
96
  transition.ready.catch(() => {
71
97
  });
72
98
  transition.finished.then(restoreCursor).catch(restoreCursor);
73
99
  };
74
- if (typeof document !== "undefined" && shouldApplyColors) {
75
- if (cookies.preferenceCookie.value && cookies.preferenceCookie.value !== preference.value) {
76
- preference.value = cookies.preferenceCookie.value;
77
- }
78
- const currentTheme = document.documentElement.getAttribute("data-theme");
79
- if (currentTheme) {
80
- if (!cookies.preferenceCookie.value) {
81
- preference.value = currentTheme;
82
- }
83
- } else {
84
- apply(preference.value);
85
- }
86
- }
87
100
  const syncPersistence = () => {
88
101
  hasConsent.value = hasCookieConsent();
89
- if (hasConsent.value) {
90
- cookies.persistIfConsented(preference.value, getResolvedTheme(preference.value));
102
+ if (cookies.canPersist()) {
103
+ cookies.persistIfConsented(preference.value, modePreference.value, getResolvedTheme());
104
+ persistToggles();
91
105
  } else {
92
106
  cookies.disablePersistence();
93
107
  }
@@ -98,91 +112,214 @@ export const useVenixTheme = () => {
98
112
  syncPersistence();
99
113
  window.dispatchEvent(new Event(COOKIE_PREFERENCES_UPDATED_EVENT));
100
114
  };
101
- const grantPersistence = () => setConsent(true);
102
- const revokePersistence = () => setConsent(false);
103
- const instance = getCurrentInstance();
104
- const runOnClientMount = (fn) => {
105
- if (instance) {
106
- onMounted(fn);
107
- } else if (typeof window !== "undefined") {
108
- queueMicrotask(fn);
115
+ const setTheme = (name) => {
116
+ if (name === SYSTEM_THEME || isSelectableGroup(theme.colors, name)) preference.value = name;
117
+ };
118
+ const setMode = (mode) => {
119
+ if (isModePreference(mode)) modePreference.value = mode;
120
+ };
121
+ const resolvedTheme = computed(() => {
122
+ void environmentVersion.value;
123
+ return getResolvedTheme(currentSelection());
124
+ });
125
+ const resolvedMode = computed(
126
+ () => theme.colors.themes[resolvedTheme.value]?.dark === false ? "light" : "dark"
127
+ );
128
+ const toggleMode = () => {
129
+ modePreference.value = resolvedMode.value === "dark" ? "light" : "dark";
130
+ };
131
+ const toggle = (forceTheme) => {
132
+ if (forceTheme === void 0) {
133
+ toggleMode();
134
+ return;
109
135
  }
136
+ if (forceTheme === SYSTEM_THEME) {
137
+ preference.value = SYSTEM_THEME;
138
+ modePreference.value = "system";
139
+ return;
140
+ }
141
+ if (isSelectableGroup(theme.colors, forceTheme)) {
142
+ preference.value = forceTheme;
143
+ return;
144
+ }
145
+ const owner = findVariantOwner(theme.colors, forceTheme);
146
+ if (owner) {
147
+ preference.value = owner.theme;
148
+ modePreference.value = owner.mode;
149
+ }
150
+ };
151
+ const data = computed(
152
+ () => colors.themes.value.find((t) => t.value === preference.value) ?? colors.themes.value[0]
153
+ );
154
+ const modeData = computed(
155
+ () => colors.modes.value.find((m) => m.value === modePreference.value) ?? colors.modes.value[0]
156
+ );
157
+ const activeSeasonalTheme = computed(() => {
158
+ void environmentVersion.value;
159
+ if (preference.value !== SYSTEM_THEME) return null;
160
+ return seasonal.getActiveSeasonalTheme(resolveMode(modePreference.value, getPrefersDark()));
161
+ });
162
+ const isSeasonalActive = computed(() => activeSeasonalTheme.value !== null);
163
+ return {
164
+ themeConfig,
165
+ theme,
166
+ shouldApplyColors,
167
+ cookies,
168
+ requireConsent,
169
+ hasConsent,
170
+ scrollbarAvailable,
171
+ cursorAvailable,
172
+ fontAvailable,
173
+ scrollbarEnabled,
174
+ cursorEnabled,
175
+ fontEnabled,
176
+ applyToggles,
177
+ persistToggles,
178
+ locale,
179
+ colors,
180
+ preference,
181
+ modePreference,
182
+ environmentVersion,
183
+ readCookieSelection,
184
+ currentSelection,
185
+ getResolvedTheme,
186
+ apply,
187
+ applyWithTransition,
188
+ syncPersistence,
189
+ setConsent,
190
+ setTheme,
191
+ setMode,
192
+ resolvedTheme,
193
+ resolvedMode,
194
+ toggleMode,
195
+ toggle,
196
+ data,
197
+ modeData,
198
+ activeSeasonalTheme,
199
+ isSeasonalActive
110
200
  };
111
- runOnClientMount(() => {
201
+ }
202
+ function installThemeEffects(core) {
203
+ const { themeConfig, shouldApplyColors, cookies, hasConsent, locale, preference, modePreference, environmentVersion, scrollbarEnabled, cursorEnabled, fontEnabled } = core;
204
+ if (themeConfig?.enabled?.manageHtmlLang) {
205
+ document.documentElement.lang = locale.currentLocale.value;
206
+ watch(locale.currentLocale, (newLocale) => {
207
+ document.documentElement.lang = newLocale;
208
+ });
209
+ }
210
+ if (shouldApplyColors) {
211
+ if (cookies.preferenceCookie.value || cookies.modeCookie.value) {
212
+ const fromCookies = core.readCookieSelection();
213
+ if (fromCookies.theme !== preference.value) preference.value = fromCookies.theme;
214
+ if (fromCookies.mode !== modePreference.value) modePreference.value = fromCookies.mode;
215
+ }
216
+ if (!document.documentElement.getAttribute("data-theme")) {
217
+ core.apply(core.currentSelection());
218
+ }
219
+ watch([preference, modePreference], () => {
220
+ const selection = core.currentSelection();
221
+ core.applyWithTransition(selection);
222
+ cookies.persistIfConsented(selection.theme, selection.mode, core.getResolvedTheme(selection));
223
+ });
224
+ }
225
+ scrollbarEnabled.value = cookies.scrollbarCookie.value !== "off";
226
+ cursorEnabled.value = cookies.cursorCookie.value !== "off";
227
+ fontEnabled.value = cookies.fontCookie.value !== "off";
228
+ core.applyToggles();
229
+ watch([scrollbarEnabled, cursorEnabled, fontEnabled], () => {
230
+ core.applyToggles();
231
+ core.persistToggles();
232
+ });
233
+ onNuxtReady(() => {
112
234
  hasConsent.value = hasCookieConsent();
235
+ window.addEventListener(COOKIE_PREFERENCES_UPDATED_EVENT, core.syncPersistence);
113
236
  if (!shouldApplyColors) return;
114
- const mq = window.matchMedia("(prefers-color-scheme: dark)");
115
- const handleSystemThemeChange = () => {
237
+ window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
116
238
  environmentVersion.value++;
117
- if (preference.value === "system") applyWithTransition("system");
118
- };
119
- mq.addEventListener("change", handleSystemThemeChange);
120
- let midnightTimeout;
239
+ if (modePreference.value === "system") core.applyWithTransition(core.currentSelection());
240
+ });
121
241
  const scheduleSeasonalCheck = () => {
122
242
  const now = /* @__PURE__ */ new Date();
123
243
  const msToMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime() - now.getTime();
124
- midnightTimeout = setTimeout(() => {
244
+ setTimeout(() => {
125
245
  environmentVersion.value++;
126
- if (preference.value === "system") applyWithTransition("system");
246
+ if (preference.value === SYSTEM_THEME) core.applyWithTransition(core.currentSelection());
127
247
  scheduleSeasonalCheck();
128
248
  }, msToMidnight + 1e3);
129
249
  };
130
250
  scheduleSeasonalCheck();
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
251
  });
140
- watch(preference, (newPref) => {
141
- if (!shouldApplyColors) return;
142
- applyWithTransition(newPref);
143
- cookies.persistIfConsented(newPref, getResolvedTheme(newPref));
144
- });
145
- const toggle = (forceTheme) => {
146
- const newTheme = forceTheme ?? (preference.value === "light" ? "dark" : "light");
147
- preference.value = newTheme;
148
- cookies.persistIfConsented(newTheme, getResolvedTheme(newTheme));
149
- };
150
- const data = computed(
151
- () => colors.themes.value.find((t) => t.value === preference.value) ?? colors.themes.value[0]
152
- );
153
- const resolvedTheme = computed(() => {
154
- void environmentVersion.value;
155
- return getResolvedTheme(preference.value);
156
- });
157
- const activeSeasonalTheme = computed(() => {
158
- void environmentVersion.value;
159
- if (preference.value !== "system") return null;
160
- return seasonal.getActiveSeasonalTheme();
161
- });
162
- const isSeasonalActive = computed(() => activeSeasonalTheme.value !== null);
252
+ }
253
+ export const useVenixTheme = () => {
254
+ const core = createThemeCore();
255
+ if (import.meta.client) {
256
+ const nuxtApp = useNuxtApp();
257
+ if (!nuxtApp._venixThemeEffects) {
258
+ nuxtApp._venixThemeEffects = true;
259
+ effectScope(true).run(() => installThemeEffects(createThemeCore()));
260
+ }
261
+ }
163
262
  return {
164
263
  theme: {
165
- value: resolvedTheme,
166
- preference,
167
- data,
168
- toggle,
169
- isSeasonalActive,
170
- activeSeasonalTheme,
171
- shouldApplyColors,
172
- colors: theme.colors
264
+ value: core.resolvedTheme,
265
+ preference: core.preference,
266
+ data: core.data,
267
+ set: core.setTheme,
268
+ toggle: core.toggle,
269
+ isSeasonalActive: core.isSeasonalActive,
270
+ activeSeasonalTheme: core.activeSeasonalTheme,
271
+ shouldApplyColors: core.shouldApplyColors,
272
+ colors: core.theme.colors
273
+ },
274
+ themes: core.colors.themes,
275
+ mode: {
276
+ value: core.resolvedMode,
277
+ preference: core.modePreference,
278
+ data: core.modeData,
279
+ set: core.setMode,
280
+ toggle: core.toggleMode
281
+ },
282
+ modes: core.colors.modes,
283
+ /** The custom scrollbar: `available` when the project has it on, `enabled` unless the visitor turned it off. */
284
+ scrollbar: {
285
+ available: core.scrollbarAvailable,
286
+ enabled: core.scrollbarEnabled,
287
+ set: (enabled) => {
288
+ core.scrollbarEnabled.value = enabled;
289
+ }
290
+ },
291
+ /** The custom cursor, same as `scrollbar`. */
292
+ cursor: {
293
+ available: core.cursorAvailable,
294
+ enabled: core.cursorEnabled,
295
+ set: (enabled) => {
296
+ core.cursorEnabled.value = enabled;
297
+ }
298
+ },
299
+ /** The custom font (`typography.customFonts`), same as `scrollbar`: off leaves the `defaultFonts`. */
300
+ font: {
301
+ available: core.fontAvailable,
302
+ enabled: core.fontEnabled,
303
+ set: (enabled) => {
304
+ core.fontEnabled.value = enabled;
305
+ }
173
306
  },
174
- themes: colors.themes,
175
307
  locale: {
176
- current: locale.currentLocale,
177
- set: locale.setLocale,
178
- translate: locale.translate
308
+ current: core.locale.currentLocale,
309
+ set: core.locale.setLocale,
310
+ translate: core.locale.translate
179
311
  },
180
312
  persistence: {
181
- hasConsent,
182
- grant: grantPersistence,
183
- revoke: revokePersistence,
184
- enable: () => cookies.persistIfConsented(preference.value, getResolvedTheme(preference.value)),
185
- disable: cookies.disablePersistence
313
+ /** `color.requireConsent`: whether changes are only saved after consent. */
314
+ required: core.requireConsent,
315
+ hasConsent: core.hasConsent,
316
+ grant: () => core.setConsent(true),
317
+ revoke: () => core.setConsent(false),
318
+ enable: () => {
319
+ core.cookies.persistIfConsented(core.preference.value, core.modePreference.value, core.getResolvedTheme());
320
+ core.persistToggles();
321
+ },
322
+ disable: core.cookies.disablePersistence
186
323
  }
187
324
  };
188
325
  };
@@ -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
  });
@@ -0,0 +1,89 @@
1
+ import { defineNuxtPlugin } from "#app";
2
+ import { OverlayScrollbars, ClickScrollPlugin } from "overlayscrollbars";
3
+ import { OVERLAY_SCROLLBAR_THEME, SCROLL_CONTAINER_ATTRIBUTE, SCROLLBAR_ATTRIBUTE } from "../../shared/constants";
4
+ const CONTAINERS = [
5
+ ".v-navigation-drawer__content",
6
+ ".v-dialog--scrollable .v-card-text",
7
+ ".v-select__content .v-list",
8
+ ".v-table__wrapper",
9
+ `[${SCROLL_CONTAINER_ATTRIBUTE}]`
10
+ ].join(", ");
11
+ const OPTIONS = {
12
+ scrollbars: { theme: OVERLAY_SCROLLBAR_THEME, autoHide: "never", clickScroll: true }
13
+ };
14
+ const SCROLL_BLOCKED_CLASS = "v-overlay-scroll-blocked";
15
+ const isOn = () => document.documentElement.getAttribute(SCROLLBAR_ATTRIBUTE) !== "off";
16
+ function followScrollBlock(instance, el) {
17
+ let previous;
18
+ const sync = () => {
19
+ const blocked = el.classList.contains(SCROLL_BLOCKED_CLASS);
20
+ if (blocked && !previous) {
21
+ previous = { ...instance.options().overflow };
22
+ instance.options({ overflow: { x: "hidden", y: "hidden" } });
23
+ } else if (!blocked && previous) {
24
+ instance.options({ overflow: previous });
25
+ previous = void 0;
26
+ }
27
+ };
28
+ const observer = new MutationObserver(sync);
29
+ observer.observe(el, { attributes: true, attributeFilter: ["class"] });
30
+ instance.on("destroyed", () => observer.disconnect());
31
+ }
32
+ function initPage() {
33
+ if (OverlayScrollbars(document.body)) return;
34
+ const instance = OverlayScrollbars(document.body, OPTIONS);
35
+ followScrollBlock(instance, document.documentElement);
36
+ }
37
+ function initContainer(el) {
38
+ const slot = el.parentElement;
39
+ if (OverlayScrollbars(el) || !slot) return;
40
+ if (getComputedStyle(slot).position === "static") slot.style.position = "relative";
41
+ const instance = OverlayScrollbars({ target: el, elements: { viewport: el }, scrollbars: { slot } }, OPTIONS);
42
+ alignScrollbars(instance, el, slot);
43
+ followScrollBlock(instance, el);
44
+ }
45
+ function alignScrollbars(instance, el, slot) {
46
+ const { scrollbarVertical, scrollbarHorizontal } = instance.elements();
47
+ for (const { scrollbar } of [scrollbarVertical, scrollbarHorizontal]) {
48
+ scrollbar.style.transition = "opacity 0.15s, visibility 0.15s";
49
+ }
50
+ const align = () => {
51
+ const top = el.offsetTop;
52
+ const left = el.offsetLeft;
53
+ const bottom = slot.clientHeight - top - el.offsetHeight;
54
+ const right = slot.clientWidth - left - el.offsetWidth;
55
+ Object.assign(scrollbarVertical.scrollbar.style, { top: `${top}px`, bottom: `${bottom}px`, right: `${right}px` });
56
+ Object.assign(scrollbarHorizontal.scrollbar.style, { left: `${left}px`, right: `${right}px`, bottom: `${bottom}px` });
57
+ };
58
+ align();
59
+ const observer = new ResizeObserver(align);
60
+ observer.observe(el);
61
+ observer.observe(slot);
62
+ instance.on("destroyed", () => observer.disconnect());
63
+ }
64
+ function containersIn(root) {
65
+ const found = [...root.querySelectorAll(CONTAINERS)];
66
+ return root.matches(CONTAINERS) ? [root, ...found] : found;
67
+ }
68
+ function initAll() {
69
+ initPage();
70
+ containersIn(document.body).forEach(initContainer);
71
+ }
72
+ function destroyAll() {
73
+ containersIn(document.body).forEach((el) => OverlayScrollbars(el)?.destroy());
74
+ OverlayScrollbars(document.body)?.destroy();
75
+ }
76
+ export default defineNuxtPlugin((nuxtApp) => {
77
+ OverlayScrollbars.plugin(ClickScrollPlugin);
78
+ nuxtApp.hook("app:mounted", () => {
79
+ if (isOn()) initAll();
80
+ new MutationObserver((mutations) => {
81
+ if (!isOn()) return;
82
+ for (const mutation of mutations) {
83
+ mutation.addedNodes.forEach((node) => node instanceof Element && containersIn(node).forEach(initContainer));
84
+ mutation.removedNodes.forEach((node) => node instanceof Element && containersIn(node).forEach((el) => OverlayScrollbars(el)?.destroy()));
85
+ }
86
+ }).observe(document.body, { childList: true, subtree: true });
87
+ new MutationObserver(() => isOn() ? initAll() : destroyAll()).observe(document.documentElement, { attributes: true, attributeFilter: [SCROLLBAR_ATTRIBUTE] });
88
+ });
89
+ });
@@ -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
  });