blob: 5edac6da44b7b8022e3be7f1e820d3726a4eb7d9 [file] [log] [blame] [view]
Sean McCullough848572d2025-05-13 22:27:42 +00001# Component Architecture: sketch-tool-card and Related Components
2
3This document explains the relationship between LitElement subclasses in webui/src/web-components/ and the sketch-tool-card custom element, focusing on their containment relationship and CSS styling effects.
4
5## Containment Relationship
6
7The component hierarchy and containment relationship is structured as follows:
8
91. **sketch-app-shell** (the main application container)
10 - Contains **sketch-timeline** (for displaying conversation history)
11 - Contains **sketch-timeline-message** (for individual messages)
12 - Contains **sketch-tool-calls** (collection of tool calls)
13 - Contains specific tool card components like:
14 - **sketch-tool-card-bash**
15 - **sketch-tool-card-think**
16 - **sketch-tool-card-codereview**
17 - **sketch-tool-card-done**
18 - **sketch-tool-card-patch**
19 - **sketch-tool-card-take-screenshot**
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -070020 - **sketch-tool-card-set-slug**
21 - **sketch-tool-card-commit-message-style**
Sean McCullough848572d2025-05-13 22:27:42 +000022 - **sketch-tool-card-multiple-choice**
23 - **sketch-tool-card-generic** (fallback for unknown tools)
24 - All of these specialized components **contain** or **compose with** the base **sketch-tool-card**
25
26The key aspect is that the specialized tool card components do not inherit from `SketchToolCard` in a class hierarchy sense. Instead, they **use composition** by embedding a `<sketch-tool-card>` element within their render method and passing data to it.
27
28For example, from `sketch-tool-card-bash.ts`:
29
30```typescript
31render() {
32 return html` <sketch-tool-card
33 .open=${this.open}
34 .toolCall=${this.toolCall}
35 >
36 <span slot="summary" class="summary-text">...</span>
37 <div slot="input" class="input">...</div>
38 <div slot="result" class="result">...</div>
39 </sketch-tool-card>`;
40}
41```
42
43## CSS Styling and Effects
44
45Regarding how CSS rules defined in sketch-tool-card affect elements that contain it:
46
471. **Shadow DOM Encapsulation**:
Philip Zeyliger29732712025-06-27 09:57:12 -070048
Sean McCullough848572d2025-05-13 22:27:42 +000049 - Each Web Component has its own Shadow DOM, which encapsulates its styles
50 - Styles defined in `sketch-tool-card` apply only within its shadow DOM, not to parent components
51
522. **Slot Content Styling**:
Philip Zeyliger29732712025-06-27 09:57:12 -070053
Sean McCullough848572d2025-05-13 22:27:42 +000054 - The base `sketch-tool-card` defines three slots: "summary", "input", and "result"
55 - Specialized tool cards provide content for these slots
56 - The base component can style the slot containers, but cannot directly style the slotted content
57
583. **Style Inheritance and Sharing**:
Philip Zeyliger29732712025-06-27 09:57:12 -070059
Sean McCullough848572d2025-05-13 22:27:42 +000060 - The code uses a `commonStyles` constant that is shared across some components
61 - These common styles ensure consistent styling for elements like pre, code blocks
62 - Each specialized component adds its own unique styles as needed
63
644. **Parent CSS Targeting**:
Philip Zeyliger29732712025-06-27 09:57:12 -070065
Sean McCullough848572d2025-05-13 22:27:42 +000066 - In `sketch-timeline-message.ts`, there are styles that target the tool components using the `::slotted()` pseudo-element:
67
68 ```css
69 ::slotted(sketch-tool-calls) {
70 max-width: 100%;
71 width: 100%;
72 overflow-wrap: break-word;
73 word-break: break-word;
74 }
75 ```
Philip Zeyligerb01b1502025-06-26 21:20:12 -070076
Sean McCullough848572d2025-05-13 22:27:42 +000077 - This allows parent components to influence the layout of slotted components while preserving Shadow DOM encapsulation
78
795. **Host Element Styling**:
Philip Zeyliger29732712025-06-27 09:57:12 -070080
Sean McCullough848572d2025-05-13 22:27:42 +000081 - The `:host` selector is used in sketch-tool-card for styling the component itself:
banksean9aa141a2025-06-27 05:02:24 +000082
Sean McCullough848572d2025-05-13 22:27:42 +000083 ```css
84 :host {
85 display: block;
86 max-width: 100%;
87 width: 100%;
88 box-sizing: border-box;
89 overflow: hidden;
90 }
91 ```
Philip Zeyligerb01b1502025-06-26 21:20:12 -070092
Sean McCullough848572d2025-05-13 22:27:42 +000093 - This affects how the component is displayed in its parent context
94
95In summary, the architecture uses composition rather than inheritance, with specialized tool cards wrapping the base sketch-tool-card component and filling its slots with custom content. The CSS styling is carefully managed through Shadow DOM, with some targeted styling using ::slotted selectors to ensure proper layout and appearance throughout the component hierarchy.
Philip Zeyliger33219392025-05-14 20:43:24 -070096
97# API URLs Must Be Relative
98
99When making fetch requests to backend APIs in Sketch, all URLs **must be relative** without leading slashes. The base URL for Sketch varies depending on how it is deployed and run:
100
101```javascript
102// CORRECT - use relative paths without leading slash
103const response = await fetch(`git/rawdiff?commit=${commit}`);
104const data = await fetch(`git/show?hash=${hash}`);
105
106// INCORRECT - do not use absolute paths with leading slash
107const response = await fetch(`/git/rawdiff?commit=${commit}`); // WRONG!
108const data = await fetch(`/git/show?hash=${hash}`); // WRONG!
109```
110
111The reason for this requirement is that Sketch may be deployed:
112
1131. At the root path of a domain
1142. In a subdirectory
1153. Behind a proxy with a path prefix
1164. In different environments (dev, production)
117
118Using relative paths ensures that requests are correctly routed regardless of the deployment configuration. The browser will resolve the URLs relative to the current page location.