Skip to main content

Overview

The ReactGrabAPI interface provides programmatic control over React Grab. It’s returned by the init() function and includes methods to activate/deactivate React Grab, manage state, register plugins, and more.

Type Definition

Methods

activate()

Activates React Grab, allowing element selection.
Example:
Notes:
  • Only activates if React Grab is currently enabled
  • Clears any pending comment mode
  • Does nothing if already active

deactivate()

Deactivates React Grab, stopping element selection.
Example:
Notes:
  • Works even if currently in a copying state
  • Cleans up all active selections and overlays
  • Restores previously focused elements

toggle()

Toggles React Grab between active and inactive states.
Example:

comment()

Activates comment mode, which allows adding a text prompt to copied elements.
Example:
Notes:
  • If already in comment mode and active, deactivates React Grab
  • Sets pending comment mode flag if not already active

isActive()

Checks if React Grab is currently active.
Example:
Returns:
  • true if React Grab is active and element selection is enabled
  • false otherwise

isEnabled()

Checks if React Grab is enabled.
Example:
Returns:
  • true if React Grab is enabled
  • false if disabled
Notes:
  • Enabled state is separate from active state
  • When disabled, React Grab cannot be activated

setEnabled()

Enables or disables React Grab.
boolean
required
Whether to enable or disable React Grab
Example:
Notes:
  • Disabling React Grab also deactivates it if currently active
  • The enabled state persists in toolbar state

getToolbarState()

Gets the current toolbar state.
Returns:
Example:

setToolbarState()

Updates the toolbar state.
Partial<ToolbarState>
required
Partial toolbar state to update
Example:
Notes:
  • Only provided properties are updated
  • Changes are saved to localStorage
  • Triggers toolbar state change callbacks

onToolbarStateChange()

Subscribes to toolbar state changes.
(state: ToolbarState) => void
required
Function to call when toolbar state changes
Returns:
  • Unsubscribe function
Example:

dispose()

Cleans up all React Grab resources, event listeners, and state.
Example:
Notes:
  • Removes all event listeners
  • Clears all timers and intervals
  • Removes overlay elements from DOM
  • After calling dispose(), a new init() call will work

copyElement()

Programmatically copies one or more elements.
Element | Element[]
required
Element or array of elements to copy
Returns:
  • Promise<boolean> - Resolves to true if copy succeeded, false otherwise
Example:
Example - Multiple Elements:

getSource()

Gets source information for an element.
Element
required
The element to get source info for
Returns:
Example:
Notes:
  • Returns null if source information cannot be determined
  • Requires React DevTools fiber information to be available

getStackContext()

Gets the stack context for an element.
Element
required
The element to get stack context for
Returns:
  • Promise<string> - A formatted string with the element’s component stack
Example:

getState()

Gets the current React Grab state.
Returns:
Example:

setOptions()

Updates React Grab options after initialization.
SettableOptions
required
Options to update. Same as Options but without the enabled property.
Example:
Notes:
  • Cannot change the enabled option - use setEnabled() instead
  • Changes take effect immediately
  • Affects all registered plugins

registerPlugin()

Registers a new plugin with React Grab.
Plugin
required
The plugin to register
Example:
Notes:
  • Plugins can add custom actions, hooks, and theme overrides
  • See Plugin API for details

unregisterPlugin()

Unregisters a plugin by name.
string
required
The name of the plugin to unregister
Example:
Notes:
  • Built-in plugins cannot be unregistered
  • Plugin cleanup methods are called automatically

getPlugins()

Gets a list of registered plugin names.
Returns:
  • string[] - Array of plugin names
Example:

getDisplayName()

Gets the display name (component name or tag name) for an element.
Element
required
The element to get the display name for
Returns:
  • Component name if available (e.g., 'Button', 'MyComponent')
  • Tag name if no component name found (e.g., 'div', 'button')
  • null if neither can be determined
Example:

Usage Example

Here’s a comprehensive example using multiple API methods: