@the-portland-company/shell 0.16.0 → 0.16.2

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.
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ // src/theme-preference.ts
4
+ var THEME_STORAGE_KEY = "politogy_theme";
5
+ var THEME_COOKIE = "politogy_theme";
6
+ var VALID = ["light", "dark", "system"];
7
+ function isThemePreference(v) {
8
+ return typeof v === "string" && VALID.includes(v);
9
+ }
10
+ function readThemeCookie() {
11
+ if (typeof document === "undefined") return null;
12
+ try {
13
+ const m = document.cookie.match(
14
+ new RegExp(`(?:^|;\\s*)${THEME_COOKIE}=([^;]+)`)
15
+ );
16
+ const v = m ? decodeURIComponent(m[1]) : null;
17
+ return isThemePreference(v) ? v : null;
18
+ } catch {
19
+ return null;
20
+ }
21
+ }
22
+ function writeThemeCookie(pref) {
23
+ if (typeof document === "undefined" || typeof window === "undefined") return;
24
+ const host = window.location.hostname;
25
+ const onParentDomain = /(^|\.)politogyvrm\.com$/i.test(host);
26
+ const domainAttr = onParentDomain ? "; Domain=.politogyvrm.com" : "";
27
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
28
+ document.cookie = `${THEME_COOKIE}=${encodeURIComponent(
29
+ pref
30
+ )}; Path=/; Max-Age=31536000; SameSite=Lax${domainAttr}${secure}`;
31
+ }
32
+ function resolveTheme(pref) {
33
+ if (pref === "system") {
34
+ try {
35
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
36
+ } catch {
37
+ return "light";
38
+ }
39
+ }
40
+ return pref;
41
+ }
42
+ function applyThemePreference(pref) {
43
+ if (typeof document === "undefined") return;
44
+ const resolved = resolveTheme(pref);
45
+ const d = document.documentElement;
46
+ d.classList.toggle("dark", resolved === "dark");
47
+ try {
48
+ d.style.colorScheme = resolved;
49
+ } catch {
50
+ }
51
+ }
52
+ var THEME_PREPAINT_SCRIPT = `(function(){try{
53
+ var KEY=${JSON.stringify(THEME_STORAGE_KEY)};
54
+ var VALID=["light","dark","system"];
55
+ function fromCookie(){try{var m=document.cookie.match(/(?:^|;\\s*)${THEME_COOKIE}=([^;]+)/);return m?decodeURIComponent(m[1]):null;}catch(e){return null;}}
56
+ var cookie=fromCookie();
57
+ var ls=null;try{ls=localStorage.getItem(KEY);}catch(e){}
58
+ var pref=cookie||ls||"system";
59
+ if(VALID.indexOf(pref)===-1)pref="system";
60
+ if(cookie&&cookie!==ls){try{localStorage.setItem(KEY,cookie);}catch(e){}}
61
+ var resolved=pref;
62
+ if(pref==="system"){try{resolved=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light";}catch(e){resolved="light";}}
63
+ var d=document.documentElement;
64
+ if(resolved==="dark"){d.classList.add("dark");}else{d.classList.remove("dark");}
65
+ try{d.style.colorScheme=resolved;}catch(e){}
66
+ }catch(e){}})();`;
67
+
68
+ exports.THEME_COOKIE = THEME_COOKIE;
69
+ exports.THEME_PREPAINT_SCRIPT = THEME_PREPAINT_SCRIPT;
70
+ exports.THEME_STORAGE_KEY = THEME_STORAGE_KEY;
71
+ exports.applyThemePreference = applyThemePreference;
72
+ exports.isThemePreference = isThemePreference;
73
+ exports.readThemeCookie = readThemeCookie;
74
+ exports.resolveTheme = resolveTheme;
75
+ exports.writeThemeCookie = writeThemeCookie;
76
+ //# sourceMappingURL=theme-preference.cjs.map
77
+ //# sourceMappingURL=theme-preference.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/theme-preference.ts"],"names":[],"mappings":";;;AAqBO,IAAM,iBAAA,GAAoB;AAC1B,IAAM,YAAA,GAAe;AAE5B,IAAM,KAAA,GAAoC,CAAC,OAAA,EAAS,MAAA,EAAQ,QAAQ,CAAA;AAE7D,SAAS,kBAAkB,CAAA,EAAkC;AAClE,EAAA,OAAO,OAAO,CAAA,KAAM,QAAA,IAAa,KAAA,CAA4B,SAAS,CAAC,CAAA;AACzE;AAGO,SAAS,eAAA,GAA0C;AACxD,EAAA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EAAA,OAAO,IAAA;AACT;AAGO,SAAS,qBAAqB,IAAA,EAA6B;AAChE,EAAA,IAAI,OAAO,aAAa,WAAA,EAAa;AACrC,EAAA,MAAM,QAAA,GAAW,aAAa,IAAI,CAAA;AAClC,EAAA,MAAM,IAAI,QAAA,CAAS,eAAA;AACnB,EAAA,CAAA,CAAE,SAAA,CAAU,MAAA,CAAO,MAAA,EAAQ,QAAA,KAAa,MAAM,CAAA;AAC9C,EAAA,IAAI;AACF,IAAA,CAAA,CAAE,MAAM,WAAA,GAAc,QAAA;AAAA,EACxB,CAAA,CAAA,MAAQ;AAAA,EAER;AACF;AAQO,IAAM,qBAAA,GAAwB,CAAA;AAAA,QAAA,EAC3B,IAAA,CAAK,SAAA,CAAU,iBAAiB,CAAC,CAAA;AAAA;AAAA,kEAAA,EAEyB,YAAY,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAA","file":"theme-preference.cjs","sourcesContent":["// Universal theme preference — the ONE cross-app contract.\n//\n// One theme choice (Light / Dark / System) follows the authenticated user\n// across every Politogy app + subdomain. Three layers cooperate:\n// 1. VRM Core `profiles.theme_preference` — source of truth, persisted via\n// the chrome API (`PATCH me/preferences { themePreference }`) and read\n// back from `GET me` (`themePreference`).\n// 2. `.politogyvrm.com` cookie `politogy_theme` — zero-flash pre-paint\n// vehicle that crosses subdomains (auth session cookies are host-only).\n// 3. localStorage `politogy_theme` — offline fallback store.\n//\n// Ported verbatim from contacts/apps/web/src/lib/theme.ts (the origin of the\n// contract) so every app runs the identical behavior from npm. This module is\n// server-safe (no 'use client'): layouts import THEME_PREPAINT_SCRIPT for the\n// pre-paint inline script. The client-side hook lives in ./native\n// (useThemePreference).\n\nexport type ThemePreference = 'light' | 'dark' | 'system'\n\n// Single converged key for BOTH the localStorage store and the\n// cross-subdomain cookie, so a choice in one app is the same key in the next.\nexport const THEME_STORAGE_KEY = 'politogy_theme'\nexport const THEME_COOKIE = 'politogy_theme'\n\nconst VALID: readonly ThemePreference[] = ['light', 'dark', 'system']\n\nexport function isThemePreference(v: unknown): v is ThemePreference {\n return typeof v === 'string' && (VALID as readonly string[]).includes(v)\n}\n\n/** Read the shared cookie (browser only; null on the server or when unset). */\nexport function readThemeCookie(): ThemePreference | null {\n if (typeof document === 'undefined') return null\n try {\n const m = document.cookie.match(\n new RegExp(`(?:^|;\\\\s*)${THEME_COOKIE}=([^;]+)`),\n )\n const v = m ? decodeURIComponent(m[1]) : null\n return isThemePreference(v) ? v : null\n } catch {\n return null\n }\n}\n\n// Set the cross-subdomain cookie. On a *.politogyvrm.com host we scope it to\n// the parent domain so every app subdomain reads it; on any other host\n// (localhost, *.pages.dev preview) we fall back to a host-only cookie so\n// local/preview theming still works without a mismatched Domain attribute\n// (which browsers silently drop). 1-year, SameSite=Lax, Secure on https.\nexport function writeThemeCookie(pref: ThemePreference): void {\n if (typeof document === 'undefined' || typeof window === 'undefined') return\n const host = window.location.hostname\n const onParentDomain = /(^|\\.)politogyvrm\\.com$/i.test(host)\n const domainAttr = onParentDomain ? '; Domain=.politogyvrm.com' : ''\n const secure = window.location.protocol === 'https:' ? '; Secure' : ''\n document.cookie = `${THEME_COOKIE}=${encodeURIComponent(\n pref,\n )}; Path=/; Max-Age=31536000; SameSite=Lax${domainAttr}${secure}`\n}\n\n/** Resolve a preference to the concrete light/dark it renders as right now. */\nexport function resolveTheme(pref: ThemePreference): 'light' | 'dark' {\n if (pref === 'system') {\n try {\n return window.matchMedia('(prefers-color-scheme: dark)').matches\n ? 'dark'\n : 'light'\n } catch {\n return 'light'\n }\n }\n return pref\n}\n\n/** Apply a preference to the document (`.dark` class + color-scheme). */\nexport function applyThemePreference(pref: ThemePreference): void {\n if (typeof document === 'undefined') return\n const resolved = resolveTheme(pref)\n const d = document.documentElement\n d.classList.toggle('dark', resolved === 'dark')\n try {\n d.style.colorScheme = resolved\n } catch {\n /* non-fatal */\n }\n}\n\n// Inline pre-paint script — render it in <head> (or as the first child of\n// <body>) so the theme applies before first paint. Resolves the preference\n// from cookie → localStorage → OS, applies the `.dark` class + `color-scheme`,\n// and seeds localStorage from the cookie so the app hydrates to the synced\n// value (the cross-subdomain flash fix). Self-contained, dependency-free, and\n// wrapped so any failure is silent (never blocks paint).\nexport const THEME_PREPAINT_SCRIPT = `(function(){try{\nvar KEY=${JSON.stringify(THEME_STORAGE_KEY)};\nvar VALID=[\"light\",\"dark\",\"system\"];\nfunction fromCookie(){try{var m=document.cookie.match(/(?:^|;\\\\s*)${THEME_COOKIE}=([^;]+)/);return m?decodeURIComponent(m[1]):null;}catch(e){return null;}}\nvar cookie=fromCookie();\nvar ls=null;try{ls=localStorage.getItem(KEY);}catch(e){}\nvar pref=cookie||ls||\"system\";\nif(VALID.indexOf(pref)===-1)pref=\"system\";\nif(cookie&&cookie!==ls){try{localStorage.setItem(KEY,cookie);}catch(e){}}\nvar resolved=pref;\nif(pref===\"system\"){try{resolved=window.matchMedia(\"(prefers-color-scheme: dark)\").matches?\"dark\":\"light\";}catch(e){resolved=\"light\";}}\nvar d=document.documentElement;\nif(resolved===\"dark\"){d.classList.add(\"dark\");}else{d.classList.remove(\"dark\");}\ntry{d.style.colorScheme=resolved;}catch(e){}\n}catch(e){}})();`\n"]}
@@ -0,0 +1,14 @@
1
+ type ThemePreference = 'light' | 'dark' | 'system';
2
+ declare const THEME_STORAGE_KEY = "politogy_theme";
3
+ declare const THEME_COOKIE = "politogy_theme";
4
+ declare function isThemePreference(v: unknown): v is ThemePreference;
5
+ /** Read the shared cookie (browser only; null on the server or when unset). */
6
+ declare function readThemeCookie(): ThemePreference | null;
7
+ declare function writeThemeCookie(pref: ThemePreference): void;
8
+ /** Resolve a preference to the concrete light/dark it renders as right now. */
9
+ declare function resolveTheme(pref: ThemePreference): 'light' | 'dark';
10
+ /** Apply a preference to the document (`.dark` class + color-scheme). */
11
+ declare function applyThemePreference(pref: ThemePreference): void;
12
+ declare const THEME_PREPAINT_SCRIPT: string;
13
+
14
+ export { THEME_COOKIE, THEME_PREPAINT_SCRIPT, THEME_STORAGE_KEY, type ThemePreference, applyThemePreference, isThemePreference, readThemeCookie, resolveTheme, writeThemeCookie };
@@ -0,0 +1,14 @@
1
+ type ThemePreference = 'light' | 'dark' | 'system';
2
+ declare const THEME_STORAGE_KEY = "politogy_theme";
3
+ declare const THEME_COOKIE = "politogy_theme";
4
+ declare function isThemePreference(v: unknown): v is ThemePreference;
5
+ /** Read the shared cookie (browser only; null on the server or when unset). */
6
+ declare function readThemeCookie(): ThemePreference | null;
7
+ declare function writeThemeCookie(pref: ThemePreference): void;
8
+ /** Resolve a preference to the concrete light/dark it renders as right now. */
9
+ declare function resolveTheme(pref: ThemePreference): 'light' | 'dark';
10
+ /** Apply a preference to the document (`.dark` class + color-scheme). */
11
+ declare function applyThemePreference(pref: ThemePreference): void;
12
+ declare const THEME_PREPAINT_SCRIPT: string;
13
+
14
+ export { THEME_COOKIE, THEME_PREPAINT_SCRIPT, THEME_STORAGE_KEY, type ThemePreference, applyThemePreference, isThemePreference, readThemeCookie, resolveTheme, writeThemeCookie };
@@ -0,0 +1,3 @@
1
+ export { THEME_COOKIE, THEME_PREPAINT_SCRIPT, THEME_STORAGE_KEY, applyThemePreference, isThemePreference, readThemeCookie, resolveTheme, writeThemeCookie } from './chunk-ZZWM7BZZ.js';
2
+ //# sourceMappingURL=theme-preference.js.map
3
+ //# sourceMappingURL=theme-preference.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"theme-preference.js"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@the-portland-company/shell",
3
- "version": "0.16.0",
3
+ "version": "0.16.2",
4
4
  "description": "Shared chrome (header, sidebar, footer, theme, auth) for politogy apps. Drop into any Vite SPA to inherit the politogy look and signed-in user.",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -30,7 +30,12 @@
30
30
  "import": "./dist/native.js",
31
31
  "require": "./dist/native.cjs"
32
32
  },
33
- "./sw": "./dist/shell-precache.worker.js"
33
+ "./sw": "./dist/shell-precache.worker.js",
34
+ "./theme-preference": {
35
+ "types": "./dist/theme-preference.d.ts",
36
+ "import": "./dist/theme-preference.js",
37
+ "require": "./dist/theme-preference.cjs"
38
+ }
34
39
  },
35
40
  "files": [
36
41
  "dist",