Skip to content

Commit 7257b49

Browse files
committed
feat: default theme to system across website, docs, blog, and scaffolder
Theme toggle defaults to 'system' instead of 'light'. Bootstrap scripts no longer force data-theme — they only set it when the user has an explicit localStorage preference. CSS prefers-color-scheme handles the default. Updated all three apps + the webjs create scaffolder template.
1 parent d730b61 commit 7257b49

7 files changed

Lines changed: 32 additions & 27 deletions

File tree

docs/app/layout.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export default function RootLayout({ children }: { children: unknown }) {
77
(function(){
88
try {
99
var t = localStorage.getItem('webjs_theme');
10-
document.documentElement.dataset.theme = (t === 'dark') ? 'dark' : 'light';
11-
} catch (_) {
12-
document.documentElement.dataset.theme = 'light';
13-
}
10+
if (t === 'light' || t === 'dark') {
11+
document.documentElement.dataset.theme = t;
12+
}
13+
} catch (_) {}
1414
})();
1515
</script>
1616
<style>

docs/components/theme-toggle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export class ThemeToggle extends WebComponent {
4848

4949
constructor() {
5050
super();
51-
this.state = { theme: 'light' };
51+
this.state = { theme: 'system' };
5252
}
5353

5454
connectedCallback() {
5555
super.connectedCallback();
5656
let saved: string | null = null;
5757
try { saved = localStorage.getItem('webjs_theme'); } catch {}
58-
const theme: Theme = saved === 'light' || saved === 'dark' ? saved : 'light';
58+
const theme: Theme = saved === 'light' || saved === 'dark' ? saved : 'system';
5959
this.setState({ theme });
6060
}
6161

examples/blog/app/layout.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export default function RootLayout({ children }: { children: unknown }) {
1919
(function(){
2020
try {
2121
var t = localStorage.getItem('webjs_theme');
22-
document.documentElement.dataset.theme = (t === 'dark') ? 'dark' : 'light';
23-
} catch (_) {
24-
document.documentElement.dataset.theme = 'light';
25-
}
22+
if (t === 'light' || t === 'dark') {
23+
document.documentElement.dataset.theme = t;
24+
}
25+
} catch (_) {}
2626
})();
2727
</script>
2828
<style>

examples/blog/components/theme-toggle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export class ThemeToggle extends WebComponent {
4848

4949
constructor() {
5050
super();
51-
this.state = { theme: 'light' };
51+
this.state = { theme: 'system' };
5252
}
5353

5454
connectedCallback() {
5555
super.connectedCallback();
5656
let saved: string | null = null;
5757
try { saved = localStorage.getItem('webjs_theme'); } catch {}
58-
const theme: Theme = saved === 'light' || saved === 'dark' ? saved : 'light';
58+
const theme: Theme = saved === 'light' || saved === 'dark' ? saved : 'system';
5959
this.setState({ theme });
6060
}
6161

packages/cli/lib/create.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ export default function RootLayout({ children }: { children: unknown }) {
130130
(function(){
131131
try {
132132
var t = localStorage.getItem('webjs_theme');
133-
document.documentElement.dataset.theme = (t === 'dark') ? 'dark' : 'light';
134-
} catch (_) {
135-
document.documentElement.dataset.theme = 'light';
136-
}
133+
if (t === 'light' || t === 'dark') {
134+
document.documentElement.dataset.theme = t;
135+
}
136+
} catch (_) {}
137137
})();
138138
</script>
139139
<style>
@@ -224,21 +224,26 @@ export class ThemeToggle extends WebComponent {
224224
225225
constructor() {
226226
super();
227-
this.state = { theme: 'light' };
227+
this.state = { theme: 'system' };
228228
}
229229
230230
connectedCallback() {
231231
super.connectedCallback();
232232
let saved: string | null = null;
233233
try { saved = localStorage.getItem('webjs_theme'); } catch {}
234-
this.setState({ theme: saved === 'dark' ? 'dark' : 'light' });
234+
this.setState({ theme: saved === 'light' || saved === 'dark' ? saved : 'system' });
235235
}
236236
237237
cycle() {
238-
const next: Theme = this.state.theme === 'light' ? 'dark' : 'light';
238+
const next: Theme = this.state.theme === 'system' ? 'light'
239+
: this.state.theme === 'light' ? 'dark' : 'system';
239240
this.setState({ theme: next });
240-
try { localStorage.setItem('webjs_theme', next); } catch {}
241-
document.documentElement.dataset.theme = next;
241+
try {
242+
if (next === 'system') localStorage.removeItem('webjs_theme');
243+
else localStorage.setItem('webjs_theme', next);
244+
} catch {}
245+
if (next === 'system') delete document.documentElement.dataset.theme;
246+
else document.documentElement.dataset.theme = next;
242247
}
243248
244249
render() {

website/app/layout.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export default function RootLayout({ children }: { children: unknown }) {
88
(function(){
99
try {
1010
var t = localStorage.getItem('webjs_theme');
11-
document.documentElement.dataset.theme = (t === 'dark') ? 'dark' : 'light';
12-
} catch (_) {
13-
document.documentElement.dataset.theme = 'light';
14-
}
11+
if (t === 'light' || t === 'dark') {
12+
document.documentElement.dataset.theme = t;
13+
}
14+
} catch (_) {}
1515
})();
1616
</script>
1717
<style>

website/components/theme-toggle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export class ThemeToggle extends WebComponent {
4848

4949
constructor() {
5050
super();
51-
this.state = { theme: 'light' };
51+
this.state = { theme: 'system' };
5252
}
5353

5454
connectedCallback() {
5555
super.connectedCallback();
5656
let saved: string | null = null;
5757
try { saved = localStorage.getItem('webjs_theme'); } catch {}
58-
const theme: Theme = saved === 'light' || saved === 'dark' ? saved : 'light';
58+
const theme: Theme = saved === 'light' || saved === 'dark' ? saved : 'system';
5959
this.setState({ theme });
6060
}
6161

0 commit comments

Comments
 (0)