fn

useTextSelection

v0.0.14testeddemo

Reactively track the user's text selection via Window.getSelection.

Example

ts
const { text, rects, ranges, selection } = useTextSelection();
watch(text, (value) => console.log('selected:', value));

Demo

Loading demo…

Signature

ts
export function useTextSelection(
  options: UseTextSelectionOptions ={ ... }

Parameters

ParameterTypeDescription
options?UseTextSelectionOptionsOptions (custom window, document)

Returns

UseTextSelectionReturnReactive text, rects, ranges, and the raw selection
PropertyTypeDescription
textComputedRef<string>The selected text, equivalent to Selection.toString().
rectsComputedRef<DOMRect[]>Bounding rectangles for every range in the current selection.
rangesComputedRef<Range[]>The Range objects that make up the current selection.
selectionShallowRef<Selection | null>The raw Selection object, or null when nothing is selected / unsupported.