Files
outline/app/hooks/useTextSelection.ts
mehmet turac 1d919cc56a fix: initialize text selection state (#12366)
* fix: initialize text selection state

* Delete app/hooks/useTextSelection.test.ts

---------

Co-authored-by: Tom Moor <tom.moor@gmail.com>
2026-05-17 18:11:31 -04:00

31 lines
623 B
TypeScript

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