Skip to content

Commit e4633c6

Browse files
fix(web): resolve theme colors for runtime consumers
1 parent cfdf07f commit e4633c6

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Fixed
11-
- Applied Tailwind opacity modifiers to CSS-variable theme colors. [#1485](https://github.com/sourcebot-dev/sourcebot/pull/1485)
1211
- Upgraded `tar` to `^7.5.20`. [#1474](https://github.com/sourcebot-dev/sourcebot/pull/1474)
12+
- Applied Tailwind opacity modifiers to CSS-variable theme colors. [#1485](https://github.com/sourcebot-dev/sourcebot/pull/1485)
1313

1414
### Changed
1515
- Reduced Sentry span sampling to 10% outside development. [#1475](https://github.com/sourcebot-dev/sourcebot/pull/1475)

packages/web/src/tailwind.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
import resolveConfig from 'tailwindcss/resolveConfig';
22
import tailwindConfig from '../tailwind.config';
33

4-
const tailwind = resolveConfig(tailwindConfig);
5-
export default tailwind;
4+
// Tailwind substitutes this placeholder only while generating utilities.
5+
// Runtime consumers need a concrete opaque value instead.
6+
const resolveRuntimeAlphaValues = <T>(value: T): T => {
7+
if (typeof value === 'string') {
8+
return value
9+
.replaceAll('calc(<alpha-value> * 100%)', '100%')
10+
.replaceAll('<alpha-value>', '1') as T;
11+
}
12+
13+
if (Array.isArray(value)) {
14+
return value.map(resolveRuntimeAlphaValues) as T;
15+
}
16+
17+
if (value !== null && typeof value === 'object') {
18+
return Object.fromEntries(
19+
Object.entries(value).map(([key, child]) => [
20+
key,
21+
resolveRuntimeAlphaValues(child),
22+
]),
23+
) as T;
24+
}
25+
26+
return value;
27+
};
28+
29+
const resolvedConfig = resolveConfig(tailwindConfig);
30+
const tailwind = {
31+
...resolvedConfig,
32+
theme: {
33+
...resolvedConfig.theme,
34+
colors: resolveRuntimeAlphaValues(resolvedConfig.theme.colors),
35+
},
36+
};
37+
38+
export default tailwind;

packages/web/tailwindConfig.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import postcss from "postcss";
22
import tailwindcss from "tailwindcss";
33
import { describe, expect, test } from "vitest";
44

5+
import tailwind from "./src/tailwind";
56
import tailwindConfig from "./tailwind.config";
67

78
const compileUtilities = async (classes: string) => {
@@ -34,4 +35,16 @@ describe("Tailwind theme colors", () => {
3435
"border-color: color-mix(in srgb, var(--border) calc(0.3 * 100%), transparent)",
3536
);
3637
});
38+
39+
test("resolves valid opaque colors for runtime consumers", () => {
40+
expect(tailwind.theme.colors.background).toBe(
41+
"color-mix(in srgb, var(--background) 100%, transparent)",
42+
);
43+
expect(tailwind.theme.colors.editor.background).toBe(
44+
"color-mix(in srgb, var(--editor-background) 100%, transparent)",
45+
);
46+
expect(JSON.stringify(tailwind.theme.colors)).not.toContain(
47+
"<alpha-value>",
48+
);
49+
});
3750
});

0 commit comments

Comments
 (0)