Skip to content

Commit 454b56a

Browse files
committed
support ?mode= and remove tw:theme key
1 parent 5b48959 commit 454b56a

File tree

4 files changed

+58
-46
lines changed

4 files changed

+58
-46
lines changed

src/lib/themes/gui/dark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const guiColors = {
3131
'fullscreen-background': '#111111',
3232
'fullscreen-accent': '#111111',
3333

34-
'page-background': '#111111',
34+
'page-background': '#121212',
3535
'page-foreground': '#eeeeee',
3636

3737
'project-title-inactive': 'var(--ui-secondary)',

src/lib/themes/themePersistance.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ const matchMedia = query => (window.matchMedia ? window.matchMedia(query) : null
44
const PREFERS_HIGH_CONTRAST_QUERY = matchMedia('(prefers-contrast: more)');
55
const PREFERS_DARK_QUERY = matchMedia('(prefers-color-scheme: dark)');
66

7-
const STORAGE_KEY = 'tw:theme';
8-
97
/**
108
* @returns {Theme} detected theme
119
*/
1210
const systemPreferencesTheme = () => {
13-
// @OVERRIDE if is on home page, return light theme
11+
// Check for URL search parameters first (highest priority)
12+
const urlParams = new URLSearchParams(window.location.search);
13+
const modeParam = urlParams.get('mode');
14+
15+
// Check for mode parameter on any page
16+
if (modeParam === 'dark') {
17+
return Theme.dark;
18+
} else if (modeParam === 'light') {
19+
return Theme.light;
20+
}
21+
22+
// If on home page, default to light theme
1423
if (window.location.pathname === "/" || window.location.pathname.includes("/index.html") || window.location.pathname === "/build/") {
1524
return Theme.light;
1625
}
26+
1727
if (PREFERS_HIGH_CONTRAST_QUERY && PREFERS_HIGH_CONTRAST_QUERY.matches) {
1828
return Theme.highContrast;
1929
}
@@ -52,29 +62,6 @@ const onSystemPreferenceChange = onChange => {
5262
*/
5363
const detectTheme = () => {
5464
const systemPreferences = systemPreferencesTheme();
55-
56-
try {
57-
const local = localStorage.getItem(STORAGE_KEY);
58-
59-
// Migrate legacy preferences
60-
if (local === 'dark') {
61-
return Theme.dark;
62-
}
63-
if (local === 'light') {
64-
return Theme.light;
65-
}
66-
67-
const parsed = JSON.parse(local);
68-
// Any invalid values in storage will be handled by Theme itself
69-
return new Theme(
70-
parsed.accent || systemPreferences.accent,
71-
parsed.gui || systemPreferences.gui,
72-
parsed.blocks || systemPreferences.blocks
73-
);
74-
} catch (e) {
75-
// ignore
76-
}
77-
7865
return systemPreferences;
7966
};
8067

src/playground/embed.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ const getProjectId = () => {
3030
const projectId = getProjectId();
3131
const urlParams = new URLSearchParams(location.search);
3232

33+
// Determine theme based on URL parameters
34+
const getThemeFromUrl = () => {
35+
const modeParam = urlParams.get('mode');
36+
if (modeParam === 'dark') {
37+
return Theme.dark;
38+
} else if (modeParam === 'light') {
39+
return Theme.light;
40+
}
41+
return Theme.light; // Default to light theme
42+
};
43+
3344
let vm;
3445

3546
const onVmInit = _vm => {
@@ -55,7 +66,7 @@ render(<WrappedGUI
5566
onVmInit={onVmInit}
5667
onProjectLoaded={onProjectLoaded}
5768
routingStyle="none"
58-
theme={Theme.light}
69+
theme={getThemeFromUrl()}
5970
/>);
6071

6172
if (urlParams.has('addons')) {

src/playground/index.ejs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,45 @@
100100
var theme = '';
101101
var accent = '#d0402e';
102102
103-
try {
104-
var themeSetting = localStorage.getItem('tw:theme');
105-
} catch (e) {
106-
// ignore
107-
}
108-
if (themeSetting === 'light') {
109-
theme = 'light';
110-
} else if (themeSetting === 'dark') {
103+
// Check URL parameters first (highest priority)
104+
var urlParams = new URLSearchParams(window.location.search);
105+
var modeParam = urlParams.get('mode');
106+
107+
if (modeParam === 'dark') {
111108
theme = 'dark';
112-
} else if (themeSetting) {
109+
} else if (modeParam === 'light') {
110+
theme = 'light';
111+
}
112+
113+
// If no URL parameter, check localStorage
114+
if (!theme) {
113115
try {
114-
var parsed = JSON.parse(themeSetting);
115-
if (parsed.accent === 'purple') {
116-
accent = '#855cd6';
117-
} else if (parsed.accent === 'blue') {
118-
accent = '#4c97ff';
119-
}
120-
if (parsed.gui === 'dark' || parsed.gui === 'light') {
121-
theme = parsed.gui;
122-
}
116+
var themeSetting = localStorage.getItem('tw:theme');
123117
} catch (e) {
124118
// ignore
125119
}
120+
if (themeSetting === 'light') {
121+
theme = 'light';
122+
} else if (themeSetting === 'dark') {
123+
theme = 'dark';
124+
} else if (themeSetting) {
125+
try {
126+
var parsed = JSON.parse(themeSetting);
127+
if (parsed.accent === 'purple') {
128+
accent = '#855cd6';
129+
} else if (parsed.accent === 'blue') {
130+
accent = '#4c97ff';
131+
}
132+
if (parsed.gui === 'dark' || parsed.gui === 'light') {
133+
theme = parsed.gui;
134+
}
135+
} catch (e) {
136+
// ignore
137+
}
138+
}
126139
}
127140
141+
// If still no theme, check system preferences
128142
if (!theme) {
129143
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
130144
}

0 commit comments

Comments
 (0)