selection-kit
v0.0.1
Published
Framework-agnostic text selection and range helpers.
Maintainers
Readme
Selection Kit
Framework-agnostic text selection and range helpers.
selection-kit provides pure text-selection primitives for editors, search fields, and annotation tools. It represents a selection with an anchor and focus so backward selections remain observable, while ordered ranges are available when applying text operations.
import { createSelection, replaceSelection, selectedText } from "selection-kit";
const selection = createSelection(8, 3);
selectedText("A small editor", selection); // "mall "
replaceSelection("A small editor", selection, "quiet").text;API
| Export | Purpose |
| --- | --- |
| createSelection(anchor, focus?) | Creates a caret or directional selection. |
| selectionRange(selection) | Returns an ordered { start, end } range. |
| isCollapsed(selection) / isBackward(selection) | Inspects selection direction. |
| clampSelection(selection, length) | Keeps offsets within text bounds. |
| collapse(selection, edge?) | Collapses to the start or end. |
| selectedText(text, selection) | Extracts selected text. |
| replaceSelection(text, selection, replacement) | Replaces text and returns the next caret. |
Offsets are UTF-16 string offsets, matching JavaScript's String#slice behavior. The package does not attach selections to a DOM or editor instance.
Development
npm run build --workspace=selection-kitMIT.
