* Add document context to allow accessing editor in header, modals, and elsewhere * lint * framework * Hacking together fast * Insights * Spacing tweak, docs
23 lines
574 B
TypeScript
23 lines
574 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>("");
|
|
|
|
const handleMouse = React.useCallback(() => {
|
|
const selection = window.getSelection();
|
|
const text = selection?.toString();
|
|
setSelection(text ?? "");
|
|
}, []);
|
|
|
|
useEventListener("mousemove", handleMouse);
|
|
useEventListener("mouseup", handleMouse);
|
|
|
|
return selection;
|
|
}
|