Elffuss BraidHub

kikocis/continue public

fc89067dc2f1 · 8.0 KiBRawPermalink
1// All vscode variables https://gist.github.com/estruyf/ba49203e1a7d6868e9320a4ea480c27a
2// Examples for vscode https://github.com/githubocto/tailwind-vscode/blob/main/index.js
3
4// The current default theme is dark with blue accents
5export const THEME_COLORS = {
6 background: {
7 vars: [
8 "--vscode-sideBar-background",
9 "--vscode-editor-background",
10 "--vscode-panel-background",
11 ],
12 default: "#1e1e1e", // dark gray
13 },
14 foreground: {
15 vars: [
16 "--vscode-sideBar-foreground",
17 "--vscode-editor-foreground",
18 "--vscode-panel-foreground",
19 ],
20 default: "#e6e6e6", // light gray
21 },
22 "editor-background": {
23 vars: ["--vscode-editor-background"],
24 default: "#1e1e1e", // dark gray
25 },
26 "editor-foreground": {
27 vars: ["--vscode-editor-foreground"],
28 default: "#e6e6e6", // light gray
29 },
30 "primary-background": {
31 vars: ["--vscode-button-background"],
32 default: "#2c5aa0", // medium blue
33 },
34 "primary-foreground": {
35 vars: ["--vscode-button-foreground"],
36 default: "#ffffff", // white
37 },
38 "primary-hover": {
39 vars: ["--vscode-button-hoverBackground"],
40 default: "#3a6db3", // lighter blue
41 },
42 "secondary-background": {
43 vars: ["--vscode-button-secondaryBackground"],
44 default: "#303030", // medium dark gray
45 },
46 "secondary-foreground": {
47 vars: ["--vscode-button-secondaryForeground"],
48 default: "#e6e6e6", // light gray
49 },
50 "secondary-hover": {
51 vars: ["--vscode-button-secondaryHoverBackground"],
52 default: "#3a3a3a", // medium gray
53 },
54 border: {
55 vars: ["--vscode-sideBar-border", "--vscode-panel-border"],
56 default: "#2a2a2a", // dark gray border
57 },
58 "border-focus": {
59 vars: ["--vscode-focusBorder"],
60 default: "#3a6db3", // lighter blue
61 },
62 // Command styles are used for tip-tap editor
63 "command-background": {
64 vars: ["--vscode-commandCenter-background"],
65 default: "#252525", // dark gray
66 },
67 "command-foreground": {
68 vars: ["--vscode-commandCenter-foreground"],
69 default: "#e6e6e6", // light gray
70 },
71 "command-border": {
72 vars: ["--vscode-commandCenter-inactiveBorder"],
73 default: "#555555", // medium gray
74 },
75 "command-border-focus": {
76 vars: ["--vscode-commandCenter-activeBorder"],
77 default: "#4d8bf0", // bright blue
78 },
79 description: {
80 vars: ["--vscode-descriptionForeground"],
81 default: "#b3b3b3", // medium light gray
82 },
83 "description-muted": {
84 vars: ["--vscode-list-deemphasizedForeground"],
85 default: "#8c8c8c", // medium gray
86 },
87 "input-background": {
88 vars: ["--vscode-input-background"],
89 default: "#2d2d2d", // dark gray
90 },
91 "input-foreground": {
92 vars: ["--vscode-input-foreground"],
93 default: "#e6e6e6", // light gray
94 },
95 "input-border": {
96 vars: [
97 "--vscode-input-border",
98 "--vscode-commandCenter-inactiveBorder",
99 "vscode-border",
100 ],
101 default: "#555555", // medium gray
102 },
103 "input-placeholder": {
104 vars: ["--vscode-input-placeholderForeground"],
105 default: "#9e9e9e", // medium light gray
106 },
107 "table-oddRow": {
108 vars: ["--vscode-tree-tableOddRowsBackground"],
109 default: "#2d2d2d", // dark gray
110 },
111 "badge-background": {
112 vars: ["--vscode-badge-background"],
113 default: "#4d4d4d", // medium dark gray
114 },
115 "badge-foreground": {
116 vars: ["--vscode-badge-foreground"],
117 default: "#ffffff", // white
118 },
119 success: {
120 vars: [
121 "--vscode-notebookStatusSuccessIcon-foreground",
122 "--vscode-testing-iconPassed",
123 "--vscode-gitDecoration-addedResourceForeground",
124 "--vscode-charts-green",
125 ],
126 default: "#4caf50", // green
127 },
128 warning: {
129 vars: [
130 "--vscode-editorWarning-foreground",
131 "--vscode-list-warningForeground",
132 ],
133 default: "#ffb74d", // amber/yellow
134 },
135 error: {
136 vars: ["--vscode-editorError-foreground", "--vscode-list-errorForeground"],
137 default: "#f44336", // red
138 },
139 link: {
140 vars: ["--vscode-textLink-foreground"],
141 default: "#5c9ce6", // medium blue
142 },
143 textCodeBlockBackground: {
144 vars: ["--vscode-textCodeBlock-background"],
145 default: "#1e1e1e", // same as editor-background
146 },
147 accent: {
148 vars: ["--vscode-tab-activeBorderTop", "--vscode-focusBorder"],
149 default: "#4d8bf0", // bright blue
150 },
151 "find-match": {
152 vars: ["--vscode-editor-findMatchBackground"], // Can't get "var(--vscode-editor-findMatchBackground, rgba(237, 18, 146, 0.5))" to work
153 default: "#264f7840", // translucent blue
154 },
155 "find-match-selected": {
156 vars: ["--vscode-editor-findMatchHighlightBackground"],
157 default: "#ffb74d40", // translucent amber
158 },
159 "list-hover": {
160 // --vscode-tab-hoverBackground
161 vars: ["--vscode-list-hoverBackground"],
162 default: "#383838", // medium dark gray
163 },
164 "list-active": {
165 vars: ["--vscode-list-activeSelectionBackground"],
166 default: "#2c5aa050", // translucent medium blue
167 },
168 "list-active-foreground": {
169 vars: ["--vscode-list-activeSelectionForeground"],
170 default: "#ffffff", // white
171 },
172};
173
174// TODO: add fonts - GUI fonts in jetbrains differ from IDE:
175// --vscode-editor-font-family;
176// --vscode-font-family;
177export const THEME_CSS_VARS = Object.values(THEME_COLORS)
178 .map((value) => value.vars)
179 .flat();
180
181export const THEME_CSS_VAR_DEFAULTS = Object.entries(THEME_COLORS).reduce(
182 (acc, [_, value]) => {
183 value.vars.forEach((varName) => {
184 acc[varName] = value.default;
185 });
186 return acc;
187 },
188 {} as Record<string, string>,
189);
190
191export const THEME_DEFAULTS = Object.entries(THEME_COLORS).reduce(
192 (acc, [key, value]) => {
193 acc[key] = value.default;
194 return acc;
195 },
196 {} as Record<string, string>,
197);
198
199// Generates recursive CSS variable fallback for a given color name
200// e.g. var(--vscode-button-background, var(--vscode-button-foreground, #ffffff))
201export const getRecursiveVar = (vars: string[], defaultColor: string) => {
202 return [...vars].reverse().reduce((curr, varName) => {
203 return `var(${varName}, ${curr})`;
204 }, defaultColor);
205};
206
207export const varWithFallback = (colorName: keyof typeof THEME_COLORS) => {
208 const themeVals = THEME_COLORS[colorName];
209 if (!themeVals) {
210 throw new Error(`Invalid theme color name ${colorName}`);
211 }
212 return getRecursiveVar(themeVals.vars, themeVals.default);
213};
214
215export const setDocumentStylesFromTheme = (
216 theme: Record<string, string | undefined | null>,
217) => {
218 // Check for extraneous theme items
219 Object.entries(theme).forEach(([colorName, value]) => {
220 const themeVals = THEME_COLORS[colorName as keyof typeof THEME_COLORS];
221 if (!themeVals) {
222 console.warn(
223 `Receieved theme color ${colorName} which is not used by the theme`,
224 );
225 return;
226 }
227 });
228
229 // Write theme values to document
230 const missingColors: string[] = [];
231 Object.entries(THEME_COLORS).forEach(([colorName, settings]) => {
232 let colorVal = settings.default;
233 const newColor = theme[colorName];
234 if (newColor) {
235 colorVal = newColor;
236 // Remove alpha channel from all hex colors (seems to cause bad colors)
237 if (newColor.startsWith("#") && newColor.length > 7) {
238 colorVal = colorVal.slice(0, 7);
239 }
240 } else {
241 missingColors.push(colorName);
242 // console.warn(
243 // `Missing theme color: ${colorName}. Falling back to default ${colorVal}`,
244 // );
245 }
246
247 localStorage.setItem(colorName, colorVal);
248 for (const cssVar of settings.vars) {
249 document.body.style.setProperty(cssVar, colorVal);
250 document.documentElement.style.setProperty(cssVar, colorVal);
251 }
252 });
253
254 return missingColors;
255};
256
257export const setDocumentStylesFromLocalStorage = (checkCache: boolean) => {
258 for (const [colorName, themeVals] of Object.entries(THEME_COLORS)) {
259 for (const cssVar of themeVals.vars) {
260 // Get cached values (for non-vscode IDEs)
261 if (checkCache) {
262 const cached = localStorage.getItem(colorName);
263 if (cached) {
264 document.body.style.setProperty(cssVar, cached);
265 }
266 }
267 }
268 }
269};
270
271export const clearThemeLocalCache = () => {
272 for (const colorName of Object.keys(THEME_COLORS)) {
273 localStorage.removeItem(colorName);
274 }
275};