@klodd/ds 5.8.0 → 5.9.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/SKILL.md CHANGED
@@ -7,10 +7,13 @@ description: Design memory for the @klodd/ds shared design system used by the tw
7
7
 
8
8
  ## Status
9
9
  Designsystem-auditen (2026-05-20, se `audits/00-summary.md`) slutförd -
10
- alla tre sprintar klara:
10
+ alla sex sprintar klara:
11
11
  - Sprint 1: dead-code-städning + topbar-bump (5.4.3)
12
12
  - Sprint 2: component-token-konsolidering, ADR 0024-backfill, light-theme-removal (5.5.0)
13
13
  - Sprint 3: base.css-extraktion till paketets base/interactions.css (5.6.0)
14
+ - Sprint 4: JS-extraktion (sheet + toast) + deprecated-selektor-grep i verify (5.7.0 + 5.7.1)
15
+ - Sprint 5: width/height-tokens (--measure-*, --content-max-*) + stylelint-regel no-literal-dimension (5.8.0)
16
+ - Sprint 6: clerk-init-extraktion (waitForClerkBase, clerkLogout, KloddDS.clerkReady, fetchPatch opt-in) + async-progress clerkReady-race-fix (5.9.0)
14
17
 
15
18
  ## Vad detta är
16
19
  Gemensamt designsystem på npm (`@klodd/ds@5.x`). Tre-lagers tokens,
@@ -91,12 +91,23 @@
91
91
  this.failed = false;
92
92
  this.consecutiveErrors = 0;
93
93
  if (this.pollTimer !== null) return;
94
- // Forsta poll efter 100ms sa ev. fetch-patches (auth-tokens etc)
95
- // hinner appliceras.
96
- setTimeout(() => this._poll(), 100);
94
+ // Forsta poll vantar in KloddDS.clerkReady (clerk-init.js) sa en
95
+ // ev. fetch-Bearer-patch ar installerad innan vi traffar /api/.
96
+ // Ersatter en tidigare blind setTimeout(_poll, 100)-gissning som
97
+ // tappade racet vid langsam Clerk.load(). Saknas clerkReady
98
+ // (clerk-init.js ej laddad) pollar _firstPoll direkt - graceful
99
+ // degradation.
100
+ this._firstPoll();
97
101
  this.pollTimer = setInterval(() => this._poll(), this.cfg.pollInterval);
98
102
  }
99
103
 
104
+ async _firstPoll() {
105
+ if (window.KloddDS && window.KloddDS.clerkReady) {
106
+ await window.KloddDS.clerkReady;
107
+ }
108
+ this._poll();
109
+ }
110
+
100
111
  stop() {
101
112
  if (this.pollTimer !== null) {
102
113
  clearInterval(this.pollTimer);
@@ -0,0 +1,109 @@
1
+ /* @klodd/ds - clerk-init
2
+ *
3
+ * Delad Clerk-initialisering for apparna som konsumerar @klodd/ds.
4
+ * Extraherad ur appernas app.js (tidigare byte-identiska kopior med
5
+ * tva divergenser: fetch-Bearer-patchen och logout-redirecten).
6
+ *
7
+ * Exponerar tre globals:
8
+ *
9
+ * window.waitForClerkBase(cb, attempts)
10
+ * Pollar window.Clerk var 200ms (max 50 forsok) och kor cb nar
11
+ * SDK:n finns. Anvands ocksa av page-login.js.
12
+ *
13
+ * window.clerkLogout()
14
+ * Async. signOut() + redirect till config.logoutRedirect. Satts
15
+ * upp av initClerk eftersom den behover config-vardet.
16
+ *
17
+ * KloddDS.clerkReady
18
+ * Promise som resolvar nar Clerk.load() ar klar. Resolvar i ett
19
+ * finally - alltsa aven om Clerk.load() kastar - sa konsumenter
20
+ * inte hanger for evigt vid en Clerk-storning. Resolvar oavsett
21
+ * fetchPatch-konfiguration. Representerar "Clerk ar redo", inte
22
+ * "patchen ar installerad". Tidiga /api/-fetchers (AsyncProgress)
23
+ * awaitar den istallet for att gissa en setTimeout-delay.
24
+ *
25
+ * KloddDS.initClerk(config) startar allt. config-falt:
26
+ *
27
+ * fetchPatch (default false)
28
+ * Om true: patcha window.fetch sa /api/-anrop far en
29
+ * Authorization: Bearer-header. Fallback nar cookies inte gar
30
+ * igenom (PWA-standalone pa iOS). Opt-in per app.
31
+ *
32
+ * logoutRedirect (default "/")
33
+ * Dit clerkLogout navigerar efter signOut().
34
+ *
35
+ * initClerk anropas via en liten extern per-app config-fil
36
+ * (clerk-config.js) - CSP utan 'unsafe-inline' tillater inte
37
+ * inline-script. defer-ordning garanterar att initClerk finns nar
38
+ * config-filen kor.
39
+ */
40
+ (function () {
41
+ "use strict";
42
+
43
+ window.KloddDS = window.KloddDS || {};
44
+
45
+ // clerkReady skapas direkt nar denna fil kor, sa konsumenter kan
46
+ // awaita den aven innan initClerk hunnit anropas. resolve-funktionen
47
+ // sparas sa initClerk-callbacken kan resolva den.
48
+ let resolveClerkReady;
49
+ window.KloddDS.clerkReady = new Promise(function (resolve) {
50
+ resolveClerkReady = resolve;
51
+ });
52
+
53
+ function waitForClerkBase(cb, attempts) {
54
+ if (window.Clerk) return cb();
55
+ if ((attempts || 0) > 50) return console.error("Clerk JS failed to load");
56
+ setTimeout(function () { waitForClerkBase(cb, (attempts || 0) + 1); }, 200);
57
+ }
58
+ window.waitForClerkBase = waitForClerkBase;
59
+
60
+ // Patchar window.fetch sa /api/-anrop far Authorization: Bearer som
61
+ // fallback nar cookies inte gar igenom (PWA-standalone pa iOS).
62
+ // Oforandrad logik fran Jubbs tidigare app.js-inline-version.
63
+ function installFetchPatch() {
64
+ if (!window.Clerk.session) return;
65
+ const originalFetch = window.fetch.bind(window);
66
+ window.fetch = async function (input, init) {
67
+ try {
68
+ const url = typeof input === "string" ? input : input.url;
69
+ if (url && (url.startsWith("/api/") || url.startsWith(window.location.origin + "/api/"))) {
70
+ const token = await window.Clerk.session.getToken();
71
+ if (token) {
72
+ init = init || {};
73
+ init.headers = new Headers(init.headers || {});
74
+ init.headers.set("Authorization", "Bearer " + token);
75
+ }
76
+ }
77
+ } catch (e) {
78
+ console.warn("Clerk token fetch failed", e);
79
+ }
80
+ return originalFetch(input, init);
81
+ };
82
+ }
83
+
84
+ function initClerk(config) {
85
+ const cfg = Object.assign(
86
+ { fetchPatch: false, logoutRedirect: "/" },
87
+ config || {}
88
+ );
89
+ waitForClerkBase(async function () {
90
+ try {
91
+ await window.Clerk.load();
92
+ if (cfg.fetchPatch) installFetchPatch();
93
+ } catch (e) {
94
+ console.log("Clerk init:", e);
95
+ } finally {
96
+ // finally: clerkReady resolvar aven om Clerk.load() kastade,
97
+ // sa await KloddDS.clerkReady aldrig hanger for evigt.
98
+ resolveClerkReady();
99
+ window.clerkLogout = async function () {
100
+ if (window.Clerk) {
101
+ await window.Clerk.signOut();
102
+ }
103
+ window.location.href = cfg.logoutRedirect;
104
+ };
105
+ }
106
+ }, 0);
107
+ }
108
+ window.KloddDS.initClerk = initClerk;
109
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@klodd/ds",
3
- "version": "5.8.0",
3
+ "version": "5.9.0",
4
4
  "description": "Klodd shared design system - tokens, components, JS",
5
5
  "main": "css/index.css",
6
6
  "bin": {