Configuration
Relevant source files
The following files were used as context for generating this wiki page:Configuration for react-grab allows developers to tailor the tool's behavior, appearance, and integrations. Setup can be accomplished through an automated CLI, manual script injection, or programmatic initialization. The system is designed to be flexible, supporting basic usage out-of-the-box while also providing deep customization for advanced workflows, particularly with AI coding agent integrations.
The two primary areas of configuration are the core library's client-side behavior (activation keys, UI theme) and the integration with various backend coding agents (Claude Code, Cursor, etc.). These are managed through a combination of CLI commands, package.json scripts, and client-side script attributes or function calls.
Automated Setup (CLI)
The recommended way to install and configure react-grab is through its Command Line Interface (CLI). The init command automates the process of detecting the project's framework, installing necessary packages, and injecting the required code.
Sources: packages/cli/src/commands/init.ts, README.md
init Command
The init command guides the user through an interactive setup process. It can also be run non-interactively for automated environments.
Command:
npx grab@latest init
Options:
| Option | Alias | Description | Default |
|---|---|---|---|
--yes | -y | Skip confirmation prompts. | false |
--force | -f | Force overwrite of existing config. | false |
--agent <agent> | -a | Specify an agent integration to install. | none |
--skip-install | Skip package installation. | false | |
--cwd <cwd> | -c | Set the working directory. | process.cwd() |
Sources: packages/cli/src/commands/init.ts:133-149
CLI Workflow
The CLI performs a series of steps to configure the project, including preflight checks, framework detection, and applying transformations to project files.
The following diagram illustrates the workflow of the init command.
This diagram shows the decision-making process of the init command, from initial checks to final configuration.
Sources: packages/cli/src/commands/init.ts:151-689
Manual Installation & Configuration
For projects where the CLI is not suitable, react-grab can be installed manually. This typically involves adding a <script> tag to the main HTML file or importing the library in the main JavaScript entry point.
Next.js (App Router)
In a Next.js App Router project, add the Script component to app/layout.tsx.
// packages/website/components/install-tabs.tsx:90-109
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"
crossOrigin="anonymous"
strategy="beforeInteractive"
/>
)}
</head>
<body>{children}</body>
</html>
);
}
Sources: packages/website/components/install-tabs.tsx:90-109, README.md
Next.js (Pages Router)
For the Pages Router, add the Script component to pages/_document.tsx.
// packages/website/components/install-tabs.tsx:118-138
import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";
export default function Document() {
return (
<Html lang="en">
<Head>
{process.env.NODE_ENV === "development" && (
<Script
src="//unpkg.com/react-grab/dist/index.global.js"
crossOrigin="anonymous"
strategy="beforeInteractive"
/>
)}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Sources: packages/website/components/install-tabs.tsx:118-138, README.md
Vite
In a Vite project, modify index.html to import the library in development mode.
<!-- packages/website/components/install-tabs.tsx:156-170 -->
<!doctype html>
<html lang="en">
<head>
<script type="module">
// first npm i react-grab
// then in head:
if (import.meta.env.DEV) {
import("react-grab");
}
</script>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Sources: packages/website/components/install-tabs.tsx:156-170, README.md
Webpack
For Webpack, import react-grab at the top of your main entry file.
// packages/website/components/install-tabs.tsx:189-203
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
if (process.env.NODE_ENV === "development") {
import("react-grab");
}
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Sources: packages/website/components/install-tabs.tsx:189-203, README.md
Core Library Options
The core behavior of react-grab can be customized programmatically using the init function or declaratively via data-options on the script tag.
Sources: packages/react-grab/README.md, packages/cli/src/commands/init.ts
Programmatic Initialization
Import and call the init function from react-grab/core with a configuration object.
// packages/react-grab/README.md
import { init } from "react-grab/core";
const api = init({
theme: {
enabled: true, // disable all UI by setting to false
hue: 180, // shift colors by 180 degrees (pink → cyan/turquoise)
crosshair: {
enabled: false, // disable crosshair
},
elementLabel: {
enabled: false, // disable element label
},
},
onElementSelect: (element) => {
console.log("Selected:", element);
},
onCopySuccess: (elements, content) => {
console.log("Copied to clipboard:", content);
},
onStateChange: (state) => {
console.log("Active:", state.isActive);
},
});
Sources: packages/react-grab/README.md
Configuration Options
The following options can be configured. These are derived from the interactive prompts in the init CLI command.
| Option | Type | Description | Default |
|---|---|---|---|
activationKey | object | Defines the key combination to activate the tool. e.g., { altKey: true, key: 'g' } | { altKey: true } |
activationMode | 'toggle' | 'hold' | Sets whether the key toggles or must be held. | toggle |
keyHoldDuration | number | Duration in ms to hold the key in hold mode. | 150 |
allowActivationInsideInput | boolean | Whether activation is allowed inside input fields. | true |
maxContextLines | number | The maximum number of surrounding HTML lines to include. | 3 |
theme | object | Customizes the UI appearance (e.g., hue, enabled). | {} |
Sources: packages/cli/src/commands/init.ts:413-503, packages/react-grab/README.md
Agent Integrations
A key feature of react-grab is its ability to send context directly to AI coding agents. This streamlines the workflow by eliminating the need to copy and paste code context. Each integration typically requires a client-side script and a server-side process.
Sources: packages/website/app/blog/agent/page.tsx, README.md, CONTRIBUTING.md
Architecture
When an agent integration is active, react-grab communicates with a local server, which in turn interfaces with the agent's CLI or SDK to perform file edits.
This diagram shows the data flow from a user action in the browser to a file modification by an AI agent.
Sources: packages/website/app/blog/agent/page.tsx:511-530
Supported Agents
react-grab supports a variety of popular coding agents. The setup involves modifying package.json and adding a client script.
| Agent | 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: packages/website/app/blog/agent/page.tsx, CONTRIBUTING.md:21-29, packages/cli/src/commands/init.ts:100-108
Example: Claude Code Integration
-
Modify
package.json: Add the agent server to yourdevscript.// packages/website/app/blog/agent/page.tsx:265-271 { "scripts": { "dev": "npx @react-grab/claude-code@latest && next dev" } } -
Add Client Script: Include the agent's client script in your HTML
<head>.<!-- packages/website/app/blog/agent/page.tsx:281-282 --> <script src="//unpkg.com/react-grab/dist/index.global.js"></script> <script src="//unpkg.com/@react-grab/claude-code/dist/client.global.js"></script>
This pattern is consistent across most agent integrations, with different package names and ports.
Sources: packages/website/app/blog/agent/page.tsx:256-283, README.md