Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): color JSON syntax highlighting with the JSON value tokens (#18635)

Trang Nguyen7b12374de2ea
diff --git a/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx b/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx
index 5f0d5bf100..56bd2d6476 100644
--- a/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx
+++ b/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx
@@ -1,7 +1,22 @@
import { type ComponentProps, type ReactNode } from "react";
-import { Highlight, themes } from "prism-react-renderer";
+import { Highlight, themes, type PrismTheme } from "prism-react-renderer";
import { useTheme } from "next-themes";
+/** JSON token colors shared with the table JSON viewer; Prism aliases `null` to `keyword`. */
+const JSON_TOKEN_STYLES: PrismTheme["styles"] = [
+ { types: ["property"], style: { color: "hsl(var(--muted-foreground))" } },
+ { types: ["string"], style: { color: "var(--json-value-string)" } },
+ { types: ["number"], style: { color: "var(--json-value-number)" } },
+ { types: ["boolean"], style: { color: "var(--json-value-boolean)" } },
+ {
+ types: ["null", "keyword"],
+ style: { color: "var(--json-value-nullish)" },
+ },
+].map((entry) => ({ ...entry, languages: ["json"] }));
+
+const LIGHT_THEME = withJsonTokens(themes.github);
+const DARK_THEME = withJsonTokens(themes.vsDark);
+
/** Shares token rendering while callers own the code container and controls. */
export function SyntaxHighlight({
code,
@@ -24,7 +39,7 @@ export function SyntaxHighlight({
<Highlight
code={code}
language={language}
- theme={appliedTheme === "dark" ? themes.vsDark : themes.github}
+ theme={appliedTheme === "dark" ? DARK_THEME : LIGHT_THEME}
>
{({ className, style, tokens, getLineProps, getTokenProps }) =>
children({
@@ -42,3 +57,7 @@ export function SyntaxHighlight({
</Highlight>
);
}
+
+function withJsonTokens(theme: PrismTheme): PrismTheme {
+ return { ...theme, styles: [...theme.styles, ...JSON_TOKEN_STYLES] };
+}
diff --git a/web/src/features/traces/components/AdvancedJsonViewer/hooks/useJsonTheme.ts b/web/src/features/traces/components/AdvancedJsonViewer/hooks/useJsonTheme.ts
index 9c41054bdc..50d33fba51 100644
--- a/web/src/features/traces/components/AdvancedJsonViewer/hooks/useJsonTheme.ts
+++ b/web/src/features/traces/components/AdvancedJsonViewer/hooks/useJsonTheme.ts
@@ -11,16 +11,16 @@ import { type JSONTheme, type PartialJSONTheme } from "../types";
/**
* Default light theme
- * Colors inspired by GitHub's syntax highlighting
+ * Value colors are the `--json-value-*` tokens, so light and dark resolve in CSS.
*/
const defaultLightTheme: JSONTheme = {
background: "hsl(var(--background))",
foreground: "hsl(var(--foreground))",
- keyColor: "#0550ae", // Blue for keys (GitHub property color)
- stringColor: "#0a3069", // Dark blue for strings (GitHub string color)
- numberColor: "#0550ae", // Blue for numbers
- booleanColor: "#0550ae", // Blue for booleans
- nullColor: "hsl(var(--muted-foreground))",
+ keyColor: "hsl(var(--muted-foreground))",
+ stringColor: "var(--json-value-string)",
+ numberColor: "var(--json-value-number)",
+ booleanColor: "var(--json-value-boolean)",
+ nullColor: "var(--json-value-nullish)",
punctuationColor: "hsl(var(--muted-foreground))",
lineNumberColor: "hsl(var(--muted-foreground))",
expandButtonColor: "hsl(var(--muted-foreground))",
@@ -36,14 +36,9 @@ const defaultLightTheme: JSONTheme = {
/**
* Default dark theme
- * Colors inspired by GitHub's dark mode syntax highlighting
*/
const defaultDarkTheme: JSONTheme = {
...defaultLightTheme,
- keyColor: "#79c0ff", // Light blue for keys (GitHub dark property color)
- stringColor: "#a5d6ff", // Lighter blue for strings (GitHub dark string color)
- numberColor: "#79c0ff", // Light blue for numbers
- booleanColor: "#79c0ff", // Light blue for booleans
searchMatchBackground: "rgba(255, 255, 0, 0.2)",
searchCurrentBackground: "rgba(255, 255, 0, 0.35)",
};