This commit is contained in:
Dallin Romney 2025-05-20 23:54:29 -07:00
parent f4d56e6545
commit 6eccee3f80
4 changed files with 152 additions and 108 deletions

View file

@ -22,8 +22,8 @@ const tabBackgroundVar = varWithFallback("background"); // --vscode-tab-inactive
const tabForegroundVar = varWithFallback("foreground"); // --vscode-tab-inactiveForeground
const tabHoverBackgroundVar = varWithFallback("list-hover"); // --vscode-tab-hoverBackground
const tabHoverForegroundVar = varWithFallback("foreground"); // --vscode-tab-hoverForeground
const tabSelectedBackgroundVar = varWithFallback("list-hover"); // --vscode-tab-activeBackground
const tabSelectedForegroundVar = varWithFallback("list-active-foreground"); // --vscode-tab-activeForeground
const tabSelectedBackgroundVar = varWithFallback("background"); // --vscode-tab-activeBackground
const tabSelectedForegroundVar = varWithFallback("foreground"); // --vscode-tab-activeForeground
const tabAccentVar = varWithFallback("accent"); // --vscode-tab-activeBorderTop
const TabBarContainer = styled.div`

View file

@ -25,7 +25,7 @@ body,
#root {
height: 100%;
line-height: 1.3;
background-color: var(--vscode-sideBar-background);
/* background-color: var(--vscode-sideBar-background); */
font-family:
system-ui,
-apple-system,
@ -42,7 +42,7 @@ body,
body {
padding: 0;
color: var(--vscode-sideBar-foreground);
/* color: var(--vscode-sideBar-foreground); */
padding: 0px;
margin: 0px;
height: 100%;

View file

@ -8,35 +8,39 @@ import { useNavigate } from "react-router-dom";
import { Button } from "../components";
import { IdeMessengerContext } from "../context/IdeMessenger";
import { isJetBrains } from "../util";
import { ROUTES } from "../util/navigation";
import {
clearThemeLocalCache,
setDocumentStylesFromTheme,
THEME_COLORS,
varWithFallback,
} from "./theme";
const ThemeTailwindClassExample = ({
className,
varName,
colorName,
varNames,
defaultColor,
isMissing,
}: {
className: string;
varName: string;
colorName: string;
varNames: string[];
defaultColor: string;
isMissing?: boolean;
}) => {
return (
<>
<div className={`line-clamp-1 break-all px-2 text-right`}>
{className}
{colorName}
</div>
<div
className={`line-clamp-1 break-all p-1 text-[9px] ${isMissing ? "text-error" : ""}`}
style={{
backgroundColor: `var(${varName})`,
backgroundColor: varWithFallback(
colorName as keyof typeof THEME_COLORS,
),
}}
>
{varName}
{varNames.join(", ")}
</div>
<div
className={`line-clamp-1 break-all p-1 text-[9px]`}
@ -71,11 +75,17 @@ function ThemePage() {
const checkMissingClasses = () => {
const missingVars: string[] = [];
Object.entries(THEME_COLORS).forEach(([colorName, themeVals]) => {
const value = getComputedStyle(document.documentElement).getPropertyValue(
themeVals.var,
);
if (!value) {
missingVars.push(`${colorName}`); //: ${themeVals.var}`);
let found = false;
themeVals.vars.forEach((cssVar) => {
const value = getComputedStyle(
document.documentElement,
).getPropertyValue(cssVar);
if (value) {
found = true;
}
});
if (!found) {
missingVars.push(colorName);
}
});
setMissingVars(missingVars);
@ -88,7 +98,7 @@ function ThemePage() {
<div className="flex flex-1 flex-col gap-3 px-2 py-2">
<span
className="flex cursor-pointer flex-row items-center gap-2"
onClick={() => navigate("/more")}
onClick={() => navigate(ROUTES.HOME)}
>
<span className="">←</span>
Back to Settings
@ -117,7 +127,7 @@ function ThemePage() {
</div>
<input
className="bg-input text-input-foreground border-input-border focus:border-border-focus active:border-border-focus active:ring-border-focus placeholder:text-input-placeholder focus:bg-input-focus focus:text-input-focus-foreground rounded-md border border-solid p-2 focus:ring-0"
className="bg-input text-input-foreground border-input-border placeholder:text-input-placeholder focus:border-border-focus rounded-md border border-solid p-2 focus:outline-none"
placeholder="Input example"
/>
@ -125,30 +135,49 @@ function ThemePage() {
<span className="px-2">Badge</span>
</div>
<div className="flex gap-3">
<div className="flex items-center gap-2">
<CheckCircleIcon className="text-success h-4 w-4" />
<span className="text-success">Success</span>
</div>
<div className="flex items-center gap-2">
<ExclamationCircleIcon className="text-warning h-4 w-4" />
<span className="text-warning">Warning</span>
</div>
<div className="flex items-center gap-2">
<ExclamationTriangleIcon className="text-error h-4 w-4" />
<span className="text-error">Error</span>
</div>
<div className="flex items-center gap-2">
<CheckCircleIcon className="text-success h-4 w-4" />
<span className="text-success">Success</span>
</div>
<div className="flex items-center gap-2">
<ExclamationCircleIcon className="text-warning h-4 w-4" />
<span className="text-warning">Warning</span>
</div>
<div className="flex items-center gap-2">
<ExclamationTriangleIcon className="text-error h-4 w-4" />
<span className="text-error">Error</span>
</div>
<div
onClick={() => setListToggled(!listToggled)}
className={`cursor-pointer p-2 ${listToggled ? "bg-list-active text-list-active-foreground" : "bg-background text-foreground hover:bg-list-hover"}`}
>
List item (click to select)
List item (clickable)
</div>
<div className="bg-editor text-editor-foreground border-border rounded-sm border border-solid p-2">
<span>Editor</span>
</div>
<table>
<tbody>
<tr>
<td className="border-border border-b border-solid p-2">
<span>even table row</span>
</td>
</tr>
<tr className="bg-table-oddRow">
<td className="border-border border-b border-solid p-2">
<span>odd table row</span>
</td>
</tr>
</tbody>
</table>
<div>
<span className="text-link">Link</span>
</div>
<div className="flex items-center gap-2">
<span className="bg-findMatch">find match</span>
<span className="bg-findMatch-selected">current match</span>
</div>
</div>
<div className="flex flex-col gap-2">
<h2 className="mb-2 mt-6 text-xl font-semibold">
@ -197,9 +226,9 @@ function ThemePage() {
<ThemeTailwindClassExample
isMissing={missingVars.includes(colorName)}
key={colorName}
className={colorName}
colorName={colorName}
defaultColor={val.default}
varName={val.var}
varNames={val.vars}
/>
))}
</div>

View file

@ -4,146 +4,157 @@
// The current default theme is dark with blue accents
export const THEME_COLORS = {
background: {
var: "--vscode-sideBar-background",
vars: [
"--vscode-sideBar-background",
"--vscode-panel-background",
"--vscode-editor-background",
],
default: "#1e1e1e", // dark gray
},
foreground: {
var: "--vscode-sideBar-foreground", // --vscode-editor-foreground
vars: [
"--vscode-sideBar-foreground",
"--vscode-editor-foreground",
"--vscode-panel-foreground",
], // --vscode-editor-foreground
default: "#e6e6e6", // light gray
},
"editor-background": {
var: "--vscode-editor-background",
vars: ["--vscode-editor-background"],
default: "#1e1e1e", // dark gray
},
"editor-foreground": {
var: "--vscode-editor-foreground",
vars: ["--vscode-editor-foreground"],
default: "#e6e6e6", // light gray
},
"primary-background": {
var: "--vscode-button-background",
vars: ["--vscode-button-background"],
default: "#2c5aa0", // medium blue
},
"primary-foreground": {
var: "--vscode-button-foreground",
vars: ["--vscode-button-foreground"],
default: "#ffffff", // white
},
"primary-hover": {
var: "--vscode-button-hoverBackground",
vars: ["--vscode-button-hoverBackground"],
default: "#3a6db3", // lighter blue
},
"secondary-background": {
var: "--vscode-button-secondaryBackground",
vars: ["--vscode-button-secondaryBackground"],
default: "#303030", // medium dark gray
},
"secondary-foreground": {
var: "--vscode-button-secondaryForeground",
vars: ["--vscode-button-secondaryForeground"],
default: "#e6e6e6", // light gray
},
"secondary-hover": {
var: "--vscode-button-secondaryHoverBackground",
vars: ["--vscode-button-secondaryHoverBackground"],
default: "#3a3a3a", // medium gray
},
border: {
// --vscode-panel-border
var: "--vscode-sideBar-border",
vars: ["--vscode-sideBar-border", "--vscode-panel-border"],
default: "#2a2a2a", // dark gray border
},
"border-focus": {
var: "--vscode-focusBorder",
vars: ["--vscode-focusBorder"],
default: "#3a6db3", // lighter blue
},
// Command styles are used for tip-tap editor
"command-background": {
var: "--vscode-commandCenter-background",
vars: ["--vscode-commandCenter-background"],
default: "#252525", // dark gray
},
"command-foreground": {
var: "--vscode-commandCenter-foreground",
vars: ["--vscode-commandCenter-foreground"],
default: "#e6e6e6", // light gray
},
"command-border": {
var: "--vscode-commandCenter-inactiveBorder",
vars: ["--vscode-commandCenter-inactiveBorder"],
default: "#555555", // medium gray
},
"command-border-focus": {
var: "--vscode-commandCenter-activeBorder",
vars: ["--vscode-commandCenter-activeBorder"],
default: "#4d8bf0", // bright blue
},
description: {
var: "--vscode-descriptionForeground",
vars: ["--vscode-descriptionForeground"],
default: "#b3b3b3", // medium light gray
},
"description-muted": {
var: "--vscode-list-deemphasizedForeground",
vars: ["--vscode-list-deemphasizedForeground"],
default: "#8c8c8c", // medium gray
},
"input-background": {
var: "--vscode-input-background",
vars: ["--vscode-input-background"],
default: "#2d2d2d", // dark gray
},
"input-foreground": {
var: "--vscode-input-foreground",
vars: ["--vscode-input-foreground"],
default: "#e6e6e6", // light gray
},
"input-border": {
var: "--vscode-input-border",
vars: [
"--vscode-input-border",
"--vscode-commandCenter-inactiveBorder",
"vscode-border",
],
default: "#555555", // medium gray
},
"input-placeholder": {
var: "--vscode-input-placeholderForeground",
vars: ["--vscode-input-placeholderForeground"],
default: "#9e9e9e", // medium light gray
},
"table-oddRow": {
var: "--vscode-tree-tableOddRowsBackground",
vars: ["--vscode-tree-tableOddRowsBackground"],
default: "#2d2d2d", // dark gray
},
"badge-background": {
var: "--vscode-badge-background",
vars: ["--vscode-badge-background"],
default: "#4d4d4d", // medium dark gray
},
"badge-foreground": {
var: "--vscode-badge-foreground",
vars: ["--vscode-badge-foreground"],
default: "#ffffff", // white
},
success: {
var: "--vscode-notebookStatusSuccessIcon-foreground", // "var(--vscode-testing-iconPassed, #1bbe84)" // --vscode-charts-green
vars: ["--vscode-notebookStatusSuccessIcon-foreground"], // "var(--vscode-testing-iconPassed, #1bbe84)" // --vscode-charts-green
default: "#4caf50", // green
},
warning: {
var: "--vscode-editorWarning-foreground", // --vscode-list-warningForeground
vars: ["--vscode-editorWarning-foreground"], // --vscode-list-warningForeground
default: "#ffb74d", // amber/yellow
},
error: {
var: "--vscode-editorError-foreground", // --vscode-list-errorForeground
vars: ["--vscode-editorError-foreground"], // --vscode-list-errorForeground
default: "#f44336", // red
},
link: {
var: "--vscode-textLink-foreground",
vars: ["--vscode-textLink-foreground"],
default: "#5c9ce6", // medium blue
},
accent: {
var: "--vscode-tab-activeBorderTop",
vars: ["--vscode-tab-activeBorderTop", "--vscode-focusBorder"],
default: "#4d8bf0", // bright blue
},
"find-match": {
var: "--vscode-editor-findMatchBackground", // Can't get "var(--vscode-editor-findMatchBackground, rgba(237, 18, 146, 0.5))" to work
vars: ["--vscode-editor-findMatchBackground"], // Can't get "var(--vscode-editor-findMatchBackground, rgba(237, 18, 146, 0.5))" to work
default: "#264f7840", // translucent blue
},
"find-match-selected": {
var: "--vscode-editor-findMatchHighlightBackground",
vars: ["--vscode-editor-findMatchHighlightBackground"],
default: "#ffb74d40", // translucent amber
},
"list-hover": {
// --vscode-tab-hoverBackground
var: "--vscode-list-hoverBackground",
vars: ["--vscode-list-hoverBackground"],
default: "#383838", // medium dark gray
},
"list-active": {
var: "--vscode-list-activeSelectionBackground",
vars: ["--vscode-list-activeSelectionBackground"],
default: "#2c5aa050", // translucent medium blue
},
"list-active-foreground": {
var: "--vscode-list-activeSelectionForeground",
vars: ["--vscode-list-activeSelectionForeground"],
default: "#ffffff", // white
},
};
@ -151,14 +162,15 @@ export const THEME_COLORS = {
// TODO: add fonts - GUI fonts in jetbrains differ from IDE:
// --vscode-editor-font-family;
// --vscode-font-family;
export const THEME_CSS_VARS = Object.values(THEME_COLORS).map(
(value) => value.var,
);
export const THEME_CSS_VARS = Object.values(THEME_COLORS)
.map((value) => value.vars)
.flat();
export const THEME_CSS_VAR_DEFAULTS = Object.entries(THEME_COLORS).reduce(
(acc, [_, value]) => {
acc[value.var] = value.default;
value.vars.forEach((varName) => {
acc[varName] = value.default;
});
return acc;
},
{} as Record<string, string>,
@ -172,8 +184,12 @@ export const THEME_DEFAULTS = Object.entries(THEME_COLORS).reduce(
{} as Record<string, string>,
);
export const varWithFallback = (className: keyof typeof THEME_COLORS) =>
`var(${THEME_COLORS[className].var}, ${THEME_COLORS[className].default})`;
// Generates recursive CSS variable fallback for a given color name
// e.g. var(--vscode-button-background, var(--vscode-button-foreground, #ffffff))
export const varWithFallback = (colorName: keyof typeof THEME_COLORS) =>
THEME_COLORS[colorName].vars.reduce((curr, varName) => {
return `var(${varName}, ${curr})`;
}, THEME_COLORS[colorName].default);
export const setDocumentStylesFromTheme = (
theme: Record<string, string | undefined | null>,
@ -191,47 +207,46 @@ export const setDocumentStylesFromTheme = (
// Write each theme color to the document
Object.entries(theme).forEach(([colorName, value]) => {
if (value) {
const cssVarName =
THEME_COLORS[colorName as keyof typeof THEME_COLORS]?.var;
// TODO VALUE WITHOUT ALPHA CHANNEL
document.body.style.setProperty(cssVarName, value);
document.documentElement.style.setProperty(cssVarName, value);
localStorage.setItem(colorName, value);
const themeVals = THEME_COLORS[colorName as keyof typeof THEME_COLORS];
if (!themeVals) {
console.warn(
`Receieved theme color ${colorName} which is not used by the theme`,
);
return;
}
// Remove alpha channel from all hex colors besides ones that are expected to have it
let colorVal = value;
if (
!colorName.includes("find-match") &&
colorVal.startsWith("#") &&
value.length > 7
) {
colorVal = colorVal.slice(0, 7);
}
localStorage.setItem(colorName, colorVal);
for (const cssVar of themeVals.vars) {
document.body.style.setProperty(cssVar, colorVal);
document.documentElement.style.setProperty(cssVar, colorVal);
}
} else {
console.warn(
`Theme color ${colorName} is undefined or null. Please check theme. Falling back to defaults.`,
);
}
});
// Cache values in local storage
for (const [colorName, themeVals] of Object.entries(THEME_COLORS)) {
const currentVal = document.body.style.getPropertyValue(themeVals.var);
if (currentVal) {
localStorage.setItem(colorName, currentVal);
}
}
};
export const setDocumentStylesFromLocalStorage = (checkCache: boolean) => {
for (const [colorName, themeVals] of Object.entries(THEME_COLORS)) {
const cssVarName = themeVals.var;
// Get cached values (for non-vscode IDEs)
if (checkCache) {
const cached = localStorage.getItem(colorName);
if (cached) {
document.body.style.setProperty(cssVarName, cached);
}
}
// Remove alpha channel from all hex colors besides ones that are expected to have it
if (!colorName.includes("find-match")) {
const value = getComputedStyle(document.documentElement).getPropertyValue(
cssVarName,
);
if (value.startsWith("#") && value.length > 7) {
document.body.style.setProperty(cssVarName, value.slice(0, 7));
for (const cssVar of themeVals.vars) {
// Get cached values (for non-vscode IDEs)
if (checkCache) {
const cached = localStorage.getItem(colorName);
if (cached) {
document.body.style.setProperty(cssVar, cached);
}
}
}
}