Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:React Grab is a developer tool designed to bridge the gap between a web application's UI and AI coding agents. Its primary purpose is to inspect React applications in a development environment, extract contextual information about UI elements—such as component names, file paths, and line numbers—and provide this context to coding agents. This process significantly improves the speed and accuracy of agents performing UI-related tasks by eliminating the need for them to search the codebase.
The project is architected as a monorepo containing several distinct packages. The main components include the core react-grab library responsible for context extraction, a CLI for project installation and configuration, and a series of provider-* packages that facilitate direct integration with various coding agents. This modular structure allows for extensibility and clear separation of concerns between the core inspection logic and the agent-specific communication protocols.
Monorepo Structure
The project is organized as a pnpm monorepo, which separates different functionalities into their own packages. This structure is defined in CONTRIBUTING.md and facilitates modular development and maintenance.
The diagram below illustrates the high-level relationship between the key packages in the monorepo.
Sources: CONTRIBUTING.md
Key Packages
The repository is divided into several packages, each with a specific role:
| Package | Description | Source -
| react-grab | The core library that performs the element inspection and context extraction. | CONTRIBUTING.md:19-28 -
| grab | An alias package for react-grab providing a shorter name. It's built by copying and transforming react-grab's assets. | packages/grab/scripts/build.js:1-9 -
| cli | The @react-grab/cli installer, which automates setup in user projects. | CONTRIBUTING.md:20 -
| provider-* | A collection of packages for integrating with specific coding agents (e.g., provider-cursor, provider-claude-code). | CONTRIBUTING.md:21-27 -
| website | The documentation and demo site (react-grab.com). | CONTRIBUTING.md:28 -
| *-playground | Development and testing environments for Vite and Next.js. | CONTRIBUTING.md:29-31 -
| benchmarks | Performance benchmarks for testing the tool's impact on agent speed. | CONTRIBUTING.md:32 -
Sources: CONTRIBUTING.md:18-33
Core Functionality: Context Extraction
The core feature of React Grab is its ability to extract contextual information from a clicked DOM element. This process is handled by the react-grab package and relies on traversing the React Fiber tree.
The workflow for context extraction is as follows:
- When a user selects an element,
getFiberFromHostInstanceis used to get the associated React Fiber node. - The
getOwnerStackfunction from thebippylibrary then walks up the Fiber tree from this starting node. - During the traversal, it collects a stack of frames, each containing component names (
functionName) and source locations (fileName,lineNumber,columnNumber), if available in the development environment. - The
getElementContextfunction formats this stack trace, filtering out internal and non-source components, and combines it with a preview of the element's HTML.
This diagram illustrates the context extraction flow:
Key Functions
getStack(element): Retrieves the owner-based component stack for a given DOM element by usingbippy'sgetOwnerStack.getElementContext(element, options): The main function that orchestrates the process. It callsgetStackandgetHTMLPreviewto construct the final context string, filtering out irrelevant internal components likeInnerLayoutRouterorErrorBoundary.getHTMLPreview(element): Generates a concise HTML representation of the selected element, including its tag, key attributes, and a snippet of its text content.
Sources: packages/react-grab/src/context.ts:54-106
Installation and Setup (@react-grab/cli)
React Grab provides a CLI tool, @react-grab/cli, to simplify its integration into various projects. The init command is the main entry point for this process.
The CLI automates the following steps:
- Project Detection: It identifies the project's framework (Next.js, Vite, Webpack), package manager, and, for Next.js, the router type (App or Pages).
- Interactive Prompts: It interactively asks the user which, if any, agent integration they want to add.
- Code Transformation: It modifies the necessary project files (e.g.,
layout.tsx,index.html,package.json) to add thereact-grabscript and configure agent providers. - Package Installation: It installs the required
react-grabandprovider-*packages using the detected package manager.
The following sequence diagram shows the npx grab@latest init workflow:
Sources: packages/cli/src/commands/init.ts
Agent Integration Architecture
A key feature of React Grab is its ability to send the extracted context directly to a coding agent, enabling a seamless "visual edit" workflow. This is achieved through a client-server architecture where a small local server acts as a bridge between the browser and the agent's CLI.
The process is as follows:
- Client-Side: The
react-grabscript, running in the browser, captures the element context and the user's prompt. It then sends this payload via an HTTP request to a local server. - Server-Side: A provider-specific server (e.g., from
@react-grab/cursor) running on a designated port receives the request. - Agent Execution: The server invokes the corresponding agent's command-line tool (e.g.,
cursor-agent), passing the context and prompt as arguments. - Feedback Loop: The server streams status updates from the agent's process back to the client in the browser, which displays the progress to the user.
This diagram shows the agent integration data flow:
Supported Agents and Providers
Each agent integration is handled by a dedicated package that runs a local server on a specific port.
| Agent | Provider Package | Default Port |
|---|---|---|
| Claude Code | @react-grab/claude-code | 4567 |
| Cursor | @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 |
| Visual Edit | @react-grab/visual-edit | N/A |
Sources: README.md, packages/cli/src/commands/init.ts:110-117, packages/website/app/blog/agent/page.tsx:200-418
Package Aliasing: grab vs. react-grab
To provide a more convenient and memorable package name, the project publishes an alias package named grab on NPM. The grab package is functionally identical to the react-grab package.
This is accomplished via a build script located at packages/grab/scripts/build.js. The script performs the following actions:
- Copies
dist: It copies the entiredistdirectory fromreact-grabtograb. - Transforms
README.md: It reads the rootREADME.md, replaces all occurrences of "react-grab" with "grab", and saves it aspackages/grab/README.md. - Syncs
package.json: It synchronizes metadata fields likeversion,description,keywords, andlicensefromreact-grab/package.jsontograb/package.json.
This ensures that users can npm install grab and get the exact same functionality as npm install react-grab.
Sources: packages/grab/scripts/build.js, packages/grab/README.md
Conclusion
The architecture of react-grab is thoughtfully designed to be modular, extensible, and easy to use. Its core strength lies in the deep integration with React's internals to provide precise context, which is then funneled through a decoupled provider system to enhance existing AI coding agents. The use of a monorepo structure simplifies dependency management and development across its various components, from the core library and CLI to the agent integrations and website. The CLI installer further abstracts the setup complexity, making the tool accessible to developers across different React-based frameworks.