Skip to content

Type Editor


Type Editor / @type-editor/selection-util / caret-from-point

caret-from-point

Functions

FunctionDescription

caretFromPoint

Gets the caret position from a point in the document.

This function tries browser-specific methods to determine the DOM position (node and offset) at the given screen coordinates. It handles both Firefox's caretPositionFromPoint and Chrome/Safari's caretRangeFromPoint.

The offset is clipped to the node size to handle edge cases where browsers might return invalid offsets (e.g., text offsets into <input> nodes).

Example

typescript
const position = caretFromPoint(document, event.clientX, event.clientY);
if (position) {
  console.log("Caret is at:", position.node, position.offset);
}