Files
outline/app/hooks/useTextSelection.ts
2022-11-13 14:47:20 -05:00

24 lines
511 B
TypeScript

import * as React 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] = React.useState<string>("");
useEventListener(
"selectionchange",
() => {
const selection = window.getSelection();
const text = selection?.toString();
setSelection(text ?? "");
},
document
);
return selection;
}