@replyke/ui-core-react-js
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
32 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
function useTextareaCursorIndicator({ textAreaRef, }) {
const [cursorPosition, setCursorPosition] = (0, react_1.useState)(0);
const [isSelectionActive, setIsSelectionActive] = (0, react_1.useState)(false);
const updateCursorAndSelection = (0, react_1.useCallback)(() => {
if (textAreaRef.current) {
const { selectionStart, selectionEnd } = textAreaRef.current;
setCursorPosition(selectionStart);
setIsSelectionActive(selectionStart !== selectionEnd);
}
}, [textAreaRef]);
(0, react_1.useEffect)(() => {
const textArea = textAreaRef.current;
if (textArea) {
textArea.addEventListener("input", updateCursorAndSelection);
textArea.addEventListener("click", updateCursorAndSelection);
textArea.addEventListener("keydown", updateCursorAndSelection);
textArea.addEventListener("keyup", updateCursorAndSelection);
return () => {
textArea.removeEventListener("input", updateCursorAndSelection);
textArea.removeEventListener("click", updateCursorAndSelection);
textArea.removeEventListener("keydown", updateCursorAndSelection);
textArea.removeEventListener("keyup", updateCursorAndSelection);
};
}
}, [updateCursorAndSelection, textAreaRef]);
return { textAreaRef, cursorPosition, isSelectionActive };
}
exports.default = useTextareaCursorIndicator;
//# sourceMappingURL=useTextareaCursorIndicator.js.map