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

themeCss.ts

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