fix(ui): let an embedding app start the dashboard with both theme axes (#910)

The desktop app shows the dashboard in an iframe and passes its theme as
?theme=, which holds one value: it cannot ask for Dark + Clean or Playful
dark. Where the iframe's localStorage is not kept (WebKitGTK on Linux), the
dashboard then started with another theme, flashed it, and reported it to
the app as if the user had picked it (runkids/skillshare-app#128).

?mode= and ?style= now name one axis each and win over ?theme=, in both the
pre-paint script and ThemeContext, through one parser. The dashboard also
posts theme-init to its parent when a page starts, before the first
theme-change reports, so the app can tell a starting theme from a user's
choice even when the dashboard reloads itself.
This commit is contained in:
Willie
2026-11-14 23:54:42 +08:00
committed by GitHub
parent be8f8b2874
commit f989f13855
5 changed files with 79 additions and 21 deletions
+8 -5
View File
@@ -18,14 +18,17 @@
<script>
// FOUC prevention: handle Clean theme + dark mode before first paint
// Default is Playful (already set on <html>); only remove for explicit Clean
// URL ?theme= param overrides localStorage (values: dark, light, playful, clean)
// URL ?theme= param overrides localStorage (values: dark, light, playful, clean).
// ?mode= (dark, light) and ?style= (playful, clean) name one axis each and win over it.
(function() {
var p = new URLSearchParams(location.search).get('theme');
if (p) {
p = p.toLowerCase();
var q = new URLSearchParams(location.search);
var get = function(key) { return (q.get(key) || '').toLowerCase(); };
[get('theme'), get('mode')].forEach(function(p) {
if (p === 'dark' || p === 'light') localStorage.setItem('skillshare-theme-preference', p);
});
[get('theme'), get('style')].forEach(function(p) {
if (p === 'playful' || p === 'clean') localStorage.setItem('skillshare-style', p);
}
});
var style = localStorage.getItem('skillshare-style');
var pref = localStorage.getItem('skillshare-theme-preference');
if (style === 'clean') {
+11
View File
@@ -0,0 +1,11 @@
import { afterEach, expect, it, vi } from 'vitest';
afterEach(() => vi.restoreAllMocks());
it('tells an embedding app that a new page is starting', async () => {
const post = vi.fn();
vi.spyOn(window, 'parent', 'get').mockReturnValue({ postMessage: post } as unknown as Window);
vi.resetModules();
await import('./ThemeContext');
expect(post).toHaveBeenCalledWith({ type: 'theme-init' }, '*');
});
+11 -16
View File
@@ -1,5 +1,6 @@
import { createContext, useContext, useState, useEffect, useCallback, useMemo } from 'react';
import type { ReactNode } from 'react';
import { themeFromSearch } from '../lib/themeParams';
export type Style = 'clean' | 'playful';
export type ModePreference = 'light' | 'dark' | 'system';
@@ -30,26 +31,20 @@ export function useTheme() {
return useContext(ThemeContext);
}
/** Read ?theme= URL param and persist to localStorage (runs once on load).
* Values: 'dark' | 'clean' | 'playful' | 'light'
* 'clean' implies light mode + clean style; 'playful' implies light mode + playful style. */
/** Persist the theme the URL asks for to localStorage (runs once on load). */
function applyUrlThemeParam(): void {
const p = new URLSearchParams(window.location.search).get('theme')?.toLowerCase();
if (!p) return;
if (p === 'dark') {
localStorage.setItem('skillshare-theme-preference', 'dark');
} else if (p === 'clean') {
localStorage.setItem('skillshare-theme-preference', 'light');
localStorage.setItem('skillshare-style', 'clean');
} else if (p === 'playful') {
localStorage.setItem('skillshare-theme-preference', 'light');
localStorage.setItem('skillshare-style', 'playful');
} else if (p === 'light') {
localStorage.setItem('skillshare-theme-preference', 'light');
}
const { style, mode } = themeFromSearch(window.location.search);
if (mode) localStorage.setItem('skillshare-theme-preference', mode);
if (style) localStorage.setItem('skillshare-style', style);
}
applyUrlThemeParam();
// Tell an embedding app (the desktop shell) that a new page is starting. It arrives before the
// first theme-change reports, so the app can tell this page's starting theme from a user's choice.
if (window.parent !== window) {
window.parent.postMessage({ type: 'theme-init' }, '*');
}
function getInitialStyle(): Style {
const stored = localStorage.getItem('skillshare-style');
if (stored === 'clean') return 'clean';
+25
View File
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest';
import { themeFromSearch } from './themeParams';
describe('themeFromSearch', () => {
it('reads a mode from ?theme= and leaves the style alone', () => {
expect(themeFromSearch('?theme=dark')).toEqual({ mode: 'dark' });
});
it('reads a style from ?theme= and implies light mode', () => {
expect(themeFromSearch('?theme=Playful')).toEqual({ style: 'playful', mode: 'light' });
});
it('reads Dark + Clean from ?mode= and ?style=', () => {
expect(themeFromSearch('?mode=dark&style=clean')).toEqual({ mode: 'dark', style: 'clean' });
});
it('lets ?mode= and ?style= win over ?theme=', () => {
expect(themeFromSearch('?theme=playful&mode=dark')).toEqual({ style: 'playful', mode: 'dark' });
expect(themeFromSearch('?theme=dark&style=clean')).toEqual({ mode: 'dark', style: 'clean' });
});
it('ignores unknown values and other params', () => {
expect(themeFromSearch('?tab=updates&mode=sepia&style=neon&theme=x')).toEqual({});
});
});
+24
View File
@@ -0,0 +1,24 @@
import type { ResolvedMode, Style } from '../context/ThemeContext';
/** The theme a URL asks for.
* `?theme=` is the single-value form: `dark`/`light` set the mode; `clean`/`playful` set the
* style and imply light mode. `?mode=` and `?style=` name one axis each and win over `?theme=`,
* so an embedding app can ask for Dark + Clean or Playful dark. */
export function themeFromSearch(search: string): { style?: Style; mode?: ResolvedMode } {
const params = new URLSearchParams(search);
const get = (key: string) => params.get(key)?.toLowerCase();
const asked: { style?: Style; mode?: ResolvedMode } = {};
const theme = get('theme');
if (theme === 'dark' || theme === 'light') asked.mode = theme;
if (theme === 'clean' || theme === 'playful') {
asked.style = theme;
asked.mode = 'light';
}
const mode = get('mode');
if (mode === 'dark' || mode === 'light') asked.mode = mode;
const style = get('style');
if (style === 'clean' || style === 'playful') asked.style = style;
return asked;
}