Skip to content

Move ctrlBindings check into keyboardBindings function body to avoid global navigator reference #53

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

Merged
merged 3 commits into from
Jul 13, 2022

Conversation

iansan5653
Copy link
Member

@iansan5653 iansan5653 commented Jun 16, 2022

The reference to navigator at the top level makes combobox-nav unusable with SSR processes like React SSR. In particular, I am working on a component in PRC and cannot render the docs if combobox-nav is a dependency. For details, see the first list item in Gatsby's Debugging HTML Builds:

Some of your code references “browser globals” like window or document that aren’t available in Node.js. If this is your problem you should see an error above like “window is not defined”. To fix this, find the offending code and either a) check before calling the code if window is defined so the code doesn’t run while Gatsby is building (see code sample below) or b) if the code is in the render function of a React.js component, move that code into a componentDidMount lifecycle or into a useEffect hook, which ensures the code doesn’t run unless it’s in the browser.

When using this utility with React, it makes complete sense to construct the class inside a useEffect hook, so that part is already taken care of. However, because the reference to navigator is global, that doesn't fix the error.

We can easily fix this by just moving the navigator reference so it doesn't get called until (at minimum) the class gets constructed, so that's what I've done with this PR.

I considered putting the check inside the keyboardBindings function, but the performance impact of an additional regex check on every keyboard event felt unnecessarym, so I moved it into the class constructor and passed it as a parameter to keyboardBindings instead.

@iansan5653 iansan5653 self-assigned this Jun 16, 2022
@iansan5653 iansan5653 force-pushed the remove-global-navigator branch from ae17fc3 to 1edabde Compare June 16, 2022 19:56
@theinterned theinterned requested review from a team and theinterned June 29, 2022 18:09
Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach 👍

Just left a suggestion, but I think it's a debatable improvement (and I can see drawbacks of exposing something that previously was internal to the module) so take it or leave it.

@iansan5653 iansan5653 force-pushed the remove-global-navigator branch from 9f63938 to ae7984c Compare July 12, 2022 17:33
@iansan5653 iansan5653 requested a review from theinterned July 12, 2022 17:34
Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making that change! Looks great

@iansan5653
Copy link
Member Author

iansan5653 commented Jul 12, 2022

😕 I have no idea why the check is failing on MacOS

Edit: it was outdated dependencies

@iansan5653 iansan5653 merged commit b27b2e9 into main Jul 13, 2022
@iansan5653 iansan5653 deleted the remove-global-navigator branch July 13, 2022 15:24
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

Successfully merging this pull request may close these issues.

2 participants