fn
useTextSelection
v0.0.14testeddemoReactively 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
| Parameter | Type | Description |
|---|---|---|
options? | UseTextSelectionOptions | Options (custom window, document) |
Returns
UseTextSelectionReturnReactive text, rects, ranges, and the raw selection| Property | Type | Description |
|---|---|---|
text | ComputedRef<string> | The selected text, equivalent to Selection.toString(). |
rects | ComputedRef<DOMRect[]> | Bounding rectangles for every range in the current selection. |
ranges | ComputedRef<Range[]> | The Range objects that make up the current selection. |
selection | ShallowRef<Selection | null> | The raw Selection object, or null when nothing is selected / unsupported. |