// Detect and return currently selected text by user
const { text, isSelected, clearSelection } = useTextSelection();
Perfect for text annotation tools, highlighting features, selection-based actions, or any text interaction functionality.
Select any text in this paragraph to see the text selection hook in action. This is a longer paragraph with multiple sentences to demonstrate text selection capabilities. You can select individual words, sentences, or the entire paragraph.
Select some text above to see selection information
import { useTextSelection } from '@usegrand/react-hokss'; function TextAnnotator() { const { text, isSelected, range, rect, clearSelection, selectText } = useTextSelection(); const handleHighlight = () => { if (isSelected && text) { // Create annotation or highlight console.log('Highlighting:', text); } }; return ( <div> <p ref={textRef}> This is some text that can be selected for annotation. </p> {isSelected && ( <div> <p>Selected: "{text}"</p> <button onClick={handleHighlight}>Highlight</button> <button onClick={clearSelection}>Clear</button> </div> )} </div> ); }
Object containing selection state and control functions
Currently selected text string
Selection range with start and end positions
Boolean indicating if text is currently selected
Bounding rectangle of the selection
Function to clear the current selection
Function to programmatically select text