Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mouseEvent doesn't have offsetX/offsetY #4431

Closed
imevro opened this issue Jul 20, 2015 · 16 comments
Closed

mouseEvent doesn't have offsetX/offsetY #4431

imevro opened this issue Jul 20, 2015 · 16 comments

Comments

@imevro
Copy link

imevro commented Jul 20, 2015

I try get position of click relative to element, but event doesn't have offsetX.

onClick(e) {
  console.log(e.offsetX) // returns undefined
  console.log(e.target.offsetX) // returns undefined
}

render() {
  return <img src='http://placehold.it/1000x500' onClick={this.onClick} />
}

How I can get position of click in element?

@imevro imevro changed the title mouseEvent don't have offsetX/offsetY mouseEvent doesn't have offsetX/offsetY Jul 20, 2015
@imevro
Copy link
Author

imevro commented Jul 20, 2015

Oh, well, I see.
I get it from e.nativeEvent.offsetX. Is it right approach?

@waldreiter
Copy link
Contributor

This shows all available values:

onClick(event) {
  event.persist();
  console.log(event);
}

Using the nativeEvent is fine.

@imevro
Copy link
Author

imevro commented Jul 20, 2015

@cody thanks!

@quantizor
Copy link
Contributor

👍 Please close if you're happy with the answer and there is no found bug(s).

@imevro imevro closed this as completed Jul 20, 2015
@sophiebits
Copy link
Collaborator

You probably want pageX or clientX and if you want it relative to another element you can use getBoundingClientRect on that element – if you do that it should work well across browsers.

@glenjamin
Copy link
Contributor

offsetX/Y appear to be on the standards track and supported in all major browsers.

Any chance of adding them into React - perhaps with a getBoundingClientRect shim for browsers which don't support it?

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/offsetX

@sophiebits
Copy link
Collaborator

That seems reasonable enough if we can do it with no performance compromise so I'll reopen this, but it won't be a high priority for us.

@git-richard
Copy link

I did some work on this and it would require a polyfill. As a property in SyntheticMouseEvent it would execute for every mouse event, which would have a performance impact. It could be added as a method that executed on-demand but then it wouldn't match the native MouseEvent interface.

@syranide
Copy link
Contributor

#7011

@gaearon
Copy link
Collaborator

gaearon commented Jul 4, 2016

I’ll close since we decided to not merge #7011.

@gaearon gaearon closed this as completed Jul 4, 2016
@Jarred-Sumner
Copy link
Contributor

As of 2020, this doesn't seem to need a polyfill. But SyntheticEvent's MouseEvent doesn't have offsetX or offsetY

I can submit a PR if you want

https://caniuse.com/#feat=mdn-api_mouseevent_offsetx

image

@amerllica
Copy link

@gaearon, Why ReactJs mouseEvent doesn't have which, offsetX, offsetY and etc?

Why the Facebook developers add them into e.nativeEvent?

@mlajtos
Copy link

mlajtos commented Sep 4, 2022

Why the Facebook developers add them into e.nativeEvent?

@amerllica e.nativeEvent is created by the browser, so if there is e.nativeEvent.offsetX the browser supports it. Why the attribute does not propagate to synthetic event, that is question to React team.

@acenturyandabit
Copy link

Can we reopen this? offsetX would make it a lot easier to implemenet encapsulated event handlers.

@Patilritik
Copy link

Is there any solution offer by react , other then the getBoundingClientRect and e.nativeEvent for handling the mouse event ??

@TomasHubelbauer
Copy link

TomasHubelbauer commented Oct 2, 2024

This was closed in 2016. It's 2024 and offsetX/offsetY is baseline level of support:

@sebmarkbage you're the most active maintainer during the past month: https://github.com/facebook/react/graphs/contributors. @gaearon, who initially engaged in this thread and ultimately closed in the issue in 2016 is no longer working on React presumably since he moved to Bluesky. Can this be re-opened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests