Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

themeCss.ts

97 lines5.8 KB

1import { THEME_COLOR_ROLES } from "./themePalettes.ts";
2​
3const FETCHES_BY_STRING = /image-set\(|\bsrc\(/i;
4// Only CSS whitespace may come first. \s also matches U+00A0, which makes the address relative.
5const URL_NOT_LOCAL = /url\((?![ \t\n\f\r]*["']?(?:data:|#))/i;
6// Read tokens left to right, as CSS does: a url( inside a string loads nothing, and a quote in an
7// unquoted url( is not a string. A quoted url() that is not data: or #id keeps `url(` for the check.
8const URL_OR_STRING =
9 /url\((?![ \t\n\f\r]*["'])[^)]*|url\([ \t\n\f\r]*("[^"\n\r\f]*"|'[^'\n\r\f]*')|"[^"\n\r\f]*"|'[^'\n\r\f]*'/gi;
10​
11/** The code with its strings removed, keeping `url(` where a quoted url() can load a file. */
12function withoutStrings(code: string): string {
13 return code.replace(URL_OR_STRING, (token, urlString: string | undefined) => {
14 if (urlString !== undefined) return /^["'](?:data:|#)/i.test(urlString) ? "" : "url(";
15 return token.startsWith('"') || token.startsWith("'") ? "" : token;
16 });
18​
19function fetchesOutsideStrings(code: string): boolean {
20 return FETCHES_BY_STRING.test(code) || URL_NOT_LOCAL.test(code);
22​
23/**
24 * Whether one declaration value can load a remote file. Clients drop those declarations.
25 * A value with var() stays unparsed until use, and an escape can spell `var` or `url` in it,
26 * so every value that still has a backslash is refused.
27 */
28export function themeCssValueFetches(value: string): boolean {
29 return value.includes("\\") || fetchesOutsideStrings(withoutStrings(value));
31​
32/** A text check for early feedback. CSS escapes can dodge it; the client check is the guard. */
33export function themeCssLoadsRemoteFiles(css: string): boolean {
34 const code = withoutStrings(css.replace(/\/\*[\s\S]*?\*\//g, ""));
35 return /@import/i.test(code) || fetchesOutsideStrings(code);
37​
38/**
39 * Selectors that theme CSS may rely on. Other class names are Tailwind utilities and change
40 * between releases. themeCss.test.ts in apps/web fails when one of these leaves the source.
41 */
42export const THEME_CSS_HOOKS = [
43 { selector: '[data-slot="sidebar"]', styles: "app sidebar" },
44 { selector: '[data-slot="sidebar-inner"]', styles: "sidebar surface" },
45 { selector: "[data-thread-item]", styles: "sidebar thread row" },
46 {
47 selector: '[data-slot="project-monogram"]',
48 styles: "project icon with letters; its svg holds the tile",
49 },
50 { selector: "[data-chat-header]", styles: "chat header" },
51 { selector: '[data-workspace-area="conversation"]', styles: "chat area, behind the messages" },
52 { selector: ".messages-timeline-scroll", styles: "chat scroller" },
53 { selector: "[data-timeline-row-kind]", styles: 'chat row, such as ="message" or ="work"' },
54 { selector: '[data-message-role="user"]', styles: "user message row" },
55 { selector: "[data-user-message-bubble]", styles: "user message bubble" },
56 { selector: '[data-message-role="assistant"]', styles: "assistant message row" },
57 { selector: ".chat-markdown", styles: "assistant text" },
58 { selector: ".chat-markdown-codeblock", styles: "code block" },
59 { selector: "[data-chat-composer-form]", styles: "composer" },
60 { selector: '[data-slot="composer-host"]', styles: "composer card" },
61 { selector: ".composer-tiptap", styles: "composer text" },
62 { selector: "[data-thread-details-card]", styles: "thread details card" },
63 { selector: "[data-thread-terminal-drawer]", styles: "terminal drawer" },
64 { selector: "[data-settings-page-scroll]", styles: "settings page" },
65 { selector: '[data-slot="button"]', styles: "button" },
66 { selector: '[data-slot="input"]', styles: "text input" },
67 { selector: '[data-slot="switch"]', styles: "switch" },
68 { selector: '[data-slot="select-trigger"]', styles: "select" },
69 { selector: '[data-slot="dialog-popup"]', styles: "dialog" },
70 { selector: '[data-slot="popover-popup"]', styles: "popover" },
71 { selector: '[data-slot="menu-popup"]', styles: "menu" },
72 { selector: '[data-slot="tooltip-popup"]', styles: "tooltip" },
73 { selector: '[data-slot="scroll-area-scrollbar"]', styles: "overlay scrollbar" },
74 { selector: '[data-slot="scroll-area-thumb"]', styles: "overlay scrollbar thumb" },
75] as const;
76​
77/** What an agent needs to restyle the app. `t3_theme_read` returns it. */
78export const THEME_GUIDE = [
79 "Themes restyle T3 Code on web and desktop. Mobile does not use them.",
80 "",
81 "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.",
82 `Roles: ${THEME_COLOR_ROLES.join(", ")}.`,
83 "",
84 "CSS: `css` applies after the app's styles. Use `.dark` or `:root:not(.dark)` for one appearance.",
85 "It cannot load files: no @import, no image-set(), and url() only with data: or #id.",
86 '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.',
87 "The app's own rules can be more specific. If a rule has no effect, add !important.",
88 "It cannot reach the terminal text, the file tree, or diff internals. Use color roles for those.",
89 "Native scrollbars take `::-webkit-scrollbar` and `::-webkit-scrollbar-thumb`.",
90 "Icons are `svg.lucide`. stroke-width, stroke-linecap, and stroke-linejoin restyle them.",
91 "",
92 "Stable selectors (other class names change between releases, so do not use them):",
93 ...THEME_CSS_HOOKS.map((hook) => `- ${hook.selector}: ${hook.styles}`),
94 "",
95 "You cannot see the result. Make small changes, then ask the user how it looks.",
96 "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.",
97].join("\n");