--terminal-bg/fg/cursor/selection are defined per theme in style.css (dark keeps the deep well, light gets a paper inset) and exposed as bg-terminal. Both xterm surfaces (SetupTerminal, SandboxView) read them via lib/terminalTheme and re-apply on every data-theme flip, so the theme switch is live; the raw #0b0f14/#e6edf3/#264f78 hex is gone.
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
/**
|
|
* The xterm palette, read from the --terminal-* theme tokens so a terminal
|
|
* follows the dashboard theme instead of hard-coded hex. DOM-bound, unlike
|
|
* the pure helpers in format.ts.
|
|
*/
|
|
|
|
/** Structural subset of xterm's ITheme, so this module need not import @xterm/xterm. */
|
|
export interface TerminalTheme {
|
|
background: string;
|
|
foreground: string;
|
|
cursor: string;
|
|
selectionBackground: string;
|
|
}
|
|
|
|
function readToken(name: string): string {
|
|
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
}
|
|
|
|
/** The current terminal palette from the theme tokens in style.css. */
|
|
export function terminalTheme(): TerminalTheme {
|
|
return {
|
|
background: readToken("--terminal-bg"),
|
|
foreground: readToken("--terminal-fg"),
|
|
cursor: readToken("--terminal-cursor"),
|
|
selectionBackground: readToken("--terminal-selection"),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Applies the terminal theme now and again on every theme switch. The
|
|
* appearance store rewrites data-theme on <html> — including OS-level
|
|
* changes while the preference is "system" — so watching that one attribute
|
|
* covers live switching. Returns the stop function.
|
|
*/
|
|
export function followTerminalTheme(apply: (theme: TerminalTheme) => void): () => void {
|
|
apply(terminalTheme());
|
|
const observer = new MutationObserver((mutations) => {
|
|
if (mutations.some((m) => m.attributeName === "data-theme")) apply(terminalTheme());
|
|
});
|
|
observer.observe(document.documentElement, {
|
|
attributes: true,
|
|
attributeFilter: ["data-theme"],
|
|
});
|
|
return () => observer.disconnect();
|
|
}
|