@venix-sistemas/jade 1.4.0 → 1.4.2-alpha.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 (45) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +994 -969
  3. package/dist/module.d.mts +1 -0
  4. package/dist/module.json +4 -1
  5. package/dist/module.mjs +39 -10
  6. package/dist/runtime/components/VenixIcon.vue +20 -20
  7. package/dist/runtime/components/VenixThemeSwitcher.vue +336 -336
  8. package/dist/runtime/composables/useVenixTheme.d.ts +4 -4
  9. package/dist/runtime/composables/useVenixTheme.js +25 -0
  10. package/dist/runtime/nitro/theme-init.js +1 -1
  11. package/dist/runtime/scripts/theme-init.d.ts +11 -1
  12. package/dist/runtime/scripts/theme-init.js +25 -2
  13. package/dist/runtime/server/tsconfig.json +3 -3
  14. package/dist/shared/css/colors.d.ts +1 -1
  15. package/dist/shared/css/colors.js +11 -3
  16. package/dist/shared/css/index.js +1 -1
  17. package/dist/shared/css/tokens.d.ts +7 -0
  18. package/dist/shared/css/tokens.js +32 -5
  19. package/dist/shared/theme.json +314 -320
  20. package/dist/shared/token-names.d.ts +14 -5
  21. package/dist/shared/token-names.js +1 -1
  22. package/dist/shared/types/color-options.d.ts +24 -1
  23. package/dist/shared/types/colors.d.ts +2 -0
  24. package/dist/shared/types/index.d.ts +3 -1
  25. package/dist/shared/types/runtime-config.d.ts +2 -0
  26. package/dist/shared/types/theme-names.d.ts +23 -0
  27. package/dist/shared/types/theme-names.js +0 -0
  28. package/dist/shared/utils/base-url.d.ts +15 -0
  29. package/dist/shared/utils/base-url.js +32 -0
  30. package/dist/shared/utils/derive-seeds.d.ts +24 -0
  31. package/dist/shared/utils/derive-seeds.js +30 -0
  32. package/dist/shared/utils/load.d.ts +1 -1
  33. package/dist/shared/utils/load.js +1 -1
  34. package/dist/shared/utils/merge-themes.d.ts +10 -0
  35. package/dist/shared/utils/merge-themes.js +55 -0
  36. package/dist/shared/utils/oklab.d.ts +8 -0
  37. package/dist/shared/utils/oklab.js +15 -2
  38. package/dist/shared/utils/scale.d.ts +7 -2
  39. package/dist/shared/utils/scale.js +4 -3
  40. package/dist/shared/utils/theme-color.d.ts +9 -0
  41. package/dist/shared/utils/theme-color.js +10 -0
  42. package/dist/shared/utils/theme-types.d.ts +8 -0
  43. package/dist/shared/utils/theme-types.js +16 -0
  44. package/dist/types.d.mts +2 -0
  45. package/package.json +25 -24
@@ -1,4 +1,4 @@
1
- import type { ThemeMode, ThemeModePreference } from '../../shared/types/index.js';
1
+ import type { ThemeMode, ThemeModePreference, ThemeName, ThemeOrVariantName, TokenName } from '../../shared/types/index.js';
2
2
  export declare const useVenixTheme: () => {
3
3
  theme: {
4
4
  value: import("vue").ComputedRef<string>;
@@ -9,8 +9,8 @@ export declare const useVenixTheme: () => {
9
9
  icon: import("../../shared/utils/icon.js").ResolvedThemeIcon | null;
10
10
  custom: boolean;
11
11
  } | undefined>;
12
- set: (name: string) => void;
13
- toggle: (forceTheme?: string) => void;
12
+ set: (name: ThemeName) => void;
13
+ toggle: (forceTheme?: ThemeOrVariantName) => void;
14
14
  isSeasonalActive: import("vue").ComputedRef<boolean>;
15
15
  activeSeasonalTheme: import("vue").ComputedRef<string | null>;
16
16
  shouldApplyColors: boolean;
@@ -32,7 +32,7 @@ export declare const useVenixTheme: () => {
32
32
  tokens: {
33
33
  all: Record<string, Record<string, string> | undefined>;
34
34
  current: import("vue").ComputedRef<Record<string, string> | undefined>;
35
- cssVar: (name: string) => string;
35
+ cssVar: (name: TokenName | (string & Record<never, never>)) => string;
36
36
  };
37
37
  mode: {
38
38
  value: import("vue").ComputedRef<ThemeMode>;
@@ -6,6 +6,7 @@ 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
+ import { themeColorOf } from "../../shared/utils/theme-color";
9
10
  import { buildResolvedTheme } from "../../shared/utils/theme-config";
10
11
  import { findVariantOwner, isModePreference, isSelectableGroup, normalizeThemeSelection, resolveMode, resolveThemeSelection } from "../../shared/utils/theme-resolve";
11
12
  function createThemeCore() {
@@ -62,6 +63,19 @@ function createThemeCore() {
62
63
  const environmentVersion = useState("venix-theme-environment", () => 0);
63
64
  const getPrefersDark = () => typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)").matches : true;
64
65
  const getResolvedTheme = (selection = currentSelection()) => resolveThemeSelection(selection, theme.colors, getPrefersDark());
66
+ const applyThemeColorMeta = (variantId) => {
67
+ const colorName = themeConfig?.themeColorMeta;
68
+ if (!colorName) return;
69
+ const color = themeColorOf(theme.colors.themes, variantId, colorName);
70
+ if (!color) return;
71
+ let meta = document.head.querySelector('meta[name="theme-color"]:not([media])');
72
+ if (!meta) {
73
+ meta = document.createElement("meta");
74
+ meta.name = "theme-color";
75
+ document.head.append(meta);
76
+ }
77
+ meta.content = color;
78
+ };
65
79
  const apply = (selection) => {
66
80
  if (!shouldApplyColors) return;
67
81
  const resolved = getResolvedTheme(selection);
@@ -71,6 +85,7 @@ function createThemeCore() {
71
85
  html.classList.add(resolved);
72
86
  html.setAttribute("data-theme", resolved);
73
87
  window.__VENIX_INITIAL_THEME__ = resolved;
88
+ applyThemeColorMeta(resolved);
74
89
  window.dispatchEvent(new CustomEvent(THEME_APPLIED_EVENT, { detail: resolved }));
75
90
  }
76
91
  };
@@ -114,6 +129,11 @@ function createThemeCore() {
114
129
  };
115
130
  const setTheme = (name) => {
116
131
  if (name === SYSTEM_THEME || isSelectableGroup(theme.colors, name)) preference.value = name;
132
+ else warnUnknownTheme("theme.set", name);
133
+ };
134
+ const warnUnknownTheme = (caller, name) => {
135
+ if (!import.meta.dev) return;
136
+ console.warn(`[jade] ${caller}('${name}') did nothing: no such theme. Available: ${[SYSTEM_THEME, ...Object.keys(theme.colors.groups).filter((group) => isSelectableGroup(theme.colors, group))].join(", ")}.`);
117
137
  };
118
138
  const setMode = (mode) => {
119
139
  if (isModePreference(mode)) modePreference.value = mode;
@@ -146,6 +166,8 @@ function createThemeCore() {
146
166
  if (owner) {
147
167
  preference.value = owner.theme;
148
168
  modePreference.value = owner.mode;
169
+ } else {
170
+ warnUnknownTheme("theme.toggle", forceTheme);
149
171
  }
150
172
  };
151
173
  const data = computed(
@@ -281,6 +303,9 @@ function installThemeEffects(core) {
281
303
  onNuxtReady(() => {
282
304
  hasConsent.value = hasCookieConsent();
283
305
  window.addEventListener(COOKIE_PREFERENCES_UPDATED_EVENT, core.syncPersistence);
306
+ window.addEventListener("storage", (event) => {
307
+ if (event.key === null || event.key === COOKIE_CONSENT_STORAGE_KEY) core.syncPersistence();
308
+ });
284
309
  const cookieStore = window.cookieStore;
285
310
  cookieStore?.addEventListener("change", syncFromCookies);
286
311
  window.addEventListener("focus", syncFromCookies);
@@ -4,7 +4,7 @@ import { buildResolvedTheme } from "../../shared/utils/theme-config";
4
4
  export default defineNitroPlugin((nitroApp) => {
5
5
  const themeConfig = useRuntimeConfig().public.venixTheme;
6
6
  const theme = buildResolvedTheme(themeConfig);
7
- const scriptBody = renderInitThemeScript(theme, createInitThemeOptions(themeConfig?.requireConsent !== false));
7
+ const scriptBody = renderInitThemeScript(theme, createInitThemeOptions(themeConfig?.requireConsent !== false, themeConfig?.themeColorMeta ?? null));
8
8
  const hookRenderHtml = nitroApp.hooks.hook;
9
9
  hookRenderHtml("render:html", (html, { event }) => {
10
10
  const nonce = event?.context?.security?.nonce;
@@ -25,6 +25,8 @@ export interface InitThemeOptions {
25
25
  cursor: string;
26
26
  font: string;
27
27
  };
28
+ /** The color of each variant that drives `<meta name="theme-color">` (`color.themeColorMeta`), or `null` to leave the tag alone. */
29
+ themeColor: string | null;
28
30
  }
29
31
  /**
30
32
  * Runs as an inline script in `<head>`, injected via Nitro's `render:html`
@@ -44,6 +46,14 @@ export interface InitThemeOptions {
44
46
  * settings from `shared/constants.ts` in `options`.
45
47
  */
46
48
  export default function initTheme(createThemeResolver: () => ThemeResolver, themeData: ResolvedTheme, options: InitThemeOptions): void;
47
- export declare function createInitThemeOptions(requireConsent: boolean): InitThemeOptions;
49
+ export declare function createInitThemeOptions(requireConsent: boolean, themeColor?: string | null): InitThemeOptions;
50
+ /**
51
+ * Only what the script's theme resolution reads, so the HTML doesn't carry
52
+ * every color, translation and icon of every theme on each response: the
53
+ * name, variants, `seasonal` and `dateRange` of each group, and per variant
54
+ * whether it is a color theme (`isColorTheme` looks at `primary` and
55
+ * `background`). `defaultColor` stays as is.
56
+ */
57
+ export declare function toInitThemeData(themeData: ResolvedTheme, themeColor?: string | null): ResolvedTheme;
48
58
  /** The inline script's source: `initTheme` as an IIFE, with its arguments serialized. */
49
59
  export declare function renderInitThemeScript(themeData: ResolvedTheme, options: InitThemeOptions): string;
@@ -52,6 +52,14 @@ export default function initTheme(createThemeResolver2, themeData, options) {
52
52
  html.classList.add(theme);
53
53
  html.setAttribute("data-theme", theme);
54
54
  window.__VENIX_INITIAL_THEME__ = theme;
55
+ const themeColor = options.themeColor;
56
+ const barColor = themeColor ? colors.themes[theme]?.[themeColor] : void 0;
57
+ if (barColor && barColor.charAt(0) === "#") {
58
+ const meta = document.createElement("meta");
59
+ meta.setAttribute("name", "theme-color");
60
+ meta.setAttribute("content", barColor);
61
+ document.head.appendChild(meta);
62
+ }
55
63
  for (const toggle of ["scrollbar", "cursor", "font"]) {
56
64
  if (getCookie(cookies[toggle]) === "off") html.setAttribute(attributes[toggle], "off");
57
65
  }
@@ -65,9 +73,10 @@ export default function initTheme(createThemeResolver2, themeData, options) {
65
73
  setCookieIfConsented(cookies.mode, selection.mode);
66
74
  }
67
75
  }
68
- export function createInitThemeOptions(requireConsent) {
76
+ export function createInitThemeOptions(requireConsent, themeColor = null) {
69
77
  return {
70
78
  requireConsent,
79
+ themeColor,
71
80
  cookies: {
72
81
  preference: THEME_PREFERENCE_COOKIE,
73
82
  mode: THEME_MODE_COOKIE,
@@ -81,6 +90,20 @@ export function createInitThemeOptions(requireConsent) {
81
90
  attributes: { scrollbar: SCROLLBAR_ATTRIBUTE, cursor: CURSOR_ATTRIBUTE, font: FONT_ATTRIBUTE }
82
91
  };
83
92
  }
93
+ export function toInitThemeData(themeData, themeColor = null) {
94
+ const { themes = {}, groups = {}, defaultColor } = themeData.colors ?? {};
95
+ const slimThemes = {};
96
+ for (const [id, colors] of Object.entries(themes)) {
97
+ const { primary, background } = colors;
98
+ const bar = themeColor ? colors[themeColor] : void 0;
99
+ slimThemes[id] = { ...themeColor && bar && { [themeColor]: bar }, ...primary && { primary }, ...background && { background } };
100
+ }
101
+ const slimGroups = {};
102
+ for (const [name, { variants, seasonal, dateRange }] of Object.entries(groups)) {
103
+ slimGroups[name] = { variants, ...seasonal !== void 0 && { seasonal }, ...dateRange && { dateRange } };
104
+ }
105
+ return { colors: { themes: slimThemes, groups: slimGroups, defaultColor } };
106
+ }
84
107
  export function renderInitThemeScript(themeData, options) {
85
- return `(${initTheme.toString()})(${createThemeResolver.toString()}, ${toInlineScriptJson(themeData)}, ${toInlineScriptJson(options)})`;
108
+ return `(${initTheme.toString()})(${createThemeResolver.toString()}, ${toInlineScriptJson(toInitThemeData(themeData, options.themeColor))}, ${toInlineScriptJson(options)})`;
86
109
  }
@@ -1,3 +1,3 @@
1
- {
2
- "extends": "../../../.nuxt/tsconfig.server.json",
3
- }
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json",
3
+ }
@@ -1,2 +1,2 @@
1
1
  import type { ContrastLevel, ThemeColors } from '../types/index.js';
2
- export declare function generateThemeVars(mode: string, colors: ThemeColors, contrast?: ContrastLevel): string;
2
+ export declare function generateThemeVars(mode: string, colors: ThemeColors, contrast?: ContrastLevel, highContrast?: boolean): string;
@@ -1,11 +1,19 @@
1
1
  import { getThemeHexColors } from "../utils/theme-resolve.js";
2
2
  import { tokensEnabled } from "../token-names.js";
3
- import { generateTokenVars } from "./tokens.js";
4
- export function generateThemeVars(mode, colors, contrast) {
3
+ import { generateHighContrastVars, generateTokenVars } from "./tokens.js";
4
+ export function generateThemeVars(mode, colors, contrast, highContrast = false) {
5
5
  const colorVars = getThemeHexColors(colors).map(([key, value]) => ` --color-${key}: ${value};`).join("\n");
6
6
  const tokenVars = tokensEnabled(colors) ? `
7
7
  /* Semantic tokens */
8
8
  ${generateTokenVars(colors, contrast)}` : "";
9
+ const highContrastVars = highContrast && tokensEnabled(colors) ? generateHighContrastVars(colors, contrast) : "";
10
+ const highContrastBlock = highContrastVars ? `
11
+ @media (prefers-contrast: more) {
12
+ :root[data-theme='${mode}'] {
13
+ ${highContrastVars}
14
+ }
15
+ }
16
+ ` : "";
9
17
  const isDark = colors.dark === true;
10
18
  const colorScheme = isDark ? "dark" : "light";
11
19
  const inverseColor = isDark ? "#FFFFFF" : "#000000";
@@ -20,5 +28,5 @@ ${generateTokenVars(colors, contrast)}` : "";
20
28
  --color-inverse: ${inverseColor};
21
29
  ` + colorVars + tokenVars + `
22
30
  }
23
- `;
31
+ ` + highContrastBlock;
24
32
  }
@@ -15,7 +15,7 @@ export function processTheme(theme) {
15
15
  output.push(generateThemeTransitionCSS());
16
16
  for (const [themeName, themeColors] of Object.entries(theme.colors.themes)) {
17
17
  if (themeName === "system" || !isColorTheme(themeColors)) continue;
18
- output.push(generateThemeVars(themeName, themeColors, theme.colors.contrast || void 0));
18
+ output.push(generateThemeVars(themeName, themeColors, theme.colors.contrast || void 0, theme.colors.highContrast === true));
19
19
  }
20
20
  }
21
21
  }
@@ -4,6 +4,13 @@ import type { ContrastLevel, ThemeColors } from '../types/index.js';
4
4
  * written. Memoized: the CSS and the build check ask for the same variant.
5
5
  */
6
6
  export declare function generateTokens(colors: ThemeColors, level?: ContrastLevel): Array<[string, string]>;
7
+ /**
8
+ * The `--jade-*` declarations for the AAA version of a variant, only for the
9
+ * tokens whose value differs from the `level` version, or an empty string when
10
+ * there is nothing to add (`level` is already AAA). Best effort: a pair that
11
+ * can't reach 7:1 keeps the best the scale can do.
12
+ */
13
+ export declare function generateHighContrastVars(colors: ThemeColors, level?: ContrastLevel): string;
7
14
  /** The `--jade-*` declarations for `generateTokens`, one per line, indented for a theme block. */
8
15
  export declare function generateTokenVars(colors: ThemeColors, level?: ContrastLevel): string;
9
16
  /**
@@ -1,10 +1,11 @@
1
1
  import { contrastRatio } from "../utils/contrast.js";
2
+ import { deriveSeeds } from "../utils/derive-seeds.js";
2
3
  import { generateScales } from "../utils/scale.js";
3
4
  import { NEUTRAL_TOKENS, OVERRIDABLE_TOKEN_NAMES, ROLE_TOKENS, TOKEN_ROLES as ROLES, parseStepRef, roleTokenName, tokenOverrides, tokensEnabled } from "../token-names.js";
4
5
  const FALLBACK_BACKGROUND = { light: "#FFFFFF", dark: "#121212" };
5
6
  const tokenCache = /* @__PURE__ */ new Map();
6
7
  export function generateTokens(colors, level = "AA") {
7
- const key = JSON.stringify([colors.dark === true, colors.background, colors.primary, colors.secondary, level, colors.neutralTint !== false, tokenOverrides(colors)]);
8
+ const key = JSON.stringify([colors.dark === true, colors.background, ROLES.map((role) => colors[role]), level, colors.neutralTint !== false, tokenOverrides(colors)]);
8
9
  let tokens = tokenCache.get(key);
9
10
  if (!tokens) {
10
11
  tokens = buildTokens(colors, level);
@@ -15,12 +16,22 @@ export function generateTokens(colors, level = "AA") {
15
16
  function buildTokens(colors, level) {
16
17
  const mode = colors.dark === true ? "dark" : "light";
17
18
  const background = colors.background ?? FALLBACK_BACKGROUND[mode];
18
- const seeds = {};
19
+ const given = {};
19
20
  for (const role of ROLES) {
20
21
  const seed = colors[role];
22
+ if (seed) given[role] = seed;
23
+ }
24
+ const derived = deriveSeeds(given, mode);
25
+ const seeds = {};
26
+ for (const role of ROLES) {
27
+ const seed = given[role] ?? derived[role];
21
28
  if (seed) seeds[role] = seed;
22
29
  }
23
- const { neutral, roles } = generateScales(background, seeds, mode, level, { tint: colors.neutralTint !== false });
30
+ const overrides = tokenOverrides(colors);
31
+ const { neutral, roles } = generateScales(background, seeds, mode, level, {
32
+ tint: colors.neutralTint !== false,
33
+ mutedOver: farthestNeutralSurface(overrides)
34
+ });
24
35
  const tokens = NEUTRAL_TOKENS.map(([name, step]) => [name, neutral[step - 1]]);
25
36
  const primary = roles.primary;
26
37
  if (primary) tokens.push(["border-focus", primary.steps[10]]);
@@ -32,9 +43,17 @@ function buildTokens(colors, level) {
32
43
  }
33
44
  tokens.push([`on-${role}`, scale.on]);
34
45
  }
35
- applyOverrides(tokens, tokenOverrides(colors), neutral, roles);
46
+ applyOverrides(tokens, overrides, neutral, roles);
36
47
  return tokens;
37
48
  }
49
+ function farthestNeutralSurface(overrides) {
50
+ let farthest = 0;
51
+ for (const name of ["bg", "bg-subtle", "bg-muted"]) {
52
+ const parsed = parseStepRef(overrides[name]);
53
+ if (parsed?.scale === "neutral") farthest = Math.max(farthest, parsed.step);
54
+ }
55
+ return farthest;
56
+ }
38
57
  function applyOverrides(tokens, overrides, neutral, roles) {
39
58
  for (const [name, ref] of Object.entries(overrides)) {
40
59
  const parsed = parseStepRef(ref);
@@ -45,6 +64,11 @@ function applyOverrides(tokens, overrides, neutral, roles) {
45
64
  if (color) entry[1] = color;
46
65
  }
47
66
  }
67
+ export function generateHighContrastVars(colors, level = "AA") {
68
+ if (level === "AAA") return "";
69
+ const base = new Map(generateTokens(colors, level));
70
+ return generateTokens(colors, "AAA").filter(([name, value]) => base.get(name) !== value).map(([name, value]) => ` --jade-${name}: ${value};`).join("\n");
71
+ }
48
72
  export function generateTokenVars(colors, level = "AA") {
49
73
  return generateTokens(colors, level).map(([name, value]) => ` --jade-${name}: ${value};`).join("\n");
50
74
  }
@@ -103,6 +127,9 @@ export function findTokenContrastIssues(themes, level) {
103
127
  }
104
128
  return messages;
105
129
  }
130
+ function hasRole(colors, role) {
131
+ return Boolean(colors[role] || colors.primary);
132
+ }
106
133
  export function findTokenOverrideIssues(themes) {
107
134
  const messages = [];
108
135
  for (const [id, colors] of Object.entries(themes)) {
@@ -111,7 +138,7 @@ export function findTokenOverrideIssues(themes) {
111
138
  const parsed = parseStepRef(ref);
112
139
  if (!OVERRIDABLE_TOKEN_NAMES.includes(name)) problems.push(`\`${name}\` is not a token that can be overridden`);
113
140
  else if (!parsed) problems.push(`\`${name}\`: \`${String(ref)}\` is not a step (use \`neutral-1\` to \`neutral-12\`, \`primary-1\`\u2026)`);
114
- else if (parsed.scale !== "neutral" && !colors[parsed.scale]) problems.push(`\`${name}\`: the variant has no \`${parsed.scale}\` seed`);
141
+ else if (parsed.scale !== "neutral" && !hasRole(colors, parsed.scale)) problems.push(`\`${name}\`: the variant has no \`${parsed.scale}\` seed and no \`primary\` to derive it from`);
115
142
  }
116
143
  if (problems.length) messages.push(`Some \`tokens\` overrides of \`${id}\` were ignored: ${problems.join("; ")}.`);
117
144
  }