useTextSelection

Tags:

A React Hook that helps to get the selection state of the document.

Demo

Source
Velit sint non mollit cupidatat ipsum culpa esse?
Selected text:
(Nothing selected)

Usage

See API for more details.

Source

Click links below to view source on GitHub.

API

const { text, rects, ranges, selectionRef } = useTextSelection()

Returns

export interface UseTextSelectionReturns { /** * A React Ref object that holds the current Selection object. */ selectionRef: MutableRefObject<Selection | null> /** * The selected text. */ text: string /** * The DOMRect objects for each selected range. */ rects: DOMRect[] /** * The Range objects for each selected range. */ ranges: Range[] }