text-selection
Track the document text selection.
| Package | @vizejs/composable/text-selection |
| Own the source | vize lib pull composable:text-selection |
| Runtime exports | useTextSelection |
| Gzip budget | 1536 B |
Usage
import { useTextSelection } from "@vizejs/composable/text-selection";
Runtime contract
| Utility | Category | Stability | SSR | Hydration | Cleanup | Targets | Host globals | Uses |
|---|---|---|---|---|---|---|---|---|
useTextSelection |
dom | experimental | deterministic-fallback | stable | reactive-scope | web, desktop | document |
tryOnScopeDispose |
API
useTextSelection
Track the document text selection. Re-reads the selection on every selectionchange. Because the platform mutates the same Selection object in place, a revision counter is used to invalidate the derived text, ranges, and rects. Server renders expose an empty selection; the listener is removed with the owning scope. Inside a component the host is read after mounting, so hydration renders the server fallback first and never mismatches.
function useTextSelection(options: UseTextSelectionOptions = {}): TextSelectionControls
Types
TextSelectionHost
Document-like capability observed by useTextSelection.
| Member | Type | Description |
|---|---|---|
getSelection |
() => Selection | null |
Current selection. |
UseTextSelectionOptions
Options for useTextSelection.
| Member | Type | Description |
|---|---|---|
host? |
MaybeRefOrGetter<TextSelectionHost | null | undefined> |
Reactive document capability for alternate runtimes and tests. |
TextSelectionControls
Reactive selection state returned by useTextSelection.
| Member | Type | Description |
|---|---|---|
selection |
Readonly<ShallowRef<Selection | null>> |
Current Selection object; null during server rendering. |
text |
ComputedRef<string> |
Selected text. Empty during server rendering. |
ranges |
ComputedRef<readonly Range[]> |
Selected ranges. |
rects |
ComputedRef<readonly DOMRect[]> |
Client rectangles of each selected range. |