Skip to content

Commit 9da5cf5

Browse files
committed
fixed theme and accent colors not saving
1 parent 26b5c94 commit 9da5cf5

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/lib/themes/themePersistance.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {BLOCKS_CUSTOM, Theme} from '.';
22

3+
const STORAGE_KEY = 'ct:theme';
4+
35
const matchMedia = query => (window.matchMedia ? window.matchMedia(query) : null);
46
const PREFERS_HIGH_CONTRAST_QUERY = matchMedia('(prefers-contrast: more)');
57
const PREFERS_DARK_QUERY = matchMedia('(prefers-color-scheme: dark)');
@@ -11,19 +13,20 @@ const systemPreferencesTheme = () => {
1113
// Check for URL search parameters first (highest priority)
1214
const urlParams = new URLSearchParams(window.location.search);
1315
const modeParam = urlParams.get('mode');
14-
16+
1517
// Check for mode parameter on any page
1618
if (modeParam === 'dark') {
1719
return Theme.dark;
1820
} else if (modeParam === 'light') {
1921
return Theme.light;
2022
}
21-
23+
2224
// If on home page, default to light theme
23-
if (window.location.pathname === "/" || window.location.pathname.includes("/index.html") || window.location.pathname === "/build/") {
25+
// eslint-disable-next-line max-len
26+
if (window.location.pathname === '/' || window.location.pathname.includes('/index.html') || window.location.pathname === '/build/') {
2427
return Theme.light;
2528
}
26-
29+
2730
if (PREFERS_HIGH_CONTRAST_QUERY && PREFERS_HIGH_CONTRAST_QUERY.matches) {
2831
return Theme.highContrast;
2932
}
@@ -62,7 +65,29 @@ const onSystemPreferenceChange = onChange => {
6265
*/
6366
const detectTheme = () => {
6467
const systemPreferences = systemPreferencesTheme();
65-
return systemPreferences;
68+
let theme = systemPreferences;
69+
70+
try {
71+
const stored = localStorage.getItem(STORAGE_KEY);
72+
if (stored) {
73+
const parsed = JSON.parse(stored);
74+
if (parsed) {
75+
if (parsed.accent) {
76+
theme = theme.set('accent', parsed.accent);
77+
}
78+
if (parsed.gui) {
79+
theme = theme.set('gui', parsed.gui);
80+
}
81+
if (parsed.blocks && parsed.blocks !== BLOCKS_CUSTOM) {
82+
theme = theme.set('blocks', parsed.blocks);
83+
}
84+
}
85+
}
86+
} catch (e) {
87+
// ignore
88+
}
89+
90+
return theme;
6691
};
6792

6893
/**

0 commit comments

Comments
 (0)