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