'use client' import React, { useEffect, useRef, useState } from 'react' import styled from 'styled-components' export const FloatMenu = () => { const useMouseAPI = () => { const [xPos, setXPos] = useState(0) const [yPos, setYPos] = useState(0) const [clickTs, setClickTs] = useState(0) const handlePositionChange = ({ screenX, screenY }) => { setXPos(screenX) setYPos(screenY) } const handleClick = () => { return setClickTs(new Date()) } useEffect(() => { document.addEventListener('mousemove', handlePositionChange) document.addEventListener('click', handleClick) return () => { document.removeEventListener('mousemove', handlePositionChange) document.removeEventListener('click', handleClick) } }, []) return { xPos, yPos, clickTs } } const [mouseLocation, setMouseLocation] = useState({ xPos: 0, yPos: 0 }) const { xPos, yPos, clickTs } = useMouseAPI() const prevClickTs = useRef(0) useEffect(() => { if (prevClickTs.current < clickTs) { setMouseLocation({ xPos, yPos }) prevClickTs.current = clickTs } }, [xPos, yPos, clickTs]) return (

{clickTs ? (

x-pos: {mouseLocation.xPos}

y-pos: {mouseLocation.yPos}

) : (

Click anywhere to start

)}

{!!clickTs && Hola}
) } const ChildWrapper = styled.div` position: absolute; background-color: red; width: 300px; height: 300px; `