@dnzn/dxkit-theme 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +204 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.global.js +201 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +179 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Denizen. // dnzn.wei
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createCSSTheme: () => createCSSTheme
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_dxkit_settings = require("@dnzn/dxkit-settings");
|
|
27
|
+
function createCSSTheme(options = {}) {
|
|
28
|
+
const { themes = ["default"], defaultMode = "system", storageKey = "dxkit:theme" } = options;
|
|
29
|
+
let currentTheme = themes[0];
|
|
30
|
+
let currentMode = defaultMode;
|
|
31
|
+
let dx = null;
|
|
32
|
+
let settingsListener = null;
|
|
33
|
+
let syncing = false;
|
|
34
|
+
const modeHandlers = /* @__PURE__ */ new Set();
|
|
35
|
+
const themeHandlers = /* @__PURE__ */ new Set();
|
|
36
|
+
const mql = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
37
|
+
function resolveMode() {
|
|
38
|
+
if (currentMode !== "system") return currentMode;
|
|
39
|
+
return mql?.matches ? "dark" : "light";
|
|
40
|
+
}
|
|
41
|
+
function applyToDOM() {
|
|
42
|
+
if (typeof document === "undefined") return;
|
|
43
|
+
const el = document.documentElement;
|
|
44
|
+
el.setAttribute("data-theme", currentTheme);
|
|
45
|
+
el.setAttribute("data-mode", resolveMode());
|
|
46
|
+
}
|
|
47
|
+
function canUseStorage() {
|
|
48
|
+
try {
|
|
49
|
+
return typeof localStorage !== "undefined" && typeof localStorage.setItem === "function";
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function persist() {
|
|
55
|
+
if (!canUseStorage()) return;
|
|
56
|
+
try {
|
|
57
|
+
localStorage.setItem(
|
|
58
|
+
storageKey,
|
|
59
|
+
JSON.stringify({
|
|
60
|
+
theme: currentTheme,
|
|
61
|
+
mode: currentMode
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
} catch {
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function restore() {
|
|
68
|
+
if (!canUseStorage()) return;
|
|
69
|
+
try {
|
|
70
|
+
const raw = localStorage.getItem(storageKey);
|
|
71
|
+
if (!raw) return;
|
|
72
|
+
const saved = JSON.parse(raw);
|
|
73
|
+
if (saved.theme && themes.includes(saved.theme)) currentTheme = saved.theme;
|
|
74
|
+
if (saved.mode && ["light", "dark", "system"].includes(saved.mode)) currentMode = saved.mode;
|
|
75
|
+
} catch {
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function syncToSettings() {
|
|
79
|
+
if (!dx?.settings || syncing) return;
|
|
80
|
+
syncing = true;
|
|
81
|
+
dx.settings.set("theme", "theme", currentTheme);
|
|
82
|
+
dx.settings.set("theme", "mode", currentMode);
|
|
83
|
+
syncing = false;
|
|
84
|
+
}
|
|
85
|
+
function notifyModeChange() {
|
|
86
|
+
const resolved = resolveMode();
|
|
87
|
+
for (const handler of modeHandlers) handler(currentMode, resolved);
|
|
88
|
+
dx?.events.emit("dx:plugin:theme:changed", { theme: currentTheme, mode: currentMode, resolved });
|
|
89
|
+
syncToSettings();
|
|
90
|
+
}
|
|
91
|
+
function notifyThemeChange() {
|
|
92
|
+
for (const handler of themeHandlers) handler(currentTheme);
|
|
93
|
+
dx?.events.emit("dx:plugin:theme:changed", { theme: currentTheme, mode: currentMode, resolved: resolveMode() });
|
|
94
|
+
syncToSettings();
|
|
95
|
+
}
|
|
96
|
+
function onSystemChange() {
|
|
97
|
+
if (currentMode !== "system") return;
|
|
98
|
+
applyToDOM();
|
|
99
|
+
notifyModeChange();
|
|
100
|
+
}
|
|
101
|
+
function onSettingsChanged(event) {
|
|
102
|
+
if (event.dappId !== "theme" || syncing) return;
|
|
103
|
+
if (event.key === "theme" && typeof event.value === "string") {
|
|
104
|
+
plugin.setTheme(event.value);
|
|
105
|
+
} else if (event.key === "mode" && typeof event.value === "string") {
|
|
106
|
+
plugin.setMode(event.value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function buildSettings() {
|
|
110
|
+
const defs = [];
|
|
111
|
+
if (themes.length > 1) {
|
|
112
|
+
defs.push({
|
|
113
|
+
key: "theme",
|
|
114
|
+
label: "Theme",
|
|
115
|
+
type: "select",
|
|
116
|
+
default: themes[0],
|
|
117
|
+
description: "Color palette.",
|
|
118
|
+
options: themes.map((t) => ({ label: t.charAt(0).toUpperCase() + t.slice(1), value: t }))
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
defs.push({
|
|
122
|
+
key: "mode",
|
|
123
|
+
label: "Mode",
|
|
124
|
+
type: "select",
|
|
125
|
+
default: defaultMode,
|
|
126
|
+
description: "Light, dark, or match your system.",
|
|
127
|
+
options: [
|
|
128
|
+
{ label: "System", value: "system" },
|
|
129
|
+
{ label: "Light", value: "light" },
|
|
130
|
+
{ label: "Dark", value: "dark" }
|
|
131
|
+
]
|
|
132
|
+
});
|
|
133
|
+
return defs;
|
|
134
|
+
}
|
|
135
|
+
const plugin = {
|
|
136
|
+
name: "theme",
|
|
137
|
+
settings: buildSettings(),
|
|
138
|
+
async init(context) {
|
|
139
|
+
dx = context;
|
|
140
|
+
context.eventRegistry.registerEvent("theme", [{ name: "dx:plugin:theme:changed" }]);
|
|
141
|
+
restore();
|
|
142
|
+
applyToDOM();
|
|
143
|
+
mql?.addEventListener("change", onSystemChange);
|
|
144
|
+
syncToSettings();
|
|
145
|
+
settingsListener = dx.events.on("dx:plugin:settings:changed", onSettingsChanged);
|
|
146
|
+
},
|
|
147
|
+
async destroy() {
|
|
148
|
+
mql?.removeEventListener("change", onSystemChange);
|
|
149
|
+
if (settingsListener) {
|
|
150
|
+
settingsListener.off();
|
|
151
|
+
settingsListener = null;
|
|
152
|
+
}
|
|
153
|
+
modeHandlers.clear();
|
|
154
|
+
themeHandlers.clear();
|
|
155
|
+
dx = null;
|
|
156
|
+
},
|
|
157
|
+
getMode() {
|
|
158
|
+
return currentMode;
|
|
159
|
+
},
|
|
160
|
+
setMode(mode) {
|
|
161
|
+
if (currentMode === mode) return;
|
|
162
|
+
currentMode = mode;
|
|
163
|
+
applyToDOM();
|
|
164
|
+
persist();
|
|
165
|
+
notifyModeChange();
|
|
166
|
+
},
|
|
167
|
+
toggleMode() {
|
|
168
|
+
const cycle = ["system", "light", "dark"];
|
|
169
|
+
const idx = cycle.indexOf(currentMode);
|
|
170
|
+
plugin.setMode(cycle[(idx + 1) % cycle.length]);
|
|
171
|
+
},
|
|
172
|
+
getResolvedMode() {
|
|
173
|
+
return resolveMode();
|
|
174
|
+
},
|
|
175
|
+
onModeChange(handler) {
|
|
176
|
+
modeHandlers.add(handler);
|
|
177
|
+
return () => modeHandlers.delete(handler);
|
|
178
|
+
},
|
|
179
|
+
getTheme() {
|
|
180
|
+
return currentTheme;
|
|
181
|
+
},
|
|
182
|
+
setTheme(theme) {
|
|
183
|
+
if (currentTheme === theme) return;
|
|
184
|
+
if (!themes.includes(theme)) return;
|
|
185
|
+
currentTheme = theme;
|
|
186
|
+
applyToDOM();
|
|
187
|
+
persist();
|
|
188
|
+
notifyThemeChange();
|
|
189
|
+
},
|
|
190
|
+
getAvailableThemes() {
|
|
191
|
+
return [...themes];
|
|
192
|
+
},
|
|
193
|
+
onThemeChange(handler) {
|
|
194
|
+
themeHandlers.add(handler);
|
|
195
|
+
return () => themeHandlers.delete(handler);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
return plugin;
|
|
199
|
+
}
|
|
200
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
201
|
+
0 && (module.exports = {
|
|
202
|
+
createCSSTheme
|
|
203
|
+
});
|
|
204
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Context, SettingDefinition, Theme, ThemeMode } from '@dnzn/dxkit';\nimport '@dnzn/dxkit-settings';\n\ndeclare module '@dnzn/dxkit' {\n interface EventMap {\n 'dx:plugin:theme:changed': { theme: string; mode: ThemeMode; resolved: 'light' | 'dark' };\n }\n}\n\nexport interface CSSThemeOptions {\n /** Available theme names. First is the default. */\n themes?: string[];\n /** Initial mode. Default: 'system'. */\n defaultMode?: ThemeMode;\n /** localStorage key prefix. Default: 'dxkit:theme'. */\n storageKey?: string;\n}\n\n/**\n * Creates a CSS theme plugin.\n *\n * Sets `data-theme` and `data-mode` attributes on `<html>`.\n * Persists selection to localStorage. Respects `prefers-color-scheme`\n * when mode is 'system'.\n *\n * Declares settings so the settings plugin can render theme/mode controls.\n */\nexport function createCSSTheme(options: CSSThemeOptions = {}): Theme {\n const { themes = ['default'], defaultMode = 'system', storageKey = 'dxkit:theme' } = options;\n\n let currentTheme = themes[0];\n let currentMode: ThemeMode = defaultMode;\n let dx: Context | null = null;\n let settingsListener: { off(): void } | null = null;\n // Prevents re-entrant loop: theme change → settings write → settings event → theme change\n let syncing = false;\n\n const modeHandlers = new Set<(mode: ThemeMode, resolved: 'light' | 'dark') => void>();\n const themeHandlers = new Set<(theme: string) => void>();\n\n // Media query for system preference\n const mql = typeof window !== 'undefined' ? window.matchMedia('(prefers-color-scheme: dark)') : null;\n\n function resolveMode(): 'light' | 'dark' {\n if (currentMode !== 'system') return currentMode;\n return mql?.matches ? 'dark' : 'light';\n }\n\n function applyToDOM(): void {\n if (typeof document === 'undefined') return;\n const el = document.documentElement;\n el.setAttribute('data-theme', currentTheme);\n el.setAttribute('data-mode', resolveMode());\n }\n\n function canUseStorage(): boolean {\n try {\n return typeof localStorage !== 'undefined' && typeof localStorage.setItem === 'function';\n } catch {\n return false;\n }\n }\n\n function persist(): void {\n if (!canUseStorage()) return;\n try {\n localStorage.setItem(\n storageKey,\n JSON.stringify({\n theme: currentTheme,\n mode: currentMode,\n }),\n );\n } catch {\n /* storage full or blocked */\n }\n }\n\n function restore(): void {\n if (!canUseStorage()) return;\n try {\n const raw = localStorage.getItem(storageKey);\n if (!raw) return;\n const saved = JSON.parse(raw);\n if (saved.theme && themes.includes(saved.theme)) currentTheme = saved.theme;\n if (saved.mode && ['light', 'dark', 'system'].includes(saved.mode)) currentMode = saved.mode;\n } catch {\n /* corrupted — use defaults */\n }\n }\n\n /** Push current theme/mode into the settings store (if available). */\n function syncToSettings(): void {\n if (!dx?.settings || syncing) return;\n syncing = true;\n dx.settings.set('theme', 'theme', currentTheme);\n dx.settings.set('theme', 'mode', currentMode);\n syncing = false;\n }\n\n function notifyModeChange(): void {\n const resolved = resolveMode();\n for (const handler of modeHandlers) handler(currentMode, resolved);\n dx?.events.emit('dx:plugin:theme:changed', { theme: currentTheme, mode: currentMode, resolved });\n syncToSettings();\n }\n\n function notifyThemeChange(): void {\n for (const handler of themeHandlers) handler(currentTheme);\n dx?.events.emit('dx:plugin:theme:changed', { theme: currentTheme, mode: currentMode, resolved: resolveMode() });\n syncToSettings();\n }\n\n // System preference change listener\n function onSystemChange(): void {\n if (currentMode !== 'system') return;\n applyToDOM();\n notifyModeChange();\n }\n\n /** React to settings changes (from settings dapp UI). */\n function onSettingsChanged(event: { dappId: string; key: string; value: unknown }): void {\n if (event.dappId !== 'theme' || syncing) return;\n if (event.key === 'theme' && typeof event.value === 'string') {\n plugin.setTheme(event.value);\n } else if (event.key === 'mode' && typeof event.value === 'string') {\n plugin.setMode(event.value as ThemeMode);\n }\n }\n\n // Build settings definitions from the options\n function buildSettings(): SettingDefinition[] {\n const defs: SettingDefinition[] = [];\n\n if (themes.length > 1) {\n defs.push({\n key: 'theme',\n label: 'Theme',\n type: 'select',\n default: themes[0],\n description: 'Color palette.',\n options: themes.map((t) => ({ label: t.charAt(0).toUpperCase() + t.slice(1), value: t })),\n });\n }\n\n defs.push({\n key: 'mode',\n label: 'Mode',\n type: 'select',\n default: defaultMode,\n description: 'Light, dark, or match your system.',\n options: [\n { label: 'System', value: 'system' },\n { label: 'Light', value: 'light' },\n { label: 'Dark', value: 'dark' },\n ],\n });\n\n return defs;\n }\n\n const plugin: Theme = {\n name: 'theme',\n settings: buildSettings(),\n\n async init(context: Context): Promise<void> {\n dx = context;\n\n context.eventRegistry.registerEvent('theme', [{ name: 'dx:plugin:theme:changed' }]);\n\n restore();\n applyToDOM();\n mql?.addEventListener('change', onSystemChange);\n\n // Seed current values into settings store\n syncToSettings();\n\n // Listen for settings changes (from settings dapp)\n settingsListener = dx.events.on('dx:plugin:settings:changed', onSettingsChanged);\n },\n\n async destroy(): Promise<void> {\n mql?.removeEventListener('change', onSystemChange);\n if (settingsListener) {\n settingsListener.off();\n settingsListener = null;\n }\n modeHandlers.clear();\n themeHandlers.clear();\n dx = null;\n },\n\n getMode(): ThemeMode {\n return currentMode;\n },\n\n setMode(mode: ThemeMode): void {\n if (currentMode === mode) return;\n currentMode = mode;\n applyToDOM();\n persist();\n notifyModeChange();\n },\n\n toggleMode(): void {\n // Cycle order: system → light → dark → system\n const cycle: ThemeMode[] = ['system', 'light', 'dark'];\n const idx = cycle.indexOf(currentMode);\n plugin.setMode(cycle[(idx + 1) % cycle.length]);\n },\n\n getResolvedMode(): 'light' | 'dark' {\n return resolveMode();\n },\n\n onModeChange(handler: (mode: ThemeMode, resolved: 'light' | 'dark') => void): () => void {\n modeHandlers.add(handler);\n return () => modeHandlers.delete(handler);\n },\n\n getTheme(): string {\n return currentTheme;\n },\n\n setTheme(theme: string): void {\n if (currentTheme === theme) return;\n if (!themes.includes(theme)) return;\n currentTheme = theme;\n applyToDOM();\n persist();\n notifyThemeChange();\n },\n\n getAvailableThemes(): string[] {\n return [...themes];\n },\n\n onThemeChange(handler: (theme: string) => void): () => void {\n themeHandlers.add(handler);\n return () => themeHandlers.delete(handler);\n },\n };\n\n return plugin;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,4BAAO;AA0BA,SAAS,eAAe,UAA2B,CAAC,GAAU;AACnE,QAAM,EAAE,SAAS,CAAC,SAAS,GAAG,cAAc,UAAU,aAAa,cAAc,IAAI;AAErF,MAAI,eAAe,OAAO,CAAC;AAC3B,MAAI,cAAyB;AAC7B,MAAI,KAAqB;AACzB,MAAI,mBAA2C;AAE/C,MAAI,UAAU;AAEd,QAAM,eAAe,oBAAI,IAA2D;AACpF,QAAM,gBAAgB,oBAAI,IAA6B;AAGvD,QAAM,MAAM,OAAO,WAAW,cAAc,OAAO,WAAW,8BAA8B,IAAI;AAEhG,WAAS,cAAgC;AACvC,QAAI,gBAAgB,SAAU,QAAO;AACrC,WAAO,KAAK,UAAU,SAAS;AAAA,EACjC;AAEA,WAAS,aAAmB;AAC1B,QAAI,OAAO,aAAa,YAAa;AACrC,UAAM,KAAK,SAAS;AACpB,OAAG,aAAa,cAAc,YAAY;AAC1C,OAAG,aAAa,aAAa,YAAY,CAAC;AAAA,EAC5C;AAEA,WAAS,gBAAyB;AAChC,QAAI;AACF,aAAO,OAAO,iBAAiB,eAAe,OAAO,aAAa,YAAY;AAAA,IAChF,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAE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oBAAoB,UAAU,cAAc;AACjD,UAAI,kBAAkB;AACpB,yBAAiB,IAAI;AACrB,2BAAmB;AAAA,MACrB;AACA,mBAAa,MAAM;AACnB,oBAAc,MAAM;AACpB,WAAK;AAAA,IACP;AAAA,IAEA,UAAqB;AACnB,aAAO;AAAA,IACT;AAAA,IAEA,QAAQ,MAAuB;AAC7B,UAAI,gBAAgB,KAAM;AAC1B,oBAAc;AACd,iBAAW;AACX,cAAQ;AACR,uBAAiB;AAAA,IACnB;AAAA,IAEA,aAAmB;AAEjB,YAAM,QAAqB,CAAC,UAAU,SAAS,MAAM;AACrD,YAAM,MAAM,MAAM,QAAQ,WAAW;AACrC,aAAO,QAAQ,OAAO,MAAM,KAAK,MAAM,MAAM,CAAC;AAAA,IAChD;AAAA,IAEA,kBAAoC;AAClC,aAAO,YAAY;AAAA,IACrB;AAAA,IAEA,aAAa,SAA4E;AACvF,mBAAa,IAAI,OAAO;AACxB,aAAO,MAAM,aAAa,OAAO,OAAO;AAAA,IAC1C;AAAA,IAEA,WAAmB;AACjB,aAAO;AAAA,IACT;AAAA,IAEA,SAAS,OAAqB;AAC5B,UAAI,iBAAiB,MAAO;AAC5B,UAAI,CAAC,OAAO,SAAS,KAAK,EAAG;AAC7B,qBAAe;AACf,iBAAW;AACX,cAAQ;AACR,wBAAkB;AAAA,IACpB;AAAA,IAEA,qBAA+B;AAC7B,aAAO,CAAC,GAAG,MAAM;AAAA,IACnB;AAAA,IAEA,cAAc,SAA8C;AAC1D,oBAAc,IAAI,OAAO;AACzB,aAAO,MAAM,cAAc,OAAO,OAAO;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ThemeMode, Theme } from '@dnzn/dxkit';
|
|
2
|
+
|
|
3
|
+
declare module '@dnzn/dxkit' {
|
|
4
|
+
interface EventMap {
|
|
5
|
+
'dx:plugin:theme:changed': {
|
|
6
|
+
theme: string;
|
|
7
|
+
mode: ThemeMode;
|
|
8
|
+
resolved: 'light' | 'dark';
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
interface CSSThemeOptions {
|
|
13
|
+
/** Available theme names. First is the default. */
|
|
14
|
+
themes?: string[];
|
|
15
|
+
/** Initial mode. Default: 'system'. */
|
|
16
|
+
defaultMode?: ThemeMode;
|
|
17
|
+
/** localStorage key prefix. Default: 'dxkit:theme'. */
|
|
18
|
+
storageKey?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a CSS theme plugin.
|
|
22
|
+
*
|
|
23
|
+
* Sets `data-theme` and `data-mode` attributes on `<html>`.
|
|
24
|
+
* Persists selection to localStorage. Respects `prefers-color-scheme`
|
|
25
|
+
* when mode is 'system'.
|
|
26
|
+
*
|
|
27
|
+
* Declares settings so the settings plugin can render theme/mode controls.
|
|
28
|
+
*/
|
|
29
|
+
declare function createCSSTheme(options?: CSSThemeOptions): Theme;
|
|
30
|
+
|
|
31
|
+
export { type CSSThemeOptions, createCSSTheme };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ThemeMode, Theme } from '@dnzn/dxkit';
|
|
2
|
+
|
|
3
|
+
declare module '@dnzn/dxkit' {
|
|
4
|
+
interface EventMap {
|
|
5
|
+
'dx:plugin:theme:changed': {
|
|
6
|
+
theme: string;
|
|
7
|
+
mode: ThemeMode;
|
|
8
|
+
resolved: 'light' | 'dark';
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
interface CSSThemeOptions {
|
|
13
|
+
/** Available theme names. First is the default. */
|
|
14
|
+
themes?: string[];
|
|
15
|
+
/** Initial mode. Default: 'system'. */
|
|
16
|
+
defaultMode?: ThemeMode;
|
|
17
|
+
/** localStorage key prefix. Default: 'dxkit:theme'. */
|
|
18
|
+
storageKey?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a CSS theme plugin.
|
|
22
|
+
*
|
|
23
|
+
* Sets `data-theme` and `data-mode` attributes on `<html>`.
|
|
24
|
+
* Persists selection to localStorage. Respects `prefers-color-scheme`
|
|
25
|
+
* when mode is 'system'.
|
|
26
|
+
*
|
|
27
|
+
* Declares settings so the settings plugin can render theme/mode controls.
|
|
28
|
+
*/
|
|
29
|
+
declare function createCSSTheme(options?: CSSThemeOptions): Theme;
|
|
30
|
+
|
|
31
|
+
export { type CSSThemeOptions, createCSSTheme };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var DxTheme = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
createCSSTheme: () => createCSSTheme
|
|
25
|
+
});
|
|
26
|
+
function createCSSTheme(options = {}) {
|
|
27
|
+
const { themes = ["default"], defaultMode = "system", storageKey = "dxkit:theme" } = options;
|
|
28
|
+
let currentTheme = themes[0];
|
|
29
|
+
let currentMode = defaultMode;
|
|
30
|
+
let dx = null;
|
|
31
|
+
let settingsListener = null;
|
|
32
|
+
let syncing = false;
|
|
33
|
+
const modeHandlers = /* @__PURE__ */ new Set();
|
|
34
|
+
const themeHandlers = /* @__PURE__ */ new Set();
|
|
35
|
+
const mql = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
36
|
+
function resolveMode() {
|
|
37
|
+
if (currentMode !== "system") return currentMode;
|
|
38
|
+
return mql?.matches ? "dark" : "light";
|
|
39
|
+
}
|
|
40
|
+
function applyToDOM() {
|
|
41
|
+
if (typeof document === "undefined") return;
|
|
42
|
+
const el = document.documentElement;
|
|
43
|
+
el.setAttribute("data-theme", currentTheme);
|
|
44
|
+
el.setAttribute("data-mode", resolveMode());
|
|
45
|
+
}
|
|
46
|
+
function canUseStorage() {
|
|
47
|
+
try {
|
|
48
|
+
return typeof localStorage !== "undefined" && typeof localStorage.setItem === "function";
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function persist() {
|
|
54
|
+
if (!canUseStorage()) return;
|
|
55
|
+
try {
|
|
56
|
+
localStorage.setItem(
|
|
57
|
+
storageKey,
|
|
58
|
+
JSON.stringify({
|
|
59
|
+
theme: currentTheme,
|
|
60
|
+
mode: currentMode
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function restore() {
|
|
67
|
+
if (!canUseStorage()) return;
|
|
68
|
+
try {
|
|
69
|
+
const raw = localStorage.getItem(storageKey);
|
|
70
|
+
if (!raw) return;
|
|
71
|
+
const saved = JSON.parse(raw);
|
|
72
|
+
if (saved.theme && themes.includes(saved.theme)) currentTheme = saved.theme;
|
|
73
|
+
if (saved.mode && ["light", "dark", "system"].includes(saved.mode)) currentMode = saved.mode;
|
|
74
|
+
} catch {
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function syncToSettings() {
|
|
78
|
+
if (!dx?.settings || syncing) return;
|
|
79
|
+
syncing = true;
|
|
80
|
+
dx.settings.set("theme", "theme", currentTheme);
|
|
81
|
+
dx.settings.set("theme", "mode", currentMode);
|
|
82
|
+
syncing = false;
|
|
83
|
+
}
|
|
84
|
+
function notifyModeChange() {
|
|
85
|
+
const resolved = resolveMode();
|
|
86
|
+
for (const handler of modeHandlers) handler(currentMode, resolved);
|
|
87
|
+
dx?.events.emit("dx:plugin:theme:changed", { theme: currentTheme, mode: currentMode, resolved });
|
|
88
|
+
syncToSettings();
|
|
89
|
+
}
|
|
90
|
+
function notifyThemeChange() {
|
|
91
|
+
for (const handler of themeHandlers) handler(currentTheme);
|
|
92
|
+
dx?.events.emit("dx:plugin:theme:changed", { theme: currentTheme, mode: currentMode, resolved: resolveMode() });
|
|
93
|
+
syncToSettings();
|
|
94
|
+
}
|
|
95
|
+
function onSystemChange() {
|
|
96
|
+
if (currentMode !== "system") return;
|
|
97
|
+
applyToDOM();
|
|
98
|
+
notifyModeChange();
|
|
99
|
+
}
|
|
100
|
+
function onSettingsChanged(event) {
|
|
101
|
+
if (event.dappId !== "theme" || syncing) return;
|
|
102
|
+
if (event.key === "theme" && typeof event.value === "string") {
|
|
103
|
+
plugin.setTheme(event.value);
|
|
104
|
+
} else if (event.key === "mode" && typeof event.value === "string") {
|
|
105
|
+
plugin.setMode(event.value);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function buildSettings() {
|
|
109
|
+
const defs = [];
|
|
110
|
+
if (themes.length > 1) {
|
|
111
|
+
defs.push({
|
|
112
|
+
key: "theme",
|
|
113
|
+
label: "Theme",
|
|
114
|
+
type: "select",
|
|
115
|
+
default: themes[0],
|
|
116
|
+
description: "Color palette.",
|
|
117
|
+
options: themes.map((t) => ({ label: t.charAt(0).toUpperCase() + t.slice(1), value: t }))
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
defs.push({
|
|
121
|
+
key: "mode",
|
|
122
|
+
label: "Mode",
|
|
123
|
+
type: "select",
|
|
124
|
+
default: defaultMode,
|
|
125
|
+
description: "Light, dark, or match your system.",
|
|
126
|
+
options: [
|
|
127
|
+
{ label: "System", value: "system" },
|
|
128
|
+
{ label: "Light", value: "light" },
|
|
129
|
+
{ label: "Dark", value: "dark" }
|
|
130
|
+
]
|
|
131
|
+
});
|
|
132
|
+
return defs;
|
|
133
|
+
}
|
|
134
|
+
const plugin = {
|
|
135
|
+
name: "theme",
|
|
136
|
+
settings: buildSettings(),
|
|
137
|
+
async init(context) {
|
|
138
|
+
dx = context;
|
|
139
|
+
context.eventRegistry.registerEvent("theme", [{ name: "dx:plugin:theme:changed" }]);
|
|
140
|
+
restore();
|
|
141
|
+
applyToDOM();
|
|
142
|
+
mql?.addEventListener("change", onSystemChange);
|
|
143
|
+
syncToSettings();
|
|
144
|
+
settingsListener = dx.events.on("dx:plugin:settings:changed", onSettingsChanged);
|
|
145
|
+
},
|
|
146
|
+
async destroy() {
|
|
147
|
+
mql?.removeEventListener("change", onSystemChange);
|
|
148
|
+
if (settingsListener) {
|
|
149
|
+
settingsListener.off();
|
|
150
|
+
settingsListener = null;
|
|
151
|
+
}
|
|
152
|
+
modeHandlers.clear();
|
|
153
|
+
themeHandlers.clear();
|
|
154
|
+
dx = null;
|
|
155
|
+
},
|
|
156
|
+
getMode() {
|
|
157
|
+
return currentMode;
|
|
158
|
+
},
|
|
159
|
+
setMode(mode) {
|
|
160
|
+
if (currentMode === mode) return;
|
|
161
|
+
currentMode = mode;
|
|
162
|
+
applyToDOM();
|
|
163
|
+
persist();
|
|
164
|
+
notifyModeChange();
|
|
165
|
+
},
|
|
166
|
+
toggleMode() {
|
|
167
|
+
const cycle = ["system", "light", "dark"];
|
|
168
|
+
const idx = cycle.indexOf(currentMode);
|
|
169
|
+
plugin.setMode(cycle[(idx + 1) % cycle.length]);
|
|
170
|
+
},
|
|
171
|
+
getResolvedMode() {
|
|
172
|
+
return resolveMode();
|
|
173
|
+
},
|
|
174
|
+
onModeChange(handler) {
|
|
175
|
+
modeHandlers.add(handler);
|
|
176
|
+
return () => modeHandlers.delete(handler);
|
|
177
|
+
},
|
|
178
|
+
getTheme() {
|
|
179
|
+
return currentTheme;
|
|
180
|
+
},
|
|
181
|
+
setTheme(theme) {
|
|
182
|
+
if (currentTheme === theme) return;
|
|
183
|
+
if (!themes.includes(theme)) return;
|
|
184
|
+
currentTheme = theme;
|
|
185
|
+
applyToDOM();
|
|
186
|
+
persist();
|
|
187
|
+
notifyThemeChange();
|
|
188
|
+
},
|
|
189
|
+
getAvailableThemes() {
|
|
190
|
+
return [...themes];
|
|
191
|
+
},
|
|
192
|
+
onThemeChange(handler) {
|
|
193
|
+
themeHandlers.add(handler);
|
|
194
|
+
return () => themeHandlers.delete(handler);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
return plugin;
|
|
198
|
+
}
|
|
199
|
+
return __toCommonJS(index_exports);
|
|
200
|
+
})();
|
|
201
|
+
//# sourceMappingURL=index.global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Context, SettingDefinition, Theme, ThemeMode } from '@dnzn/dxkit';\nimport '@dnzn/dxkit-settings';\n\ndeclare module '@dnzn/dxkit' {\n interface EventMap {\n 'dx:plugin:theme:changed': { theme: string; mode: ThemeMode; resolved: 'light' | 'dark' };\n }\n}\n\nexport interface CSSThemeOptions {\n /** Available theme names. First is the default. */\n themes?: string[];\n /** Initial mode. Default: 'system'. */\n defaultMode?: ThemeMode;\n /** localStorage key prefix. Default: 'dxkit:theme'. */\n storageKey?: string;\n}\n\n/**\n * Creates a CSS theme plugin.\n *\n * Sets `data-theme` and `data-mode` attributes on `<html>`.\n * Persists selection to localStorage. Respects `prefers-color-scheme`\n * when mode is 'system'.\n *\n * Declares settings so the settings plugin can render theme/mode controls.\n */\nexport function createCSSTheme(options: CSSThemeOptions = {}): Theme {\n const { themes = ['default'], defaultMode = 'system', storageKey = 'dxkit:theme' } = options;\n\n let currentTheme = themes[0];\n let currentMode: ThemeMode = defaultMode;\n let dx: Context | null = null;\n let settingsListener: { off(): void } | null = null;\n // Prevents re-entrant loop: theme change → settings write → settings event → theme change\n let syncing = false;\n\n const modeHandlers = new Set<(mode: ThemeMode, resolved: 'light' | 'dark') => void>();\n const themeHandlers = new Set<(theme: string) => void>();\n\n // Media query for system preference\n const mql = typeof window !== 'undefined' ? window.matchMedia('(prefers-color-scheme: dark)') : null;\n\n function resolveMode(): 'light' | 'dark' {\n if (currentMode !== 'system') return currentMode;\n return mql?.matches ? 'dark' : 'light';\n }\n\n function applyToDOM(): void {\n if (typeof document === 'undefined') return;\n const el = document.documentElement;\n el.setAttribute('data-theme', currentTheme);\n el.setAttribute('data-mode', resolveMode());\n }\n\n function canUseStorage(): boolean {\n try {\n return typeof localStorage !== 'undefined' && typeof localStorage.setItem === 'function';\n } catch {\n return false;\n }\n }\n\n function persist(): void {\n if (!canUseStorage()) return;\n try {\n localStorage.setItem(\n storageKey,\n JSON.stringify({\n theme: currentTheme,\n mode: currentMode,\n }),\n );\n } catch {\n /* storage full or blocked */\n }\n }\n\n function restore(): void {\n if (!canUseStorage()) return;\n try {\n const raw = localStorage.getItem(storageKey);\n if (!raw) return;\n const saved = JSON.parse(raw);\n if (saved.theme && themes.includes(saved.theme)) currentTheme = saved.theme;\n if (saved.mode && ['light', 'dark', 'system'].includes(saved.mode)) currentMode = saved.mode;\n } catch {\n /* corrupted — use defaults */\n }\n }\n\n /** Push current theme/mode into the settings store (if available). */\n function syncToSettings(): void {\n if (!dx?.settings || syncing) return;\n syncing = true;\n dx.settings.set('theme', 'theme', currentTheme);\n dx.settings.set('theme', 'mode', currentMode);\n syncing = false;\n }\n\n function notifyModeChange(): void {\n const resolved = resolveMode();\n for (const handler of modeHandlers) handler(currentMode, resolved);\n dx?.events.emit('dx:plugin:theme:changed', { theme: currentTheme, mode: currentMode, resolved });\n syncToSettings();\n }\n\n function notifyThemeChange(): void {\n for (const handler of themeHandlers) handler(currentTheme);\n dx?.events.emit('dx:plugin:theme:changed', { theme: currentTheme, mode: currentMode, resolved: resolveMode() });\n syncToSettings();\n }\n\n // System preference change listener\n function onSystemChange(): void {\n if (currentMode !== 'system') return;\n applyToDOM();\n notifyModeChange();\n }\n\n /** React to settings changes (from settings dapp UI). */\n function onSettingsChanged(event: { dappId: string; key: string; value: unknown }): void {\n if (event.dappId !== 'theme' || syncing) return;\n if (event.key === 'theme' && typeof event.value === 'string') {\n plugin.setTheme(event.value);\n } else if (event.key === 'mode' && typeof event.value === 'string') {\n plugin.setMode(event.value as ThemeMode);\n }\n }\n\n // Build settings definitions from the options\n function buildSettings(): SettingDefinition[] {\n const defs: SettingDefinition[] = [];\n\n if (themes.length > 1) {\n defs.push({\n key: 'theme',\n label: 'Theme',\n type: 'select',\n default: themes[0],\n description: 'Color palette.',\n options: themes.map((t) => ({ label: t.charAt(0).toUpperCase() + t.slice(1), value: t })),\n });\n }\n\n defs.push({\n key: 'mode',\n label: 'Mode',\n type: 'select',\n default: defaultMode,\n description: 'Light, dark, or match your system.',\n options: [\n { label: 'System', value: 'system' },\n { label: 'Light', value: 'light' },\n { label: 'Dark', value: 'dark' },\n ],\n });\n\n return defs;\n }\n\n const plugin: Theme = {\n name: 'theme',\n settings: buildSettings(),\n\n async init(context: Context): Promise<void> {\n dx = context;\n\n context.eventRegistry.registerEvent('theme', [{ name: 'dx:plugin:theme:changed' }]);\n\n restore();\n applyToDOM();\n mql?.addEventListener('change', onSystemChange);\n\n // Seed current values into settings store\n syncToSettings();\n\n // Listen for settings changes (from settings dapp)\n settingsListener = dx.events.on('dx:plugin:settings:changed', onSettingsChanged);\n },\n\n async destroy(): Promise<void> {\n mql?.removeEventListener('change', onSystemChange);\n if (settingsListener) {\n settingsListener.off();\n settingsListener = null;\n }\n modeHandlers.clear();\n themeHandlers.clear();\n dx = null;\n },\n\n getMode(): ThemeMode {\n return currentMode;\n },\n\n setMode(mode: ThemeMode): void {\n if (currentMode === mode) return;\n currentMode = mode;\n applyToDOM();\n persist();\n notifyModeChange();\n },\n\n toggleMode(): void {\n // Cycle order: system → light → dark → system\n const cycle: ThemeMode[] = ['system', 'light', 'dark'];\n const idx = cycle.indexOf(currentMode);\n plugin.setMode(cycle[(idx + 1) % cycle.length]);\n },\n\n getResolvedMode(): 'light' | 'dark' {\n return resolveMode();\n },\n\n onModeChange(handler: (mode: ThemeMode, resolved: 'light' | 'dark') => void): () => void {\n modeHandlers.add(handler);\n return () => modeHandlers.delete(handler);\n },\n\n getTheme(): string {\n return currentTheme;\n },\n\n setTheme(theme: string): void {\n if (currentTheme === theme) return;\n if (!themes.includes(theme)) return;\n currentTheme = theme;\n applyToDOM();\n persist();\n notifyThemeChange();\n },\n\n getAvailableThemes(): string[] {\n return [...themes];\n },\n\n onThemeChange(handler: (theme: string) => void): () => void {\n themeHandlers.add(handler);\n return () => themeHandlers.delete(handler);\n },\n };\n\n return plugin;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AA2BO,WAAS,eAAe,UAA2B,CAAC,GAAU;AACnE,UAAM,EAAE,SAAS,CAAC,SAAS,GAAG,cAAc,UAAU,aAAa,cAAc,IAAI;AAErF,QAAI,eAAe,OAAO,CAAC;AAC3B,QAAI,cAAyB;AAC7B,QAAI,KAAqB;AACzB,QAAI,mBAA2C;AAE/C,QAAI,UAAU;AAEd,UAAM,eAAe,oBAAI,IAA2D;AACpF,UAAM,gBAAgB,oBAAI,IAA6B;AAGvD,UAAM,MAAM,OAAO,WAAW,cAAc,OAAO,WAAW,8BAA8B,IAAI;AAEhG,aAAS,cAAgC;AACvC,UAAI,gBAAgB,SAAU,QAAO;AACrC,aAAO,KAAK,UAAU,SAAS;AAAA,IACjC;AAEA,aAAS,aAAmB;AAC1B,UAAI,OAAO,aAAa,YAAa;AACrC,YAAM,KAAK,SAAS;AACpB,SAAG,aAAa,cAAc,YAAY;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}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import "@dnzn/dxkit-settings";
|
|
3
|
+
function createCSSTheme(options = {}) {
|
|
4
|
+
const { themes = ["default"], defaultMode = "system", storageKey = "dxkit:theme" } = options;
|
|
5
|
+
let currentTheme = themes[0];
|
|
6
|
+
let currentMode = defaultMode;
|
|
7
|
+
let dx = null;
|
|
8
|
+
let settingsListener = null;
|
|
9
|
+
let syncing = false;
|
|
10
|
+
const modeHandlers = /* @__PURE__ */ new Set();
|
|
11
|
+
const themeHandlers = /* @__PURE__ */ new Set();
|
|
12
|
+
const mql = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
13
|
+
function resolveMode() {
|
|
14
|
+
if (currentMode !== "system") return currentMode;
|
|
15
|
+
return mql?.matches ? "dark" : "light";
|
|
16
|
+
}
|
|
17
|
+
function applyToDOM() {
|
|
18
|
+
if (typeof document === "undefined") return;
|
|
19
|
+
const el = document.documentElement;
|
|
20
|
+
el.setAttribute("data-theme", currentTheme);
|
|
21
|
+
el.setAttribute("data-mode", resolveMode());
|
|
22
|
+
}
|
|
23
|
+
function canUseStorage() {
|
|
24
|
+
try {
|
|
25
|
+
return typeof localStorage !== "undefined" && typeof localStorage.setItem === "function";
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function persist() {
|
|
31
|
+
if (!canUseStorage()) return;
|
|
32
|
+
try {
|
|
33
|
+
localStorage.setItem(
|
|
34
|
+
storageKey,
|
|
35
|
+
JSON.stringify({
|
|
36
|
+
theme: currentTheme,
|
|
37
|
+
mode: currentMode
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
} catch {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function restore() {
|
|
44
|
+
if (!canUseStorage()) return;
|
|
45
|
+
try {
|
|
46
|
+
const raw = localStorage.getItem(storageKey);
|
|
47
|
+
if (!raw) return;
|
|
48
|
+
const saved = JSON.parse(raw);
|
|
49
|
+
if (saved.theme && themes.includes(saved.theme)) currentTheme = saved.theme;
|
|
50
|
+
if (saved.mode && ["light", "dark", "system"].includes(saved.mode)) currentMode = saved.mode;
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function syncToSettings() {
|
|
55
|
+
if (!dx?.settings || syncing) return;
|
|
56
|
+
syncing = true;
|
|
57
|
+
dx.settings.set("theme", "theme", currentTheme);
|
|
58
|
+
dx.settings.set("theme", "mode", currentMode);
|
|
59
|
+
syncing = false;
|
|
60
|
+
}
|
|
61
|
+
function notifyModeChange() {
|
|
62
|
+
const resolved = resolveMode();
|
|
63
|
+
for (const handler of modeHandlers) handler(currentMode, resolved);
|
|
64
|
+
dx?.events.emit("dx:plugin:theme:changed", { theme: currentTheme, mode: currentMode, resolved });
|
|
65
|
+
syncToSettings();
|
|
66
|
+
}
|
|
67
|
+
function notifyThemeChange() {
|
|
68
|
+
for (const handler of themeHandlers) handler(currentTheme);
|
|
69
|
+
dx?.events.emit("dx:plugin:theme:changed", { theme: currentTheme, mode: currentMode, resolved: resolveMode() });
|
|
70
|
+
syncToSettings();
|
|
71
|
+
}
|
|
72
|
+
function onSystemChange() {
|
|
73
|
+
if (currentMode !== "system") return;
|
|
74
|
+
applyToDOM();
|
|
75
|
+
notifyModeChange();
|
|
76
|
+
}
|
|
77
|
+
function onSettingsChanged(event) {
|
|
78
|
+
if (event.dappId !== "theme" || syncing) return;
|
|
79
|
+
if (event.key === "theme" && typeof event.value === "string") {
|
|
80
|
+
plugin.setTheme(event.value);
|
|
81
|
+
} else if (event.key === "mode" && typeof event.value === "string") {
|
|
82
|
+
plugin.setMode(event.value);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function buildSettings() {
|
|
86
|
+
const defs = [];
|
|
87
|
+
if (themes.length > 1) {
|
|
88
|
+
defs.push({
|
|
89
|
+
key: "theme",
|
|
90
|
+
label: "Theme",
|
|
91
|
+
type: "select",
|
|
92
|
+
default: themes[0],
|
|
93
|
+
description: "Color palette.",
|
|
94
|
+
options: themes.map((t) => ({ label: t.charAt(0).toUpperCase() + t.slice(1), value: t }))
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
defs.push({
|
|
98
|
+
key: "mode",
|
|
99
|
+
label: "Mode",
|
|
100
|
+
type: "select",
|
|
101
|
+
default: defaultMode,
|
|
102
|
+
description: "Light, dark, or match your system.",
|
|
103
|
+
options: [
|
|
104
|
+
{ label: "System", value: "system" },
|
|
105
|
+
{ label: "Light", value: "light" },
|
|
106
|
+
{ label: "Dark", value: "dark" }
|
|
107
|
+
]
|
|
108
|
+
});
|
|
109
|
+
return defs;
|
|
110
|
+
}
|
|
111
|
+
const plugin = {
|
|
112
|
+
name: "theme",
|
|
113
|
+
settings: buildSettings(),
|
|
114
|
+
async init(context) {
|
|
115
|
+
dx = context;
|
|
116
|
+
context.eventRegistry.registerEvent("theme", [{ name: "dx:plugin:theme:changed" }]);
|
|
117
|
+
restore();
|
|
118
|
+
applyToDOM();
|
|
119
|
+
mql?.addEventListener("change", onSystemChange);
|
|
120
|
+
syncToSettings();
|
|
121
|
+
settingsListener = dx.events.on("dx:plugin:settings:changed", onSettingsChanged);
|
|
122
|
+
},
|
|
123
|
+
async destroy() {
|
|
124
|
+
mql?.removeEventListener("change", onSystemChange);
|
|
125
|
+
if (settingsListener) {
|
|
126
|
+
settingsListener.off();
|
|
127
|
+
settingsListener = null;
|
|
128
|
+
}
|
|
129
|
+
modeHandlers.clear();
|
|
130
|
+
themeHandlers.clear();
|
|
131
|
+
dx = null;
|
|
132
|
+
},
|
|
133
|
+
getMode() {
|
|
134
|
+
return currentMode;
|
|
135
|
+
},
|
|
136
|
+
setMode(mode) {
|
|
137
|
+
if (currentMode === mode) return;
|
|
138
|
+
currentMode = mode;
|
|
139
|
+
applyToDOM();
|
|
140
|
+
persist();
|
|
141
|
+
notifyModeChange();
|
|
142
|
+
},
|
|
143
|
+
toggleMode() {
|
|
144
|
+
const cycle = ["system", "light", "dark"];
|
|
145
|
+
const idx = cycle.indexOf(currentMode);
|
|
146
|
+
plugin.setMode(cycle[(idx + 1) % cycle.length]);
|
|
147
|
+
},
|
|
148
|
+
getResolvedMode() {
|
|
149
|
+
return resolveMode();
|
|
150
|
+
},
|
|
151
|
+
onModeChange(handler) {
|
|
152
|
+
modeHandlers.add(handler);
|
|
153
|
+
return () => modeHandlers.delete(handler);
|
|
154
|
+
},
|
|
155
|
+
getTheme() {
|
|
156
|
+
return currentTheme;
|
|
157
|
+
},
|
|
158
|
+
setTheme(theme) {
|
|
159
|
+
if (currentTheme === theme) return;
|
|
160
|
+
if (!themes.includes(theme)) return;
|
|
161
|
+
currentTheme = theme;
|
|
162
|
+
applyToDOM();
|
|
163
|
+
persist();
|
|
164
|
+
notifyThemeChange();
|
|
165
|
+
},
|
|
166
|
+
getAvailableThemes() {
|
|
167
|
+
return [...themes];
|
|
168
|
+
},
|
|
169
|
+
onThemeChange(handler) {
|
|
170
|
+
themeHandlers.add(handler);
|
|
171
|
+
return () => themeHandlers.delete(handler);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
return plugin;
|
|
175
|
+
}
|
|
176
|
+
export {
|
|
177
|
+
createCSSTheme
|
|
178
|
+
};
|
|
179
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Context, SettingDefinition, Theme, ThemeMode } from '@dnzn/dxkit';\nimport '@dnzn/dxkit-settings';\n\ndeclare module '@dnzn/dxkit' {\n interface EventMap {\n 'dx:plugin:theme:changed': { theme: string; mode: ThemeMode; resolved: 'light' | 'dark' };\n }\n}\n\nexport interface CSSThemeOptions {\n /** Available theme names. First is the default. */\n themes?: string[];\n /** Initial mode. Default: 'system'. */\n defaultMode?: ThemeMode;\n /** localStorage key prefix. Default: 'dxkit:theme'. */\n storageKey?: string;\n}\n\n/**\n * Creates a CSS theme plugin.\n *\n * Sets `data-theme` and `data-mode` attributes on `<html>`.\n * Persists selection to localStorage. Respects `prefers-color-scheme`\n * when mode is 'system'.\n *\n * Declares settings so the settings plugin can render theme/mode controls.\n */\nexport function createCSSTheme(options: CSSThemeOptions = {}): Theme {\n const { themes = ['default'], defaultMode = 'system', storageKey = 'dxkit:theme' } = options;\n\n let currentTheme = themes[0];\n let currentMode: ThemeMode = defaultMode;\n let dx: Context | null = null;\n let settingsListener: { off(): void } | null = null;\n // Prevents re-entrant loop: theme change → settings write → settings event → theme change\n let syncing = false;\n\n const modeHandlers = new Set<(mode: ThemeMode, resolved: 'light' | 'dark') => void>();\n const themeHandlers = new Set<(theme: string) => void>();\n\n // Media query for system preference\n const mql = typeof window !== 'undefined' ? window.matchMedia('(prefers-color-scheme: dark)') : null;\n\n function resolveMode(): 'light' | 'dark' {\n if (currentMode !== 'system') return currentMode;\n return mql?.matches ? 'dark' : 'light';\n }\n\n function applyToDOM(): void {\n if (typeof document === 'undefined') return;\n const el = document.documentElement;\n el.setAttribute('data-theme', currentTheme);\n el.setAttribute('data-mode', resolveMode());\n }\n\n function canUseStorage(): boolean {\n try {\n return typeof localStorage !== 'undefined' && typeof localStorage.setItem === 'function';\n } catch {\n return false;\n }\n }\n\n function persist(): void {\n if (!canUseStorage()) return;\n try {\n localStorage.setItem(\n storageKey,\n JSON.stringify({\n theme: currentTheme,\n mode: currentMode,\n }),\n );\n } catch {\n /* storage full or blocked */\n }\n }\n\n function restore(): void {\n if (!canUseStorage()) return;\n try {\n const raw = localStorage.getItem(storageKey);\n if (!raw) return;\n const saved = JSON.parse(raw);\n if (saved.theme && themes.includes(saved.theme)) currentTheme = saved.theme;\n if (saved.mode && ['light', 'dark', 'system'].includes(saved.mode)) currentMode = saved.mode;\n } catch {\n /* corrupted — use defaults */\n }\n }\n\n /** Push current theme/mode into the settings store (if available). */\n function syncToSettings(): void {\n if (!dx?.settings || syncing) return;\n syncing = true;\n dx.settings.set('theme', 'theme', currentTheme);\n dx.settings.set('theme', 'mode', currentMode);\n syncing = false;\n }\n\n function notifyModeChange(): void {\n const resolved = resolveMode();\n for (const handler of modeHandlers) handler(currentMode, resolved);\n dx?.events.emit('dx:plugin:theme:changed', { theme: currentTheme, mode: currentMode, resolved });\n syncToSettings();\n }\n\n function notifyThemeChange(): void {\n for (const handler of themeHandlers) handler(currentTheme);\n dx?.events.emit('dx:plugin:theme:changed', { theme: currentTheme, mode: currentMode, resolved: resolveMode() });\n syncToSettings();\n }\n\n // System preference change listener\n function onSystemChange(): void {\n if (currentMode !== 'system') return;\n applyToDOM();\n notifyModeChange();\n }\n\n /** React to settings changes (from settings dapp UI). */\n function onSettingsChanged(event: { dappId: string; key: string; value: unknown }): void {\n if (event.dappId !== 'theme' || syncing) return;\n if (event.key === 'theme' && typeof event.value === 'string') {\n plugin.setTheme(event.value);\n } else if (event.key === 'mode' && typeof event.value === 'string') {\n plugin.setMode(event.value as ThemeMode);\n }\n }\n\n // Build settings definitions from the options\n function buildSettings(): SettingDefinition[] {\n const defs: SettingDefinition[] = [];\n\n if (themes.length > 1) {\n defs.push({\n key: 'theme',\n label: 'Theme',\n type: 'select',\n default: themes[0],\n description: 'Color palette.',\n options: themes.map((t) => ({ label: t.charAt(0).toUpperCase() + t.slice(1), value: t })),\n });\n }\n\n defs.push({\n key: 'mode',\n label: 'Mode',\n type: 'select',\n default: defaultMode,\n description: 'Light, dark, or match your system.',\n options: [\n { label: 'System', value: 'system' },\n { label: 'Light', value: 'light' },\n { label: 'Dark', value: 'dark' },\n ],\n });\n\n return defs;\n }\n\n const plugin: Theme = {\n name: 'theme',\n settings: buildSettings(),\n\n async init(context: Context): Promise<void> {\n dx = context;\n\n context.eventRegistry.registerEvent('theme', [{ name: 'dx:plugin:theme:changed' }]);\n\n restore();\n applyToDOM();\n mql?.addEventListener('change', onSystemChange);\n\n // Seed current values into settings store\n syncToSettings();\n\n // Listen for settings changes (from settings dapp)\n settingsListener = dx.events.on('dx:plugin:settings:changed', onSettingsChanged);\n },\n\n async destroy(): Promise<void> {\n mql?.removeEventListener('change', onSystemChange);\n if (settingsListener) {\n settingsListener.off();\n settingsListener = null;\n }\n modeHandlers.clear();\n themeHandlers.clear();\n dx = null;\n },\n\n getMode(): ThemeMode {\n return currentMode;\n },\n\n setMode(mode: ThemeMode): void {\n if (currentMode === mode) return;\n currentMode = mode;\n applyToDOM();\n persist();\n notifyModeChange();\n },\n\n toggleMode(): void {\n // Cycle order: system → light → dark → system\n const cycle: ThemeMode[] = ['system', 'light', 'dark'];\n const idx = cycle.indexOf(currentMode);\n plugin.setMode(cycle[(idx + 1) % cycle.length]);\n },\n\n getResolvedMode(): 'light' | 'dark' {\n return resolveMode();\n },\n\n onModeChange(handler: (mode: ThemeMode, resolved: 'light' | 'dark') => void): () => void {\n modeHandlers.add(handler);\n return () => modeHandlers.delete(handler);\n },\n\n getTheme(): string {\n return currentTheme;\n },\n\n setTheme(theme: string): void {\n if (currentTheme === theme) return;\n if (!themes.includes(theme)) return;\n currentTheme = theme;\n applyToDOM();\n persist();\n notifyThemeChange();\n },\n\n getAvailableThemes(): string[] {\n return [...themes];\n },\n\n onThemeChange(handler: (theme: string) => void): () => void {\n themeHandlers.add(handler);\n return () => themeHandlers.delete(handler);\n },\n };\n\n return plugin;\n}\n"],"mappings":";AACA,OAAO;AA0BA,SAAS,eAAe,UAA2B,CAAC,GAAU;AACnE,QAAM,EAAE,SAAS,CAAC,SAAS,GAAG,cAAc,UAAU,aAAa,cAAc,IAAI;AAErF,MAAI,eAAe,OAAO,CAAC;AAC3B,MAAI,cAAyB;AAC7B,MAAI,KAAqB;AACzB,MAAI,mBAA2C;AAE/C,MAAI,UAAU;AAEd,QAAM,eAAe,oBAAI,IAA2D;AACpF,QAAM,gBAAgB,oBAAI,IAA6B;AAGvD,QAAM,MAAM,OAAO,WAAW,cAAc,OAAO,WAAW,8BAA8B,IAAI;AAEhG,WAAS,cAAgC;AACvC,QA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,MAAM;AAAA,IACnB;AAAA,IAEA,cAAc,SAA8C;AAC1D,oBAAc,IAAI,OAAO;AACzB,aAAO,MAAM,cAAc,OAAO,OAAO;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dnzn/dxkit-theme",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "DxKit theme plugin — CSS theme + light/dark/system mode",
|
|
5
|
+
"author": "Denizen. <null@dnzn.dev>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/DxNZN/DxKit",
|
|
10
|
+
"directory": "plugins/theme"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://dnzn.dev",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "dist/index.cjs",
|
|
15
|
+
"module": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@dnzn/dxkit-settings": "0.1.0",
|
|
29
|
+
"@dnzn/dxkit": "0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"clean": "rm -rf dist"
|
|
34
|
+
}
|
|
35
|
+
}
|