Skip to content

Commit 937a044

Browse files
committed
Refactor theme change handling to include source parameter and improve initial theme setting
1 parent f872355 commit 937a044

3 files changed

Lines changed: 44 additions & 27 deletions

File tree

astro.config.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ export default defineConfig({
9393
content: `
9494
document.addEventListener('starlight:theme-change', (e) => {
9595
e.preventDefault();
96-
const { theme, resolvedTheme } = e.detail;
97-
document.startViewTransition({types: ['light-dark'], update: () => (document.documentElement.dataset.theme = resolvedTheme)});
96+
const { theme, resolvedTheme, source } = e.detail;
97+
if (source !== 'initial') {
98+
document.startViewTransition({types: ['light-dark'], update: () => (document.documentElement.dataset.theme = resolvedTheme)});
99+
} else {
100+
document.documentElement.dataset.theme = resolvedTheme;
101+
}
98102
});`,
99103
},
100104
{
@@ -201,7 +205,7 @@ export default defineConfig({
201205
allowedHosts: [".trycloudflare.com"],
202206
},
203207
optimizeDeps: {
204-
208+
205209
include: [
206210
'unified',
207211
'remark-parse',
@@ -241,8 +245,10 @@ function sidebar() {
241245
label: "Mechanics of Default Animations",
242246
link: "/basics/default-animations/",
243247
},
244-
{ label: "Styling View Transitions", link: "/basics/styling/",
245-
badge: { text: "Updated!", variant: "success" } as Badge },
248+
{
249+
label: "Styling View Transitions", link: "/basics/styling/",
250+
badge: { text: "Updated!", variant: "success" } as Badge
251+
},
246252
{ label: "JavaScript API", link: "/basics/javascript/" },
247253
{ label: "Playing Hide & Seek", link: "/basics/hide-and-seek/", },
248254
{
Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
diff --git a/node_modules/@astrojs/starlight/components/ThemeSelect.astro b/node_modules/@astrojs/starlight/components/ThemeSelect.astro
2-
index a7cd175..d3e9670 100644
2+
index d3e9670..ed6c93c 100644
33
--- a/node_modules/@astrojs/starlight/components/ThemeSelect.astro
44
+++ b/node_modules/@astrojs/starlight/components/ThemeSelect.astro
5-
@@ -48,9 +48,20 @@ import Select from './Select.astro';
5+
@@ -47,13 +47,13 @@ import Select from './Select.astro';
6+
matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
67

78
/** Update select menu UI, document theme, and local storage state. */
8-
function onThemeChange(theme: Theme): void {
9-
- StarlightThemeProvider.updatePickers(theme);
10-
- document.documentElement.dataset.theme = theme === 'auto' ? getPreferredColorScheme() : theme;
11-
- storeTheme(theme);
12-
+ StarlightThemeProvider.updatePickers(theme);
13-
+
14-
+ const resolvedTheme = theme === 'auto' ? getPreferredColorScheme() : theme;
15-
+ const event = new CustomEvent('starlight:theme-change', {
16-
+ cancelable: true,
17-
+ detail: { theme, resolvedTheme },
18-
+ });
19-
+
20-
+ const handled = !document.dispatchEvent(event);
21-
+ if (!handled) {
22-
+ document.documentElement.dataset.theme = resolvedTheme;
23-
+ }
24-
+
25-
+ storeTheme(theme);
26-
}
9+
- function onThemeChange(theme: Theme): void {
10+
+ function onThemeChange(theme: Theme, source: 'initial' | 'media' | 'select'): void {
11+
StarlightThemeProvider.updatePickers(theme);
12+
13+
const resolvedTheme = theme === 'auto' ? getPreferredColorScheme() : theme;
14+
const event = new CustomEvent('starlight:theme-change', {
15+
cancelable: true,
16+
- detail: { theme, resolvedTheme },
17+
+ detail: { theme, resolvedTheme, source },
18+
});
19+
20+
const handled = !document.dispatchEvent(event);
21+
@@ -66,16 +66,16 @@ import Select from './Select.astro';
2722

2823
// React to changes in system color scheme.
24+
matchMedia(`(prefers-color-scheme: light)`).addEventListener('change', () => {
25+
- if (loadTheme() === 'auto') onThemeChange('auto');
26+
+ if (loadTheme() === 'auto') onThemeChange('auto', 'media');
27+
});
28+
29+
class StarlightThemeSelect extends HTMLElement {
30+
constructor() {
31+
super();
32+
- onThemeChange(loadTheme());
33+
+ onThemeChange(loadTheme(), "initial");
34+
this.querySelector('select')?.addEventListener('change', (e) => {
35+
if (e.currentTarget instanceof HTMLSelectElement) {
36+
- onThemeChange(parseTheme(e.currentTarget.value));
37+
+ onThemeChange(parseTheme(e.currentTarget.value), 'select');
38+
}
39+
});
40+
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"extends": "astro/tsconfigs/strictest",
33
"exclude": ["node_modules", "dist"],
44
"compilerOptions": {
5-
"types": ["@cloudflare/workers-types"],
65
"paths": { "@/*": ["./src/*"] }
76
}
87
}

0 commit comments

Comments
 (0)