Next.js Discord

Discord Forum

SelectionChangeEvent Handler

Unanswered
Atlantic mackerel posted this in #help-forum
Open in Discord
Avatar
Atlantic mackerelOP
hey guys, I wanna showing some menu (like notion) if selected text

But the below code doesn't work at all
    const [isSelected, setIsSelected] = useState<boolean>();

    useEffect(() => {
        document.addEventListener("selectionchange", () => {
            const activeSelection = document.getSelection();
            let selectedText = activeSelection?.toString();
            setIsSelected(selectedText?.length == 0);

            console.log("selectedText", selectedText?.length)
        })
    }, []);


how can I detect selectionchange on nextjs?

0 Replies