The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { THEME_COLOR_ROLES } from "./themePalettes.ts"; |
| 2 | |
| 3 | const FETCHES_BY_STRING = /image-set\(|\bsrc\(/i; |
| 4 | // Only CSS whitespace may come first. \s also matches U+00A0, which makes the address relative. |
| 5 | const URL_NOT_LOCAL = /url\((?![ \t\n\f\r]*["']?(?:data:|#))/i; |
| 6 | // Read tokens left to right, as CSS does: a url( or /* inside a string loads nothing, and a quote |
| 7 | // in an unquoted url( or after a backslash is not a string. A quoted url() that is not data: or #id |
| 8 | // keeps `url(` for the check. |
| 9 | const ESCAPE = String.raw`\\(?:(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{1,5}(?![0-9a-fA-F]))(?:\r\n|[\n\r\f])?|\r\n|[^0-9a-fA-F])`; |
| 10 | const STRING = String.raw`"(?:${ESCAPE}|[^"\\\n\r\f])*"|'(?:${ESCAPE}|[^'\\\n\r\f])*'`; |
| 11 | const URL_OR_STRING = String.raw`url\((?![ \t\n\f\r]*["'])([^)]*)|url\([ \t\n\f\r]*(${STRING})|${STRING}|${ESCAPE}`; |
| 12 | const CSS_TOKENS = new RegExp(String.raw`\/\*[\s\S]*?\*\/|${URL_OR_STRING}`, "gi"); |
| 13 | |
| 14 | /** |
| 15 | * The code with its strings and comments removed, keeping `url(` where a quoted url() can load a |
| 16 | * file. A declaration value keeps a comment or quoted url() that holds an escape. In a stylesheet |
| 17 | * an unquoted url() is judged by its address too. |
| 18 | */ |
| 19 | function withoutStrings(code: string, stylesheet: boolean): string { |
| 20 | return code.replace(CSS_TOKENS, (token, address?: string, urlString?: string) => { |
| 21 | const url = stylesheet ? (address ?? urlString) : urlString; |
| 22 | if (url !== undefined) { |
| 23 | if (!stylesheet && url.includes("\\")) return token; |
| 24 | return /^[ \t\n\f\r]*["']?(?:data:|#)/i.test(url) ? "" : "url("; |
| 25 | } |
| 26 | if (token.startsWith("/*")) { |
| 27 | if (stylesheet) return ""; |
| 28 | return token.includes("\\") ? token : " "; |
| 29 | } |
| 30 | return /^["']/.test(token) ? "" : token; |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | function fetchesOutsideStrings(code: string): boolean { |
| 35 | return FETCHES_BY_STRING.test(code) || URL_NOT_LOCAL.test(code); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Whether one declaration value can load a remote file. Clients drop those declarations. |
| 40 | * Values with var() can keep escapes that spell `var` or `url`. Refuse escapes that survive |
| 41 | * removing plain strings, including escapes in URL addresses. |
| 42 | */ |
| 43 | export function themeCssValueFetches(value: string): boolean { |
| 44 | const code = withoutStrings(value, false); |
| 45 | return code.includes("\\") || fetchesOutsideStrings(code); |
| 46 | } |
| 47 | |
| 48 | /** A text check for early feedback. CSS escapes can dodge it; the client check is the guard. */ |
| 49 | export function themeCssLoadsRemoteFiles(css: string): boolean { |
| 50 | const code = withoutStrings(css, true); |
| 51 | return /@import/i.test(code) || fetchesOutsideStrings(code); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Selectors that theme CSS may rely on. Other class names are Tailwind utilities and change |
| 56 | * between releases. themeCss.test.ts in apps/web fails when one of these leaves the source. |
| 57 | */ |
| 58 | export const THEME_CSS_HOOKS = [ |
| 59 | { selector: '[data-slot="sidebar"]', styles: "app sidebar" }, |
| 60 | { selector: '[data-slot="sidebar-inner"]', styles: "sidebar surface" }, |
| 61 | { selector: "[data-thread-item]", styles: "sidebar thread row" }, |
| 62 | { |
| 63 | selector: '[data-slot="project-monogram"]', |
| 64 | styles: "project icon with letters; its svg holds the tile", |
| 65 | }, |
| 66 | { selector: "[data-chat-header]", styles: "chat header" }, |
| 67 | { selector: '[data-workspace-area="conversation"]', styles: "chat area, behind the messages" }, |
| 68 | { selector: ".messages-timeline-scroll", styles: "chat scroller" }, |
| 69 | { selector: "[data-timeline-row-kind]", styles: 'chat row, such as ="message" or ="work"' }, |
| 70 | { selector: '[data-message-role="user"]', styles: "user message row" }, |
| 71 | { selector: "[data-user-message-bubble]", styles: "user message bubble" }, |
| 72 | { selector: '[data-message-role="assistant"]', styles: "assistant message row" }, |
| 73 | { selector: ".chat-markdown", styles: "assistant text" }, |
| 74 | { selector: ".chat-markdown-codeblock", styles: "code block" }, |
| 75 | { selector: "[data-chat-composer-form]", styles: "composer" }, |
| 76 | { selector: '[data-slot="composer-host"]', styles: "composer card" }, |
| 77 | { selector: ".composer-tiptap", styles: "composer text" }, |
| 78 | { selector: "[data-thread-details-card]", styles: "thread details card" }, |
| 79 | { selector: "[data-thread-terminal-drawer]", styles: "terminal drawer" }, |
| 80 | { selector: "[data-settings-page-scroll]", styles: "settings page" }, |
| 81 | { selector: '[data-slot="button"]', styles: "button" }, |
| 82 | { selector: '[data-slot="input"]', styles: "text input" }, |
| 83 | { selector: '[data-slot="switch"]', styles: "switch" }, |
| 84 | { selector: '[data-slot="select-trigger"]', styles: "select" }, |
| 85 | { selector: '[data-slot="dialog-popup"]', styles: "dialog" }, |
| 86 | { selector: '[data-slot="popover-popup"]', styles: "popover" }, |
| 87 | { selector: '[data-slot="menu-popup"]', styles: "menu" }, |
| 88 | { selector: '[data-slot="tooltip-popup"]', styles: "tooltip" }, |
| 89 | { selector: '[data-slot="scroll-area-scrollbar"]', styles: "overlay scrollbar" }, |
| 90 | { selector: '[data-slot="scroll-area-thumb"]', styles: "overlay scrollbar thumb" }, |
| 91 | ] as const; |
| 92 | |
| 93 | /** What an agent needs to restyle the app. `t3_theme_read` returns it. */ |
| 94 | export const THEME_GUIDE = [ |
| 95 | "Themes restyle T3 Code on web and desktop. Mobile does not use them.", |
| 96 | "", |
| 97 | "Colors: `canvas` and `accent` (hex) derive a full palette. `colors` overrides single roles, and `variants.light` or `variants.dark` does that for the other appearance. Prefer colors over CSS.", |
| 98 | `Roles: ${THEME_COLOR_ROLES.join(", ")}.`, |
| 99 | "", |
| 100 | "CSS: `css` applies after the app's styles. Use `.dark` or `:root:not(.dark)` for one appearance.", |
| 101 | "It cannot load files: no @import, no image-set(), and url() only with data: or #id.", |
| 102 | 'For a background image, copy a JPEG, PNG, or WebP file of up to 16 MB to `<themesDir>/<themeId>.jpg` (or .png, .webp), beside the theme file. CSS shows it with `var(--theme-background-image)`, such as `[data-workspace-area="conversation"] { background: var(--theme-background-image) center / cover no-repeat; }`. Delete the file to remove it. Do not embed images in `css`: it holds 100,000 characters.', |
| 103 | "The app's own rules can be more specific. If a rule has no effect, add !important.", |
| 104 | "It cannot reach the terminal text, the file tree, or diff internals. Use color roles for those.", |
| 105 | "Native scrollbars take `::-webkit-scrollbar` and `::-webkit-scrollbar-thumb`.", |
| 106 | "Icons are `svg.lucide`. stroke-width, stroke-linecap, and stroke-linejoin restyle them.", |
| 107 | "", |
| 108 | "Stable selectors (other class names change between releases, so do not use them):", |
| 109 | ...THEME_CSS_HOOKS.map((hook) => `- ${hook.selector}: ${hook.styles}`), |
| 110 | "", |
| 111 | "You cannot see the result. Make small changes, then ask the user how it looks.", |
| 112 | "To undo, set `t3-code`, the stock T3 Code theme, or another built-in theme. An empty `current` means the stock theme or no environment theme.", |
| 113 | ].join("\n"); |