The div is actually a modal, I am using tailwind CSS with this
<div className={( displayModal ? 'block' : 'hidden' ) + ` w-screen h-screen absolute top-${hereIWantToPlaceXValueInPx} left=${hereIWantToPlaceYInPx} z-20 grid`}>
</div>
I want these pixel values
2
Answers
You need to capture the click event and then get the necessary coordinates from the event body, depending on your implementation, these are either
screenX
andscreenY
orclientY
andclientX
:You could store the mouse position in the state as an object and then use it when you return your
JSX
.The idea is simple, you’ll have a state named, for example,
mousePos
that is an object initialized as{x:0, y:0}
(or whatever suits you) then you listen for the click event on thewindow
to update your state which you can use to directly change the values in yourclassName
string.You might also create a custom hook that allows you to get the mouse position on any component you need, something like: