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

How do I set window with useMouse() ? #203

Open
s7tya opened this issue Apr 19, 2022 · 5 comments
Open

How do I set window with useMouse() ? #203

s7tya opened this issue Apr 19, 2022 · 5 comments

Comments

@s7tya
Copy link

s7tya commented Apr 19, 2022

Sorry for creating an issue that is not a bug report or feature request.

How do i set window as a target of useMouse() ?
As I read the README, it looks like I can do that, but I couldn't find the way to do so.

| target | <code>React.RefObject&lt;T&gt; &#124; T &#124; null</code> | Yes | The React ref, `window`, or HTML element to add the event listener to |

@jaredLunde
Copy link
Owner

you can but maybe the type is incorrect. would happily accept a PR that fixes it.

@s7tya
Copy link
Author

s7tya commented Apr 24, 2022

I think the type is not the only thing to be fixed. Like we cannot use window obj in SSR?

( I’m a beginner so it might be wrong tho🙇)

@williamstein
Copy link

Using

  const mouse = useMouse(window as any);

fails. Typescript works fine, but there is a runtime stack trace.

Uncaught TypeError: element.getBoundingClientRect is not a function
    at _ref (index.js?64c7:27:1)
    at _ref2 (index.js?64c7:71:1)
    at updateReducer (react-dom.development.js?fc10:16664:1)
    at Object.useReducer (react-dom.development.js?fc10:17898:1)
    at Object.useReducer (react.development.js?443b:1626:1)
    at useMouse (index.js?64c7:142:1)

This is using the latest version of this library in the newest version of Chrome with recent react 18.

image

@williamstein
Copy link

My guess is that the solution to this issue is:

  • The place in the docs where it says that window is allowed as input should be removed, since window definitely does not work, since it doesn't have getBoundingClientRect, but this library assumes the input has that method.
  • Mention in the docs that if you need to listen for events on the entire window, use
const mouse = useMouse(document.body);

which I think does work as one would expect with the window. This is the case of an HTMLElement type input, which works fine.

@jaredLunde
Copy link
Owner

So the types are correct

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

3 participants