Repository navigation
Expand file tree
/
Copy paththeme.js
More file actions
96 lines (84 loc) · 2.64 KB
/
Copy paththeme.js
File metadata and controls
96 lines (84 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(() => {
const THEME_KEY = "db-wallet:theme";
const SELECTABLE_THEME_NAMES = [
"Lilac Carbon",
"Teal Ember",
"Slate Sunrise",
"Paper Mint",
"Peach Cloud",
];
const LEGACY_THEME_ALIASES = {
"Nord Glow": "Lilac Carbon",
"Cyan Mist": "Teal Ember",
};
function canonicalThemeName(name) {
const raw = String(name || "").trim();
return LEGACY_THEME_ALIASES[raw] || raw;
}
function isSelectableThemeName(name) {
return SELECTABLE_THEME_NAMES.includes(name);
}
function getStoredTheme() {
try {
const stored = localStorage.getItem(THEME_KEY);
const canonical = canonicalThemeName(stored);
return isSelectableThemeName(canonical) ? canonical : null;
} catch (e) {
return null;
}
}
function highlightActiveTheme(name) {
const buttons = document.querySelectorAll(".theme-btn");
buttons.forEach((btn) => {
btn.classList.toggle("active", btn.dataset.theme === name);
});
}
function applyTheme(name) {
const canonical = canonicalThemeName(name);
if (!isSelectableThemeName(canonical)) return false;
document.documentElement.setAttribute("data-theme", canonical);
try {
localStorage.setItem(THEME_KEY, canonical);
} catch (e) {
// ignore storage issues
}
highlightActiveTheme(canonical);
return true;
}
function initThemeSelector(options = {}) {
const container =
options.container ||
document.getElementById(options.containerId || "theme-buttons");
if (!container) return;
container.innerHTML = "";
const names = Array.isArray(options.names)
? options.names
: SELECTABLE_THEME_NAMES;
names.forEach((themeName) => {
const btn = document.createElement("button");
btn.className = "theme-btn";
btn.dataset.theme = themeName;
btn.textContent = themeName;
btn.addEventListener("click", () => applyTheme(themeName));
container.appendChild(btn);
});
const stored = getStoredTheme();
if (stored) {
applyTheme(stored);
}
}
window.dbWalletTheme = {
THEME_KEY,
SELECTABLE_THEME_NAMES,
canonicalThemeName,
isSelectableThemeName,
getStoredTheme,
applyTheme,
highlightActiveTheme,
initThemeSelector,
};
const stored = getStoredTheme();
if (stored) {
applyTheme(stored);
}
})();