ten1seven / what-input

A global utility for tracking the current input method (mouse/pointer, keyboard or touch).
https://ten1seven.github.io/what-input
MIT License
1.35k stars 89 forks source link

Fail gracefully in non-DOM environments #75

Closed dallonf closed 6 years ago

dallonf commented 6 years ago

As seen in #53, requiring what-input in a non-DOM environment like Node (ex. for tests or server-side rendering) causes a "ReferenceError: document is not defined". The OP of that issue worked around the issue by setting up a JSDOM environment, but this approach is very expensive and isn't always tenable, especially in test runners like Jest that set up the environment from scratch for every test suite.

Here's an alternate strategy for making this work. The idea is to bail out if the document or window objects aren't defined, and instead return a mock object that behaves as if the user never did and never will interact with the page.

ten1seven commented 6 years ago

This looks like a great solution, thanks @dallonf! I'll test and get a new version published shortly.

ten1seven commented 6 years ago

Okay, this is now published as v5.0.5.

dallonf commented 6 years ago

Wow, that was quick! Thank you 🎉