Pro Feature: Visual Editing
Relevant source files
The following files were used as context for generating this wiki page:
- src/pro/main/ipc/handlers/visual_editing_handlers.ts
- src/pro/ui/components/Annotator/Annotator.tsx
- worker/dyad-visual-editor-client.js
- worker/dyad-component-selector-client.js
- e2e-tests/visual_editing.spec.ts
- e2e-tests/snapshots/select_component.spec.ts_select-component-1.txt
- src/prompts/system_prompt.ts
Visual Editing is a Pro feature that allows users to select a UI component directly from the live application preview and instruct the AI to modify it. This creates a powerful and intuitive workflow where users can point at an element on the screen, describe the desired changes, and have the AI edit the corresponding source code.
The system works by injecting client-side scripts into the user's application iframe to capture user interactions. Once a component is selected, its source code location is identified, and this contextual information is passed to the AI along with the user's prompt. A UI overlay, the Annotator, highlights the selected component in the preview to provide clear visual feedback.
Architecture Overview
The Visual Editing feature spans across the Electron main process, the renderer process (UI), and the user's application running in a webview (iframe). Communication between these parts is handled via IPC (Inter-Process Communication) and the postMessage API.
The following diagram illustrates the high-level architecture of the system.
Component Selection Flow
The process of selecting a component is initiated by the user and involves coordination between the Dyad UI, the main process, and a script injected into the user's application.
- The user clicks the "Select Component" button in the Dyad UI.
- An IPC message
visual-editing:startis sent to the main process. - The main process handler injects
dyad-component-selector-client.jsinto the webview. - The injected script adds event listeners for
mouseoverandclick. As the user hovers over elements tagged with adata-dyad-id, the script sends their dimensions back to the UI to draw a hover highlight. - When the user clicks on a tagged element, the script captures the
data-dyad-id(containing the file path and component name) and sends it to the Dyad UI viawindow.top.postMessage. - The script then removes its event listeners to stop the selection mode.
- The Dyad UI receives the message and displays the
Annotatorcomponent around the selected element.
The sequence diagram below details this interaction.
AI Prompt Generation
After a component is selected, the user's subsequent chat message is augmented with information about the selection. This provides the AI with the necessary context to perform a targeted code edit.
- The UI, having received the
filePathandcomponentNamefrom the selection, sends an IPC messagevisual-editing:get-component-infoto the main process. - The handler in
visual_editing_handlers.tsreads the specified file. - It uses
@babel/parserto create an Abstract Syntax Tree (AST) of the file content. - It traverses the AST to find the exact AST node corresponding to the
componentName. - From the node, it extracts the source code snippet and location (line/column numbers).
- This information is returned to the UI and formatted into the user's prompt before being sent to the AI.
The final prompt sent to the AI includes a special section for the selected component, as shown in test snapshots.
Selected components:
Component: h1 (file: src/pages/Index.tsx)
Snippet:
<div className="text-center">
<h1 className="text-4xl font-bold mb-4">Welcome to Your Blank App</h1> // <-- EDIT HERE
<p className="text-xl text-gray-600">
Start building your amazing project here!
</p>
*Sources: [e2e-tests/snapshots/select_component.spec.ts_select-component-1.txt](https://github.com/dyad-sh/dyad/blob/HEAD/e2e-tests/snapshots/select_component.spec.ts_select-component-1.txt), [src/pro/main/ipc/handlers/visual_editing_handlers.ts:31-62](https://github.com/dyad-sh/dyad/blob/HEAD/src/pro/main/ipc/handlers/visual_editing_handlers.ts#L31-L62)*
## Key Components & Files
The feature is implemented across several key files, each with a distinct responsibility.
| File | Responsibility |
| --- | --- |
| `src/pro/main/ipc/handlers/visual_editing_handlers.ts` | Handles backend logic for starting/stopping selection mode and parsing source files with Babel to extract component snippets. |
| `src/pro/ui/components/Annotator/Annotator.tsx` | A React component that renders the visual overlay (annotation box) around the selected element in the app preview. |
| `worker/dyad-component-selector-client.js` | A client-side script injected into the user's app to detect hovers and clicks on components tagged with `data-dyad-id`. |
| `worker/dyad-visual-editor-client.js` | A client-side script that listens for `postMessage` events to show or hide the annotation box overlay within the iframe. |
| `e2e-tests/visual_editing.spec.ts` | End-to-end test that validates the entire visual editing flow from a user's perspective. |
## Summary
The Visual Editing feature provides a highly interactive and intuitive way for users to request code changes. By combining script injection, DOM event handling, AST parsing, and a clear UI overlay, it successfully bridges the gap between the visual representation of a component and its underlying source code. This allows the AI to receive precise, context-aware instructions, leading to more accurate and efficient code generation.