Files
outline/app/hooks/useTextSelection.ts
Tom Moor 084490ba6b chore: Remove React in scope requirement (#9261)
* Add rules

* codemod: update-react-imports

* Update babelrc
2025-05-20 19:26:11 -04:00

24 lines
507 B
TypeScript

import { useState } from "react";
import useEventListener from "./useEventListener";
/**
* A hook that returns the currently selected text.
*
* @returns The selected text
*/
export default function useTextSelection() {
const [selection, setSelection] = useState<string>("");
useEventListener(
"selectionchange",
() => {
const selection = window.getSelection();
const text = selection?.toString();
setSelection(text ?? "");
},
document
);
return selection;
}