useTextSelection
Track user text selection based on document.getSelection
Usage
Live Editor
function Demo() { const selection = useTextSelection(); return ( <div style={{ padding: 40 }}> <p> Select some text here or anywhere on the page and it will be displayed below </p> <div>Selected text: {selection?.toString()}</div> </div> ); };
Result
Error: Minified React error #185; visit https://react.dev/errors/185 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
API
useTextSelection
Returns
Selection | null
: selected text object