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

Switch from global to globalThis #63

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Here are the parameters that you can use. (\* = optional)
| :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `eventName` | The event name (string). Here is a list of [common events](https://developer.mozilla.org/en-US/docs/Web/Events). |
| `handler` | A function that will be called whenever `eventName` fires on `element`. |
| `element`\* | An optional element to listen on. Defaults to `global` (i.e., `window`). |
| `element`\* | An optional element to listen on. Defaults to `globalThis` (i.e., `window`). |
| `options`\* | An object `{ capture?: boolean, passive?: boolean, once?: boolean }` to be passed to `addEventListener`. For advanced use cases. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) for details. |

### Return
Expand Down
6 changes: 3 additions & 3 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ describe('useEventListener', () => {
addEventListenerSpy.mockRestore();
});

test.skip('`element` is optional (defaults to `window`/`global`)', () => {
test.skip('`element` is optional (defaults to `window`/`globalThis`)', () => {
const handler = jest.fn();
const addEventListenerSpy = jest.spyOn(global, 'addEventListener');
const addEventListenerSpy = jest.spyOn(globalThis, 'addEventListener');

renderHook(() => useEventListener('foo', handler));

Expand All @@ -54,7 +54,7 @@ describe('useEventListener', () => {

test.skip('does not add event listener to `window` if `element` is `null`', () => {
const handler = jest.fn();
const addEventListenerSpy = jest.spyOn(global, 'addEventListener');
const addEventListenerSpy = jest.spyOn(globalThis, 'addEventListener');

renderHook(() => useEventListener('foo', handler, null));

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRef, useEffect } from 'react';
const useEventListener = (
eventName,
handler,
element = global,
element = globalThis,
options = {}
) => {
const savedHandler = useRef();
Expand Down