Documentation
Hooks
useSelection

useSelection

Get the current selected text with its position on the window or a target element.

Select anything.

A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection and the focus is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button, and the focus is placed where you released the mouse button.

Type Definitions

const useSelection: <T extends HTMLElement = HTMLDivElement>(ref?: React.RefObject<T> | null) => {
    isSelecting: boolean;
    ref: React.RefObject<T>;
    text: string;
    rect: DOMRect | undefined;
};