-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove useEventListener function overloads - Remove useDocument hook - Remove useWindow hook - Add useDocument hook - Add useWindow hook - Add RefObjectOption to useEventListener for ref support #101 #102
- Loading branch information
1 parent
2f6f3da
commit 9c1957f
Showing
26 changed files
with
286 additions
and
1,889 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
<Meta title="hooks/useDocument" /> | ||
|
||
# useDocument | ||
|
||
This hook returns the Document instance when it's availbable in the environment. If the Document is | ||
not available, it will return `undefined`. | ||
|
||
## Reference | ||
|
||
```ts | ||
function useDocument(): Document | undefined; | ||
``` | ||
|
||
## Usage | ||
|
||
```tsx | ||
function DemoComponent(): ReactNode { | ||
const document = useDocument(ref, ':focus-within'); | ||
|
||
useEventListener(document, 'focusin', (event) => { | ||
// do something | ||
}); | ||
|
||
return null; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useDocument } from './useDocument.js'; | ||
|
||
describe('useEventListener', () => { | ||
it('Should return the document', () => { | ||
const result = renderHook(useDocument); | ||
|
||
expect(result.result.current).toBeInstanceOf(Document); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns undefined when in a server environment, otherwise returns document. | ||
* | ||
* Note: be careful with this hook as it returns a different value on on server | ||
* and client. | ||
*/ | ||
export function useDocument(): Document | undefined { | ||
return typeof document === 'undefined' ? undefined : document; | ||
} |
26 changes: 0 additions & 26 deletions
26
src/hooks/useDocumentEventListener/useDocumentEventListener.stories.mdx
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
src/hooks/useDocumentEventListener/useDocumentEventListener.stories.tsx
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
src/hooks/useDocumentEventListener/useDocumentEventListener.test.tsx
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/hooks/useDocumentEventListener/useDocumentEventListener.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,48 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
/* eslint-disable react/no-multi-comp */ | ||
import { jest } from '@jest/globals'; | ||
import { render } from '@testing-library/react'; | ||
import { createRef, useEffect, useState, type ReactElement } from 'react'; | ||
import { useEventListener } from './useEventListener.js'; | ||
|
||
describe('useEventListener', () => { | ||
it('should not crash', () => { | ||
renderHook( | ||
() => { | ||
useEventListener(typeof document === 'undefined' ? undefined : document, 'focusin', () => { | ||
// eslint-disable-next-line no-console | ||
console.log(document.activeElement); | ||
}); | ||
}, | ||
{ | ||
initialProps: undefined, | ||
}, | ||
); | ||
it('Should listen to event attached to element from RefObject', () => { | ||
const spy = jest.fn(); | ||
const ref = createRef<HTMLDivElement>(); | ||
|
||
function Test(): ReactElement { | ||
useEventListener(ref, 'click', spy); | ||
|
||
return <div ref={ref} />; | ||
} | ||
|
||
render(<Test />); | ||
|
||
ref.current?.click(); | ||
|
||
expect(spy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('Should listen to event attached to element from state', async () => { | ||
const spy = jest.fn(); | ||
let exposedRef: HTMLDivElement | null = null; | ||
|
||
function Test(): ReactElement { | ||
const [ref, setRef] = useState<HTMLDivElement | null>(null); | ||
|
||
useEffect(() => { | ||
exposedRef = ref; | ||
}, [ref]); | ||
|
||
useEventListener(ref, 'click', spy); | ||
|
||
return <div ref={setRef} />; | ||
} | ||
|
||
render(<Test />); | ||
|
||
// @ts-expect-error typescript doesn't infer type for exposedRef correctly | ||
exposedRef?.click(); | ||
|
||
expect(spy).toBeCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.