Element Selection & Context Grabbing
Relevant source files
The following files were used as context for generating this wiki page:
The Element Selection and Context Grabbing system is a core feature of react-grab. It provides a developer tool that runs in the browser during development, allowing users to point at any UI element and extract its underlying React component information, source code location, and HTML structure. The primary purpose is to generate a precise, actionable context for AI coding agents, which significantly speeds up UI development by eliminating the need for the agent to search the codebase. Instead of a vague prompt like "make this button bigger," the developer can provide the agent with the exact file path, line number, and component name.
This system is designed to be activated via a hotkey, presenting a non-intrusive overlay for element selection. Once an element is selected, the generated context can be copied to the clipboard or, through agent provider integrations, sent directly to a coding agent like Claude Code or Cursor for automated code modification. This creates a seamless "visual editing" workflow directly within the browser.
Sources: packages/react-grab/README.md, packages/website/app/blog/intro/page.tsx, packages/website/app/blog/agent/page.tsx
Core Architecture
The system is injected into a React application during development. It listens for user interactions to activate, overlays a selection UI on the page, and uses React's internal development-only APIs to inspect the component tree.
Initialization
react-grab is typically initialized by adding a script tag to the application's main HTML file or by importing it into the main entry point. This should only be done in development mode. The library exposes a public init function for manual setup and customization.
Sources: packages/react-grab/README.md, packages/react-grab/src/core.tsx
The init function accepts a configuration object to customize its behavior and appearance.
| Option | Type | Description |
|---|---|---|
theme | object | Customizes UI elements like colors (hue), crosshair, and labels. Set enabled: false to disable all UI. |
onElementSelect | (element: Element) => void | Callback function triggered when an element is selected. |
onCopySuccess | (elements, content) => void | Callback function triggered after the context is successfully copied. |
onStateChange | (state: { isActive }) => void | Callback function triggered when the activation state changes. |
Sources: packages/react-grab/README.md
An example of manual initialization:
import { init } from "react-grab/core";
if (process.env.NODE_ENV === "development") {
const api = init({
theme: {
hue: 180, // Changes the theme color
},
onElementSelect: (element) => {
console.log("Selected:", element);
},
});
}
Sources: packages/react-grab/README.md, packages/website/components/install-tabs.tsx:190-203
User Interaction Flow
The selection process is initiated by the user pressing and holding a hotkey (e.g., Cmd+C). This activates an overlay on the page, allowing the developer to select an element.
The following diagram illustrates the user interaction flow for selecting an element.
Sources: packages/react-grab/src/core.tsx
Context Extraction
After an element is selected, react-grab performs a multi-step process to gather all relevant context about it. This is the key to providing an actionable prompt for AI agents.
Sources: packages/react-grab/src/context.ts
React Fiber Tree Traversal
The core of the context extraction relies on traversing the React Fiber tree, which is only possible in a development environment.
- Get Fiber Node: Using the selected DOM
Element, thegetFiberFromHostInstancefunction retrieves the associated React Fiber node. - Walk the Stack: The
getOwnerStackfunction is then called with the Fiber node. It walks up the "owner" chain of the Fiber tree to build a stack trace of the components that rendered the selected element.
This process provides a direct link from a visual element on the page to the component hierarchy in the code.
Sources: packages/react-grab/src/context.ts:51-55
Component and Source Location Identification
The raw stack trace from the Fiber tree contains many internal and irrelevant frames. react-grab filters this stack to find meaningful, user-defined components.
The following diagram shows the filtering logic applied to each stack frame.
Key functions in this process:
checkIsSourceComponentName(name): Determines if a component name is likely a user-defined source component based on capitalization and by filtering out known patterns likePrimitive.*or...ContextProvider.checkIsInternalComponentName(name): Filters out internal components from frameworks like Next.js (e.g.,InnerLayoutRouter,ErrorBoundary).
For each valid frame, the system extracts the component's functionName, fileName, lineNumber, and columnNumber.
Sources: packages/react-grab/src/context.ts:25-48
HTML Snippet Generation
Alongside the React component stack, a concise HTML preview of the selected element is generated by getHTMLPreview. This function:
- Captures the element's
tagName. - Lists its attributes, truncating long values (e.g.,
value.slice(0, 20)). - Provides a summary of child nodes, showing the first few children or a count (e.g.,
(5 elements)). - Includes the inner text content, truncated if it's too long.
This gives the AI agent a clear picture of the element's DOM structure and content.
Sources: packages/react-grab/src/context.ts:121-179
Final Context Assembly
The getElementContext function assembles the HTML preview and the filtered component stack into a single, formatted string. This string is the final output that gets copied or sent to an agent.
An example of the final formatted context:
<span>React Grab</span>
in StreamDemo (at components/stream-demo.tsx:42:11)
Sources: packages/react-grab/src/context.ts:64-119, packages/website/app/blog/intro/page.tsx:250-262
Agent Integration
While the default behavior is to copy the context to the clipboard, react-grab can be extended to integrate directly with coding agents. This is marketed as "React Grab for Agents" or "Visual Edit".
This integration works through "agent providers"—small, separate packages (@react-grab/cursor, @react-grab/claude-code, etc.) that connect react-grab to a specific agent's CLI or API.
The flow is as follows:
- The user selects an element and enters a prompt in the
react-grabUI. - The
react-grabclient sends the prompt and the extracted context to a local server started by the agent provider package. - The local server invokes the corresponding agent's CLI (e.g.,
cursor-agentorclaude) with the context and prompt. - The agent edits the codebase directly.
- Status updates from the agent are streamed back to the browser and displayed in the
react-grabUI.
Sources: packages/react-grab/README.md, packages/website/app/blog/agent/page.tsx:325-356
This sequence diagram illustrates the agent integration architecture.
Summary
The Element Selection and Context Grabbing system is a powerful developer utility that bridges the gap between the visual representation of a web application and its underlying source code. By leveraging React's development-time introspection capabilities, it extracts a highly specific and actionable context for a given UI element. This context, combining a DOM snapshot with a clean component stack trace, enables AI coding agents to perform UI modifications with significantly improved speed and accuracy, ultimately creating a more fluid and efficient development workflow.