Skip to content

Commit e666a8a

Browse files
feat(studio): the code editor follows studio's light and dark theme (#4862)
* feat(studio): the code editor follows studio's light and dark theme Replaces One Dark with a Studio editor theme whose colours are theme.css variables: eight syntax role tokens (One Light and One Dark hues held at 4.5:1), editor surfaces from Studio's roles, all measured by the contrast ratchet. * test(studio): the code editor theme takes every colour from a theme.css token * fix(studio): code stays at 4.5:1 when a selection or search match sits on the active line Syntax tokens are tuned for the selection over the active line, which the ratchet now measures; matches are outlined rather than stacking another wash; the search panel's pressed button and special characters follow the theme; unused tooltip rules go. * fix(studio): code matches carry no background, and types read apart from constants Search and selection matches are outlined only, so CodeMirror's lime match wash and stacked washes are gone; type and constant tokens split by hue in both themes. * fix(studio): the current search match reads apart by width, not colour alone A 2px accent outline on the current match and a muted 1px outline on other copies of the selected word.
1 parent 9c19327 commit e666a8a

8 files changed

Lines changed: 200 additions & 12 deletions

File tree

‎bun.lock‎

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/studio/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@
9393
"@codemirror/language": "^6.12.2",
9494
"@codemirror/search": "^6.6.0",
9595
"@codemirror/state": "^6.6.0",
96-
"@codemirror/theme-one-dark": "^6.1.2",
9796
"@codemirror/view": "6.40.0",
9897
"@hyperframes/core": "workspace:*",
9998
"@hyperframes/parsers": "workspace:*",
10099
"@hyperframes/player": "workspace:*",
101100
"@hyperframes/sdk": "workspace:*",
102101
"@hyperframes/shader-transitions": "workspace:*",
103102
"@hyperframes/studio-server": "workspace:*",
103+
"@lezer/highlight": "^1.2.3",
104104
"@mcp-b/global": "^5.0.1",
105105
"@phosphor-icons/react": "^2.1.10",
106106
"@tanstack/react-virtual": "^3.14.13",

‎packages/studio/src/components/editor/SourceEditor.tsx‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
1111
import { bracketMatching, foldGutter, indentOnInput } from "@codemirror/language";
1212
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
1313
import { highlightSelectionMatches, searchKeymap } from "@codemirror/search";
14-
import { oneDark } from "@codemirror/theme-one-dark";
1514
import type { Extension } from "@codemirror/state";
1615
import { html } from "@codemirror/lang-html";
1716
import { css } from "@codemirror/lang-css";
1817
import { javascript } from "@codemirror/lang-javascript";
1918
import { markdown } from "@codemirror/lang-markdown";
19+
import { studioCodeTheme } from "./studioCodeTheme";
2020

2121
// Marks a programmatic doc sync (external content push — e.g. a manual-edit
2222
// commit writing the source) so the update listener doesn't mistake it for a
@@ -114,13 +114,9 @@ export const SourceEditor = memo(function SourceEditor({
114114
highlightSelectionMatches(),
115115
keymap.of([...closeBracketsKeymap, ...defaultKeymap, ...searchKeymap, ...historyKeymap]),
116116
getLanguageExtension(lang),
117-
oneDark,
117+
studioCodeTheme,
118118
updateListener,
119119
EditorState.readOnly.of(readOnly),
120-
EditorView.theme({
121-
"&": { height: "100%" },
122-
".cm-scroller": { overflow: "auto" },
123-
}),
124120
],
125121
});
126122

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { readFileSync } from "node:fs";
2+
import { describe, expect, it } from "vitest";
3+
4+
const read = (file: string) => readFileSync(new URL(file, import.meta.url), "utf8");
5+
const source = read("./studioCodeTheme.ts");
6+
const themeCss = read("../../styles/theme.css");
7+
8+
describe("the code editor theme", () => {
9+
it("takes every colour from a token theme.css declares", () => {
10+
const used = [...source.matchAll(/var\((--color-[\w-]+)\)/g)].map((match) => match[1]!);
11+
expect(used.length).toBeGreaterThan(20);
12+
for (const token of new Set(used)) expect(themeCss, token).toContain(`${token}:`);
13+
});
14+
15+
it("paints no literal colour of its own", () => {
16+
expect(source).not.toMatch(
17+
/#[\da-f]{3,8}\b|\b(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix)\(|"(white|black|red|orange|yellow|green|blue|purple|pink|gray|grey|silver|lime|cyan|magenta)"/i,
18+
);
19+
});
20+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
2+
import { EditorView } from "@codemirror/view";
3+
import { tags as t } from "@lezer/highlight";
4+
5+
// One Dark's layout on Studio's tokens: every colour is a theme.css variable, so the editor
6+
// follows the light or dark theme the page paints with no editor reconfiguration.
7+
const editorTheme = EditorView.theme({
8+
"&": { height: "100%", color: "var(--color-text-0)", backgroundColor: "var(--color-surface)" },
9+
".cm-scroller": { overflow: "auto" },
10+
".cm-content": { caretColor: "var(--color-text-0)" },
11+
".cm-cursor, .cm-dropCursor": { borderLeftColor: "var(--color-text-0)" },
12+
"&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection":
13+
{ backgroundColor: "var(--color-on)" },
14+
".cm-activeLine": { backgroundColor: "var(--color-hover)" },
15+
".cm-selectionMatch": {
16+
backgroundColor: "transparent",
17+
outline: "1px solid var(--color-text-muted)",
18+
},
19+
".cm-searchMatch": {
20+
backgroundColor: "transparent",
21+
outline: "1px solid var(--color-text-muted)",
22+
},
23+
".cm-searchMatch.cm-searchMatch-selected": {
24+
backgroundColor: "transparent",
25+
outline: "2px solid var(--color-accent-ink)",
26+
},
27+
"&.cm-focused .cm-matchingBracket, &.cm-focused .cm-nonmatchingBracket": {
28+
backgroundColor: "var(--color-on)",
29+
},
30+
".cm-gutters": {
31+
backgroundColor: "var(--color-surface)",
32+
color: "var(--color-text-muted)",
33+
border: "none",
34+
},
35+
".cm-activeLineGutter": { backgroundColor: "var(--color-hover)", color: "var(--color-text-2)" },
36+
".cm-foldPlaceholder": {
37+
backgroundColor: "transparent",
38+
border: "none",
39+
color: "var(--color-text-2)",
40+
},
41+
".cm-panels": { backgroundColor: "var(--color-raised)", color: "var(--color-text-0)" },
42+
".cm-panels.cm-panels-top": { borderBottom: "1px solid var(--color-border)" },
43+
".cm-panels.cm-panels-bottom": { borderTop: "1px solid var(--color-border)" },
44+
".cm-textfield": {
45+
backgroundColor: "var(--color-input)",
46+
color: "var(--color-text-0)",
47+
border: "1px solid var(--color-border-input)",
48+
},
49+
".cm-button": {
50+
backgroundImage: "none",
51+
backgroundColor: "var(--color-hover)",
52+
color: "var(--color-text-0)",
53+
border: "1px solid var(--color-border)",
54+
},
55+
"&.cm-editor .cm-button:active": {
56+
backgroundImage: "none",
57+
backgroundColor: "var(--color-press)",
58+
},
59+
".cm-specialChar": { color: "var(--color-danger-ink)" },
60+
});
61+
62+
const highlightStyle = HighlightStyle.define([
63+
{ tag: t.keyword, color: "var(--color-code-keyword)" },
64+
{
65+
tag: [t.name, t.deleted, t.character, t.propertyName, t.macroName],
66+
color: "var(--color-code-name)",
67+
},
68+
{ tag: [t.function(t.variableName), t.labelName], color: "var(--color-code-function)" },
69+
{ tag: [t.color, t.constant(t.name), t.standard(t.name)], color: "var(--color-code-constant)" },
70+
{ tag: [t.definition(t.name), t.separator], color: "var(--color-text-0)" },
71+
{
72+
tag: [
73+
t.typeName,
74+
t.className,
75+
t.number,
76+
t.changed,
77+
t.annotation,
78+
t.modifier,
79+
t.self,
80+
t.namespace,
81+
],
82+
color: "var(--color-code-type)",
83+
},
84+
{
85+
tag: [t.operator, t.operatorKeyword, t.url, t.escape, t.regexp, t.special(t.string)],
86+
color: "var(--color-code-operator)",
87+
},
88+
{ tag: [t.meta, t.comment], color: "var(--color-code-comment)" },
89+
{ tag: t.strong, fontWeight: "bold" },
90+
{ tag: t.emphasis, fontStyle: "italic" },
91+
{ tag: t.strikethrough, textDecoration: "line-through" },
92+
{ tag: t.link, color: "var(--color-code-comment)", textDecoration: "underline" },
93+
{ tag: t.heading, fontWeight: "bold", color: "var(--color-code-name)" },
94+
{ tag: [t.atom, t.bool, t.special(t.variableName)], color: "var(--color-code-constant)" },
95+
{ tag: [t.processingInstruction, t.string, t.inserted], color: "var(--color-code-string)" },
96+
{ tag: t.invalid, color: "var(--color-danger-ink)" },
97+
]);
98+
99+
export const studioCodeTheme = [editorTheme, syntaxHighlighting(highlightStyle)];

‎packages/studio/src/styles/theme.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@
7777
--color-media: light-dark(oklch(0.5 0.1 215), #00e3ff);
7878
--color-container: light-dark(oklch(0.55 0.13 70), #f5a623);
7979

80+
/* Code editor syntax: One Light and One Dark hues, held at 4.5:1 on a line, the active line and a selection on it. */
81+
--color-code-keyword: light-dark(oklch(0.509 0.211 329), oklch(0.829 0.164 318));
82+
--color-code-name: light-dark(oklch(0.504 0.179 28), oklch(0.856 0.145 17));
83+
--color-code-function: light-dark(oklch(0.492 0.193 263), oklch(0.805 0.121 245));
84+
--color-code-constant: light-dark(oklch(0.495 0.13 50), oklch(0.815 0.11 50));
85+
--color-code-type: light-dark(oklch(0.48 0.12 92), oklch(0.805 0.1 92));
86+
--color-code-operator: light-dark(oklch(0.471 0.127 237), oklch(0.793 0.092 206));
87+
--color-code-comment: light-dark(oklch(0.48 0.009 279), oklch(0.806 0.03 262));
88+
--color-code-string: light-dark(oklch(0.468 0.142 143), oklch(0.793 0.11 133));
89+
8090
/* The two stops of the HeyGen brand gradient, worn by the header logo. Fixed brand values
8191
* rather than roles, so they are not reachable as a background or text colour by mistake. */
8292
--color-brand-gradient-from: #06e3fa;

‎packages/studio/tsup.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export default defineConfig({
2323
"@codemirror/language",
2424
"@codemirror/search",
2525
"@codemirror/state",
26-
"@codemirror/theme-one-dark",
2726
"@codemirror/view",
2827
"@hyperframes/core",
2928
"@hyperframes/player",
3029
"@hyperframes/sdk",
30+
"@lezer/highlight",
3131
"@phosphor-icons/react",
3232
"bpm-detective",
3333
"mediabunny",

‎scripts/contrast-pairs.json‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,70 @@
435435
"role": "text",
436436
"minimum": 4.5,
437437
"sources": ["packages/studio/src/styles/theme.css"]
438+
},
439+
{
440+
"id": "code-keyword",
441+
"foreground": "--color-code-keyword",
442+
"role": "text",
443+
"minimum": 4.5,
444+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
445+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
446+
},
447+
{
448+
"id": "code-name",
449+
"foreground": "--color-code-name",
450+
"role": "text",
451+
"minimum": 4.5,
452+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
453+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
454+
},
455+
{
456+
"id": "code-function",
457+
"foreground": "--color-code-function",
458+
"role": "text",
459+
"minimum": 4.5,
460+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
461+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
462+
},
463+
{
464+
"id": "code-constant",
465+
"foreground": "--color-code-constant",
466+
"role": "text",
467+
"minimum": 4.5,
468+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
469+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
470+
},
471+
{
472+
"id": "code-type",
473+
"foreground": "--color-code-type",
474+
"role": "text",
475+
"minimum": 4.5,
476+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
477+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
478+
},
479+
{
480+
"id": "code-operator",
481+
"foreground": "--color-code-operator",
482+
"role": "text",
483+
"minimum": 4.5,
484+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
485+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
486+
},
487+
{
488+
"id": "code-comment",
489+
"foreground": "--color-code-comment",
490+
"role": "text",
491+
"minimum": 4.5,
492+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
493+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
494+
},
495+
{
496+
"id": "code-string",
497+
"foreground": "--color-code-string",
498+
"role": "text",
499+
"minimum": 4.5,
500+
"sources": ["packages/studio/src/components/editor/studioCodeTheme.ts"],
501+
"on": ["panel", "panel-hover", "panel-on", "panel-hover-on"]
438502
}
439503
],
440504
"surfaces": {
@@ -447,7 +511,8 @@
447511
"panel-pressed": ["--color-surface", "--color-press"],
448512
"panel-on": ["--color-surface", "--color-on"],
449513
"panel-on-hover": ["--color-surface", "--color-on-hover"],
450-
"raised-on-hover": ["--color-raised", "--color-on-hover"]
514+
"raised-on-hover": ["--color-raised", "--color-on-hover"],
515+
"panel-hover-on": ["--color-surface", "--color-hover", "--color-on"]
451516
}
452517
},
453518
{

0 commit comments

Comments
 (0)