mirror of
https://github.com/outline/outline.git
synced 2025-12-30 07:19:52 -06:00
24 lines
511 B
TypeScript
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;
|
|
}
|