Feature: Visual Edit
Relevant source files
The following files were used as context for generating this wiki page:Visual Edit is a feature of React Grab that allows developers to make changes to their application's UI directly from the browser. By clicking on any part of the app, a developer can provide a natural language prompt describing the desired change. React Grab then leverages an AI coding agent to apply these changes to the source code in real-time. This workflow eliminates the need to manually search for component files, significantly speeding up the UI iteration process.
The feature is designed to be tool-agnostic, integrating with various existing AI coding agents. It works by capturing precise context about the selected element—including the component stack, file paths, and line numbers—and passing this information to the agent. This allows the agent to bypass the code search phase and proceed directly to implementation, resulting in an average edit time of under 6 seconds in benchmark tests.
Sources: packages/website/app/blog/visual-edit/page.tsx:300-309, 331-337, packages/website/app/blog/agent/page.tsx:206-218
Architecture and Data Flow
The Visual Edit feature operates on a client-server architecture. The client-side script, running in the browser during development, captures user interactions. The server-side component acts as a bridge between the browser and the chosen AI coding agent's CLI or SDK.
Sources: packages/website/app/blog/agent/page.tsx:206-218, 417-438
High-Level Workflow
The following diagram illustrates the end-to-end process from a user's request to the code being updated.
This sequence diagram shows how a user prompt is captured by the client, sent to a local server, processed by an AI agent, and results in file system changes, with status updates streamed back to the user.
Sources: packages/website/app/blog/agent/page.tsx:206-218, packages/react-grab/README.md:83-89
Context Gathering
When an element is selected, React Grab walks up the React fiber tree to gather context. This is the core mechanism that makes the process efficient.
- Fiber Tree Traversal: Starting from the clicked DOM element, it walks up the React fiber tree.
- Source Location: In development mode, it collects component display names and their source locations (
fileName,lineNumber,columnNumber). - DOM Snippet: It captures a small slice of the DOM and attributes around the selected node.
This rich context is then packaged and sent to the agent, eliminating any guesswork.
Sources: packages/website/app/blog/agent/page.tsx:421-429, packages/website/app/blog/intro/page.tsx:265-272
Installation and Setup
Visual Edit is enabled by running an initialization script and adding agent-specific configurations.
Automatic Installation
The recommended way to install React Grab and configure Visual Edit is by using the CLI at the project root.
npx grab@latest init
This command automatically detects the project's framework (Next.js, Vite, etc.) and injects the necessary scripts. If an agent is selected, it also modifies the package.json to prepend the agent's server command to the dev script.
Sources: packages/website/app/blog/visual-edit/page.tsx:355-361, packages/react-grab/README.md:18-22, packages/cli/test/transform.test.ts:23-32, 513-522
Manual Setup for Agent Providers
For manual setup, you need to add both a client-side script to your application's HTML head and a server-side command to your development process.
Example: Next.js App Router Layout (app/layout.tsx)
import Script from "next/script";
export default function RootLayout({ children }) {
return (
<html>
<head>
{process.env.NODE_ENV === "development" && (
<>
<Script
src="//unpkg.com/react-grab/dist/index.global.js"
strategy="beforeInteractive"
/>
<Script
src="//unpkg.com/@react-grab/cursor/dist/client.global.js"
strategy="lazyOnload"
/>
</>
)}
</head>
<body>{children}</body>
</html>
);
}
Sources: packages/react-grab/README.md:120-138, packages/website/app/blog/agent/page.tsx:300-318
Example: package.json script modification
{
"scripts": {
"dev": "npx @react-grab/cursor@latest && next dev"
}
}
Sources: packages/react-grab/README.md:111-115, packages/cli/test/transform.test.ts:513-522
Agent Provider Integrations
Visual Edit integrates with multiple coding agents through dedicated provider packages. Each provider runs a local server on a specific port that interfaces with the corresponding agent's CLI or SDK.
| Agent Provider | Package Name | Default Port |
|---|---|---|
| Claude Code | @react-grab/claude-code | 4567 |
| Cursor CLI | @react-grab/cursor | 5567 |
| OpenCode | @react-grab/opencode | 6567 |
| Codex | @react-grab/codex | 7567 |
| Gemini | @react-grab/gemini | 8567 |
| Amp | @react-grab/amp | 9567 |
| Factory Droid | @react-grab/droid | 10567 |
Sources: CONTRIBUTING.md:27-38, packages/react-grab/README.md:83-294, packages/website/app/blog/agent/page.tsx:258-415
The project structure reflects this modular approach, with each provider located in its own package within the packages/ directory.
Sources: CONTRIBUTING.md:24-38
Performance Benchmarks
A key advantage of Visual Edit is its speed. By providing exact file paths and line numbers, it eliminates the agent's need to search the codebase, which is often a significant bottleneck.
Benchmarks run on a shadcn/ui dashboard with 20 UI editing tasks showed that Visual Edit makes agents significantly faster.
| Metric | Without React Grab (Control) | With React Grab (Treatment) | Improvement |
|---|---|---|---|
| Average Time | ~17 seconds | ~6 seconds | ~3x faster |
Sources: packages/website/app/blog/visual-edit/page.tsx:331-337, packages/website/app/blog/intro/page.tsx:151-154, 345-350
The following chart shows the distribution of edit times for the 20 tasks, highlighting the consistent performance gain.
This diagram shows that Visual Edit leads to a more predictable and faster edit time by eliminating the variable "search phase".