Elffuss BraidHub

kikocis/continue public

ea05c35331da · 8.1 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 terminal: {
144 vars: ["--vscode-terminal-ansiGreen"],
145 default: "#0dbc79", // green
146 },
147 textCodeBlockBackground: {
148 vars: ["--vscode-textCodeBlock-background"],
149 default: "#1e1e1e", // same as editor-background
150 },
151 accent: {
152 vars: ["--vscode-tab-activeBorderTop", "--vscode-focusBorder"],
153 default: "#4d8bf0", // bright blue
154 },
155 "find-match": {
156 vars: ["--vscode-editor-findMatchBackground"], // Can't get "var(--vscode-editor-findMatchBackground, rgba(237, 18, 146, 0.5))" to work
157 default: "#264f7840", // translucent blue
158 },
159 "find-match-selected": {
160 vars: ["--vscode-editor-findMatchHighlightBackground"],
161 default: "#ffb74d40", // translucent amber
162 },
163 "list-hover": {
164 // --vscode-tab-hoverBackground
165 vars: ["--vscode-list-hoverBackground"],
166 default: "#383838", // medium dark gray
167 },
168 "list-active": {
169 vars: ["--vscode-list-activeSelectionBackground"],
170 default: "#2c5aa050", // translucent medium blue
171 },
172 "list-active-foreground": {
173 vars: ["--vscode-list-activeSelectionForeground"],
174 default: "#ffffff", // white
175 },
176};
177
178// TODO: add fonts - GUI fonts in jetbrains differ from IDE:
179// --vscode-editor-font-family;
180// --vscode-font-family;
181export const THEME_CSS_VARS = Object.values(THEME_COLORS)
182 .map((value) => value.vars)
183 .flat();
184
185export const THEME_CSS_VAR_DEFAULTS = Object.entries(THEME_COLORS).reduce(
186 (acc, [_, value]) => {
187 value.vars.forEach((varName) => {
188 acc[varName] = value.default;
189 });
190 return acc;
191 },
192 {} as Record<string, string>,
193);
194
195export const THEME_DEFAULTS = Object.entries(THEME_COLORS).reduce(
196 (acc, [key, value]) => {
197 acc[key] = value.default;
198 return acc;
199 },
200 {} as Record<string, string>,
201);
202
203// Generates recursive CSS variable fallback for a given color name
204// e.g. var(--vscode-button-background, var(--vscode-button-foreground, #ffffff))
205export const getRecursiveVar = (vars: string[], defaultColor: string) => {
206 return [...vars].reverse().reduce((curr, varName) => {
207 return `var(${varName}, ${curr})`;
208 }, defaultColor);
209};
210
211export const varWithFallback = (colorName: keyof typeof THEME_COLORS) => {
212 const themeVals = THEME_COLORS[colorName];
213 if (!themeVals) {
214 throw new Error(`Invalid theme color name ${colorName}`);
215 }
216 return getRecursiveVar(themeVals.vars, themeVals.default);
217};
218
219export const setDocumentStylesFromTheme = (
220 theme: Record<string, string | undefined | null>,
221) => {
222 // Check for extraneous theme items
223 Object.entries(theme).forEach(([colorName, value]) => {
224 const themeVals = THEME_COLORS[colorName as keyof typeof THEME_COLORS];
225 if (!themeVals) {
226 console.warn(
227 `Receieved theme color ${colorName} which is not used by the theme`,
228 );
229 return;
230 }
231 });
232
233 // Write theme values to document
234 const missingColors: string[] = [];
235 Object.entries(THEME_COLORS).forEach(([colorName, settings]) => {
236 let colorVal = settings.default;
237 const newColor = theme[colorName];
238 if (newColor) {
239 colorVal = newColor;
240 // Remove alpha channel from all hex colors (seems to cause bad colors)
241 if (newColor.startsWith("#") && newColor.length > 7) {
242 colorVal = colorVal.slice(0, 7);
243 }
244 } else {
245 missingColors.push(colorName);
246 // console.warn(
247 // `Missing theme color: ${colorName}. Falling back to default ${colorVal}`,
248 // );
249 }
250
251 localStorage.setItem(colorName, colorVal);
252 for (const cssVar of settings.vars) {
253 document.body.style.setProperty(cssVar, colorVal);
254 document.documentElement.style.setProperty(cssVar, colorVal);
255 }
256 });
257
258 return missingColors;
259};
260
261export const setDocumentStylesFromLocalStorage = (checkCache: boolean) => {
262 for (const [colorName, themeVals] of Object.entries(THEME_COLORS)) {
263 for (const cssVar of themeVals.vars) {
264 // Get cached values (for non-vscode IDEs)
265 if (checkCache) {
266 const cached = localStorage.getItem(colorName);
267 if (cached) {
268 document.body.style.setProperty(cssVar, cached);
269 }
270 }
271 }
272 }
273};
274
275export const clearThemeLocalCache = () => {
276 for (const colorName of Object.keys(THEME_COLORS)) {
277 localStorage.removeItem(colorName);
278 }
279};