ethanselzer / react-cursor-position

A React component that decorates its children with mouse and touch coordinates relative to itself.
https://ethanselzer.github.io/react-cursor-position
MIT License
143 stars 35 forks source link

Is there anyway to clear the position information? #2

Closed junyuanz123 closed 7 years ago

junyuanz123 commented 7 years ago

Hi,

This tool works perfectly for our project. However, I have a question, is that possible to reset the cursor information when it moves out the object?

Thanks!

ethanselzer commented 7 years ago

Hi,

Thanks for opening your issue. The feature you describe is not currently supported, but I would like to support it. To achieve the greatest utility from the implementation I would like to make it as general as possible. I propose child components and subscribers to onCursorPositionChanged receive a new Boolean property that would be set to true when the cursor is outside the bounds of the target element. Do you think that would address the use case you are inquiring about?

I'm very happy that react-cursor-position is working well for you!

junyuanz123 commented 7 years ago

That would be perfect! Thank you so much!

junyuanz123 commented 7 years ago

I will also study the code. I might be able to implement this.

ethanselzer commented 7 years ago

Okay great! I will get started on adding this feature. I think it will be ready in one to two days.

ethanselzer commented 7 years ago

The feature is ready! Child components and subscribers to onCursorPositionChanged will receive a new property named isOutside. Please see updated code release and npm package @ V1.2.

Thanks again for opening and issue. Please let me know if the new release addresses your use case.

junyuanz123 commented 7 years ago

Thank you so much!

junyuanz123 commented 7 years ago

Sorry to bother you again.

This code is working perfectly now. But I am just wondering Is there any way I can override the onMouseLeave function to make x, y equal to null?

Thanks!

ethanselzer commented 7 years ago

Please don't apologize. My goal is to make react-cursor-position useful to you. We are on the same team!

It looks like you closed the issue. Does that mean you found a solution?

I'm envisioning an enhancement to the API that would allow consumers to map the name and values of child component props (see below). Would that be helpful to you?

mapProps: (props) => {
  if (props.cursorPosition.isOutside) {
    return {
      cursorPosition: {
        x: null,
        y: null,
     };
  }
  return props;
}
junyuanz123 commented 7 years ago

Oh, thanks for the reply. I rethink about this question, and I think adding another function doesn't make any sense, so I close this issue.