Skip to main content

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
Loading...

API

useTextSelection

Returns

Selection | null: selected text object

Arguments