| Sean McCullough | 848572d | 2025-05-13 22:27:42 +0000 | [diff] [blame] | 1 | # Component Architecture: sketch-tool-card and Related Components |
| 2 | |
| 3 | This 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 | |
| 7 | The component hierarchy and containment relationship is structured as follows: |
| 8 | |
| 9 | 1. **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** |
| 20 | - **sketch-tool-card-title** |
| 21 | - **sketch-tool-card-precommit** |
| 22 | - **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 | |
| 26 | The 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 | |
| 28 | For example, from `sketch-tool-card-bash.ts`: |
| 29 | |
| 30 | ```typescript |
| 31 | render() { |
| 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 | |
| 45 | Regarding how CSS rules defined in sketch-tool-card affect elements that contain it: |
| 46 | |
| 47 | 1. **Shadow DOM Encapsulation**: |
| 48 | |
| 49 | - 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 | |
| 52 | 2. **Slot Content Styling**: |
| 53 | |
| 54 | - 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 | |
| 58 | 3. **Style Inheritance and Sharing**: |
| 59 | |
| 60 | - 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 | |
| 64 | 4. **Parent CSS Targeting**: |
| 65 | |
| 66 | - 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 | ``` |
| 76 | |
| 77 | - This allows parent components to influence the layout of slotted components while preserving Shadow DOM encapsulation |
| 78 | |
| 79 | 5. **Host Element Styling**: |
| 80 | - The `:host` selector is used in sketch-tool-card for styling the component itself: |
| 81 | ```css |
| 82 | :host { |
| 83 | display: block; |
| 84 | max-width: 100%; |
| 85 | width: 100%; |
| 86 | box-sizing: border-box; |
| 87 | overflow: hidden; |
| 88 | } |
| 89 | ``` |
| 90 | - This affects how the component is displayed in its parent context |
| 91 | |
| 92 | In 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. |