Overview
Theinit() function is the primary entry point for React Grab. It initializes the library, sets up event listeners, and returns an API instance that you can use to control React Grab programmatically.
Signature
Parameters
Configuration options for React Grab
Return Value
Returns an API instance with methods to control React Grab programmatically. See ReactGrabAPI for details.
Usage
Basic Initialization
With Custom Options
With Custom Activation Key
With Custom Content Generator
Disabling React Grab
Behavior
- Single Initialization: The function can only be called once successfully. Subsequent calls return a no-op API.
- Server-Side Rendering: When called in a non-browser environment (e.g., during SSR), returns a no-op API.
- Built-in Plugins: Automatically registers built-in plugins (copy, comment, copyHtml, copyStyles, open).
- Event Listeners: Sets up keyboard, mouse, and touch event listeners for element selection.
- Cleanup: The returned API includes a
dispose()method to clean up all listeners and resources.
Notes
- The
enabledoption cannot be changed viasetOptions()after initialization. UsesetEnabled()on the API instead. - Options can be provided via a
<script>tag withdata-react-grab-optionsattribute, which will be merged with the options passed toinit(). - The activation key defaults to Cmd+C on Mac and Ctrl+C on Windows/Linux.
