@venix-sistemas/jade 1.1.0 → 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.
- package/README.md +339 -222
- package/dist/module.json +1 -1
- package/dist/module.mjs +25 -11
- package/dist/runtime/components/VenixThemeSwitcher.d.vue.ts +1 -1
- package/dist/runtime/components/VenixThemeSwitcher.vue +242 -99
- package/dist/runtime/components/VenixThemeSwitcher.vue.d.ts +1 -1
- package/dist/runtime/composables/useThemeColors.d.ts +6 -0
- package/dist/runtime/composables/useThemeColors.js +24 -9
- package/dist/runtime/composables/useThemeCookies.d.ts +9 -2
- package/dist/runtime/composables/useThemeCookies.js +24 -6
- package/dist/runtime/composables/useThemeLocale.d.ts +5 -0
- package/dist/runtime/composables/useThemeLocale.js +2 -2
- package/dist/runtime/composables/useThemeSeasonal.d.ts +2 -2
- package/dist/runtime/composables/useThemeSeasonal.js +1 -1
- package/dist/runtime/composables/useVenixTheme.d.ts +20 -0
- package/dist/runtime/composables/useVenixTheme.js +168 -108
- package/dist/runtime/nitro/theme-init.js +8 -5
- package/dist/runtime/plugins/theme-init.server.js +7 -12
- package/dist/runtime/plugins/vuetify-theme.js +8 -11
- package/dist/runtime/scripts/theme-init.d.ts +23 -10
- package/dist/runtime/scripts/theme-init.js +42 -88
- package/dist/shared/constants.d.ts +18 -3
- package/dist/shared/constants.js +6 -1
- package/dist/shared/css/cursor.js +9 -2
- package/dist/shared/css/index.d.ts +1 -7
- package/dist/shared/css/index.js +5 -5
- package/dist/shared/css/transition.d.ts +1 -1
- package/dist/shared/theme.json +302 -192
- package/dist/shared/types/color-options.d.ts +26 -2
- package/dist/shared/types/colors.d.ts +64 -12
- package/dist/shared/types/index.d.ts +1 -1
- package/dist/shared/types/runtime-config.d.ts +4 -2
- package/dist/shared/utils/icon-refs.d.ts +7 -3
- package/dist/shared/utils/icon.d.ts +4 -2
- package/dist/shared/utils/icon.js +6 -2
- package/dist/shared/utils/index.d.ts +5 -4
- package/dist/shared/utils/index.js +12 -6
- package/dist/shared/utils/inline-script.d.ts +6 -0
- package/dist/shared/utils/inline-script.js +3 -0
- package/dist/shared/utils/load.d.ts +0 -1
- package/dist/shared/utils/load.js +5 -12
- package/dist/shared/utils/merge-themes.d.ts +39 -9
- package/dist/shared/utils/merge-themes.js +101 -5
- package/dist/shared/utils/normalize.d.ts +0 -11
- package/dist/shared/utils/normalize.js +0 -12
- package/dist/shared/utils/theme-config.d.ts +13 -4
- package/dist/shared/utils/theme-config.js +28 -15
- package/dist/shared/utils/theme-resolve.d.ts +15 -23
- package/dist/shared/utils/theme-resolve.js +18 -67
- package/dist/shared/utils/theme-resolver.d.ts +46 -0
- package/dist/shared/utils/theme-resolver.js +139 -0
- package/package.json +13 -10
- package/README-default.md +0 -84
- package/dist/runtime/plugin.d.ts +0 -2
- 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
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
const
|
|
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
|
|
18
|
-
|
|
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>;
|
|
@@ -5,7 +5,7 @@ import { normalizeLocale, extractFirstLocale } from "../../shared/utils/normaliz
|
|
|
5
5
|
import { extractLocales } from "../../shared/utils/load";
|
|
6
6
|
import { hasCookieConsent } from "../../shared/utils/consent";
|
|
7
7
|
export const useThemeLocale = (theme, localeCookie, options = {}) => {
|
|
8
|
-
const { enabled = true, forcedLocale, defaultLocale } = options;
|
|
8
|
+
const { enabled = true, forcedLocale, defaultLocale, canPersist = hasCookieConsent } = options;
|
|
9
9
|
const headers = useRequestHeaders(["accept-language"]);
|
|
10
10
|
const route = useRoute();
|
|
11
11
|
const fallbackLocale = defaultLocale || DEFAULT_LOCALE;
|
|
@@ -69,7 +69,7 @@ export const useThemeLocale = (theme, localeCookie, options = {}) => {
|
|
|
69
69
|
const setLocale = (locale) => {
|
|
70
70
|
const normalized = normalizeLocale(locale);
|
|
71
71
|
currentLocale.value = normalized;
|
|
72
|
-
if (enabled && !forcedLocale &&
|
|
72
|
+
if (enabled && !forcedLocale && canPersist()) {
|
|
73
73
|
localeCookie.value = normalized;
|
|
74
74
|
}
|
|
75
75
|
};
|
|
@@ -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: (
|
|
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 = (
|
|
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 {
|
|
2
|
-
import { useRuntimeConfig, useState } from "#app";
|
|
3
|
-
import {
|
|
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 {
|
|
11
|
-
|
|
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(
|
|
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,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
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
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) => {
|
|
45
41
|
if (!shouldApplyColors) return;
|
|
46
|
-
const resolved = getResolvedTheme(
|
|
42
|
+
const resolved = getResolvedTheme(selection);
|
|
47
43
|
if (typeof document !== "undefined") {
|
|
48
44
|
const html = document.documentElement;
|
|
49
45
|
html.classList.remove(...colors.themeValues.value);
|
|
@@ -53,10 +49,10 @@ export const useVenixTheme = () => {
|
|
|
53
49
|
window.dispatchEvent(new CustomEvent(THEME_APPLIED_EVENT, { detail: resolved }));
|
|
54
50
|
}
|
|
55
51
|
};
|
|
56
|
-
const applyWithTransition = (
|
|
52
|
+
const applyWithTransition = (selection) => {
|
|
57
53
|
const canAnimate = typeof document !== "undefined" && typeof document.startViewTransition === "function" && !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
58
54
|
if (!canAnimate) {
|
|
59
|
-
apply(
|
|
55
|
+
apply(selection);
|
|
60
56
|
return;
|
|
61
57
|
}
|
|
62
58
|
const body = document.body;
|
|
@@ -66,28 +62,15 @@ export const useVenixTheme = () => {
|
|
|
66
62
|
body.style.setProperty("cursor", currentCursor, "important");
|
|
67
63
|
}
|
|
68
64
|
const restoreCursor = () => body.style.removeProperty("cursor");
|
|
69
|
-
const transition = document.startViewTransition(() => apply(
|
|
65
|
+
const transition = document.startViewTransition(() => apply(selection));
|
|
70
66
|
transition.ready.catch(() => {
|
|
71
67
|
});
|
|
72
68
|
transition.finished.then(restoreCursor).catch(restoreCursor);
|
|
73
69
|
};
|
|
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
70
|
const syncPersistence = () => {
|
|
88
71
|
hasConsent.value = hasCookieConsent();
|
|
89
|
-
if (
|
|
90
|
-
cookies.persistIfConsented(preference.value, getResolvedTheme(
|
|
72
|
+
if (cookies.canPersist()) {
|
|
73
|
+
cookies.persistIfConsented(preference.value, modePreference.value, getResolvedTheme());
|
|
91
74
|
} else {
|
|
92
75
|
cookies.disablePersistence();
|
|
93
76
|
}
|
|
@@ -98,91 +81,168 @@ export const useVenixTheme = () => {
|
|
|
98
81
|
syncPersistence();
|
|
99
82
|
window.dispatchEvent(new Event(COOKIE_PREFERENCES_UPDATED_EVENT));
|
|
100
83
|
};
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
84
|
+
const setTheme = (name) => {
|
|
85
|
+
if (name === SYSTEM_THEME || isSelectableGroup(theme.colors, name)) preference.value = name;
|
|
86
|
+
};
|
|
87
|
+
const setMode = (mode) => {
|
|
88
|
+
if (isModePreference(mode)) modePreference.value = mode;
|
|
89
|
+
};
|
|
90
|
+
const resolvedTheme = computed(() => {
|
|
91
|
+
void environmentVersion.value;
|
|
92
|
+
return getResolvedTheme(currentSelection());
|
|
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
|
+
};
|
|
100
|
+
const toggle = (forceTheme) => {
|
|
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;
|
|
109
118
|
}
|
|
110
119
|
};
|
|
111
|
-
|
|
120
|
+
const data = computed(
|
|
121
|
+
() => colors.themes.value.find((t) => t.value === preference.value) ?? colors.themes.value[0]
|
|
122
|
+
);
|
|
123
|
+
const modeData = computed(
|
|
124
|
+
() => colors.modes.value.find((m) => m.value === modePreference.value) ?? colors.modes.value[0]
|
|
125
|
+
);
|
|
126
|
+
const activeSeasonalTheme = computed(() => {
|
|
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(() => {
|
|
112
186
|
hasConsent.value = hasCookieConsent();
|
|
187
|
+
window.addEventListener(COOKIE_PREFERENCES_UPDATED_EVENT, core.syncPersistence);
|
|
113
188
|
if (!shouldApplyColors) return;
|
|
114
|
-
|
|
115
|
-
const handleSystemThemeChange = () => {
|
|
189
|
+
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
|
|
116
190
|
environmentVersion.value++;
|
|
117
|
-
if (
|
|
118
|
-
};
|
|
119
|
-
mq.addEventListener("change", handleSystemThemeChange);
|
|
120
|
-
let midnightTimeout;
|
|
191
|
+
if (modePreference.value === "system") core.applyWithTransition(core.currentSelection());
|
|
192
|
+
});
|
|
121
193
|
const scheduleSeasonalCheck = () => {
|
|
122
194
|
const now = /* @__PURE__ */ new Date();
|
|
123
195
|
const msToMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime() - now.getTime();
|
|
124
|
-
|
|
196
|
+
setTimeout(() => {
|
|
125
197
|
environmentVersion.value++;
|
|
126
|
-
if (preference.value ===
|
|
198
|
+
if (preference.value === SYSTEM_THEME) core.applyWithTransition(core.currentSelection());
|
|
127
199
|
scheduleSeasonalCheck();
|
|
128
200
|
}, msToMidnight + 1e3);
|
|
129
201
|
};
|
|
130
202
|
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
|
-
});
|
|
140
|
-
watch(preference, (newPref) => {
|
|
141
|
-
if (!shouldApplyColors) return;
|
|
142
|
-
applyWithTransition(newPref);
|
|
143
|
-
cookies.persistIfConsented(newPref, getResolvedTheme(newPref));
|
|
144
203
|
});
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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);
|
|
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
|
+
}
|
|
163
214
|
return {
|
|
164
215
|
theme: {
|
|
165
|
-
value: resolvedTheme,
|
|
166
|
-
preference,
|
|
167
|
-
data,
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
|
173
233
|
},
|
|
174
|
-
|
|
234
|
+
modes: core.colors.modes,
|
|
175
235
|
locale: {
|
|
176
|
-
current: locale.currentLocale,
|
|
177
|
-
set: locale.setLocale,
|
|
178
|
-
translate: locale.translate
|
|
236
|
+
current: core.locale.currentLocale,
|
|
237
|
+
set: core.locale.setLocale,
|
|
238
|
+
translate: core.locale.translate
|
|
179
239
|
},
|
|
180
240
|
persistence: {
|
|
181
|
-
hasConsent,
|
|
182
|
-
grant:
|
|
183
|
-
revoke:
|
|
184
|
-
enable: () => cookies.persistIfConsented(preference.value, getResolvedTheme(
|
|
185
|
-
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
|
|
186
246
|
}
|
|
187
247
|
};
|
|
188
248
|
};
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { defineNitroPlugin, useRuntimeConfig } from "nitropack/runtime";
|
|
2
|
-
import
|
|
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
|
|
6
|
-
const
|
|
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
|
-
|
|
9
|
+
hookRenderHtml("render:html", (html, { event }) => {
|
|
10
|
+
const nonce = event?.context?.security?.nonce;
|
|
11
|
+
const nonceAttr = nonce ? ` nonce="${nonce.replace(/"/g, """)}"` : "";
|
|
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
|
|
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 {
|
|
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
|
|
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,
|
|
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
|
-
|
|
43
|
-
if (
|
|
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
|
});
|
|
@@ -1,27 +1,40 @@
|
|
|
1
1
|
import type { ThemeConfig } from '../../shared/types/index.js';
|
|
2
|
+
import type { ThemeResolver } from '../../shared/utils/theme-resolver.js';
|
|
2
3
|
declare global {
|
|
3
4
|
interface Window {
|
|
4
5
|
__VENIX_INITIAL_THEME__?: string;
|
|
5
6
|
}
|
|
6
7
|
}
|
|
8
|
+
export interface InitThemeOptions {
|
|
9
|
+
/** See `color.requireConsent` — when `false`, the theme cookies are always written. */
|
|
10
|
+
requireConsent: boolean;
|
|
11
|
+
cookies: {
|
|
12
|
+
preference: string;
|
|
13
|
+
mode: string;
|
|
14
|
+
resolved: string;
|
|
15
|
+
};
|
|
16
|
+
/** In seconds. */
|
|
17
|
+
cookieMaxAge: number;
|
|
18
|
+
consentStorageKey: string;
|
|
19
|
+
}
|
|
7
20
|
/**
|
|
8
21
|
* Runs as an inline script in `<head>`, injected via Nitro's `render:html`
|
|
9
22
|
* hook by the `runtime/nitro/theme-init.ts` plugin (see
|
|
10
23
|
* `setup/register-plugins.ts`) — BEFORE the Vue bundle, to avoid a
|
|
11
24
|
* wrong-theme flash before hydration (SSR doesn't know the user's real
|
|
12
|
-
* light/dark preference when the
|
|
25
|
+
* light/dark preference when the mode is 'system', nor does it have
|
|
13
26
|
* `matchMedia`).
|
|
14
27
|
*
|
|
15
28
|
* Because it runs this way, this function (only this function — the rest of
|
|
16
29
|
* the file, including the type imports, is erased by TypeScript and never
|
|
17
30
|
* reaches the browser) has to be 100% self-contained: `initTheme.toString()`
|
|
18
|
-
* gets serialized and executed as a standalone IIFE, so
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* and the SSR plugin, lives in `shared/constants.ts` and
|
|
24
|
-
* `shared/utils/theme-resolve.ts`. Keep the two sides in sync when changing
|
|
25
|
-
* either one.
|
|
31
|
+
* gets serialized and executed as a standalone IIFE, so anything from another
|
|
32
|
+
* module would be a `ReferenceError` on the client. Everything it needs comes
|
|
33
|
+
* in as arguments instead: the theme-resolution rule as the (equally
|
|
34
|
+
* self-contained) `createThemeResolver` factory, and the cookie names and
|
|
35
|
+
* settings from `shared/constants.ts` in `options`.
|
|
26
36
|
*/
|
|
27
|
-
export default function initTheme(themeData: ThemeConfig): void;
|
|
37
|
+
export default function initTheme(createThemeResolver: () => ThemeResolver, themeData: ThemeConfig, options: InitThemeOptions): void;
|
|
38
|
+
export declare function createInitThemeOptions(requireConsent: boolean): InitThemeOptions;
|
|
39
|
+
/** The inline script's source: `initTheme` as an IIFE, with its arguments serialized. */
|
|
40
|
+
export declare function renderInitThemeScript(themeData: ThemeConfig, options: InitThemeOptions): string;
|