Skip to content

Commit b4842c7

Browse files
authored
Feat/use document evt listener (#85)
* feat: add docs for the use document event listener * fix: docs for useWindowEventListener * feat: addition of changeset * feat: add once options to the event listener
1 parent 352667d commit b4842c7

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

.changeset/happy-buckets-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@abhushanaj/react-hooks': minor
3+
---
4+
5+
Addition of the useDocumentEventListener
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { useDocumentEventListener } from '.';
4+
5+
/**
6+
* Other test are not required since this is just a wrapper over _useEventListener which already has the necessary tests
7+
*/
8+
describe('useDocumentEventListener() hook', () => {
9+
it('should be defined', () => {
10+
expect.hasAssertions();
11+
expect(useDocumentEventListener).toBeDefined();
12+
});
13+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { UseEventListenerCallback, UseEventListenerEventMap, UseEventListenerOptions } from '../_useEventListener';
2+
3+
import { _useEventListener } from '../_useEventListener';
4+
import { useIsClient } from '../useIsClient';
5+
6+
/**
7+
* useDocumentEventListener() - Custom react hook to attch event listeners to document object.
8+
* @see - https://react-hooks.abhushan.dev/hooks/dom/usedocumenteventlistener/
9+
*/
10+
export function useDocumentEventListener(
11+
eventName: keyof UseEventListenerEventMap<Document>,
12+
callback: UseEventListenerCallback,
13+
options?: UseEventListenerOptions
14+
) {
15+
const client = useIsClient();
16+
17+
return _useEventListener(client ? document : (null as unknown as Document), eventName, callback, options);
18+
}

react-hooks/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export { useDocumentTitle } from './hooks/useDocumentTitle';
33
export type { DocumentTitleOptions, UseDocumentTitleOptions } from './hooks/useDocumentTitle';
44
export { useIsDocumentVisible } from './hooks/useIsDocumentVisible';
5+
export { useDocumentEventListener } from './hooks/useDocumentEventListener';
56

67
// ======= Window =======
78
export { useWindowSize } from './hooks/useWindowSize';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useDocumentEventListener } from '@abhushanaj/react-hooks';
2+
3+
function UseDocumentEventListenerExample() {
4+
useDocumentEventListener(
5+
'click',
6+
() => {
7+
alert('Document Clicked');
8+
},
9+
{
10+
once: true
11+
}
12+
);
13+
14+
return (
15+
<div className="flex flex-col gap-2">
16+
<small>Click anywhere on document to trigger an event (only once)</small>
17+
</div>
18+
);
19+
}
20+
21+
export default UseDocumentEventListenerExample;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: useDocumentEventListener
3+
description: 'Adds an event listener to the document object.'
4+
subtitle: 'Adds an event listener to the document object.'
5+
sidebar:
6+
badge: 'New'
7+
---
8+
9+
import Example from '@/components/demo/useDocumentEventListener';
10+
import { DemoWrapper } from '@/components/demo/wrapper';
11+
12+
The `useDocumentEventListener` hook is helpful when you want attach event listener to the document object and run callbacks after the event is triggered.
13+
14+
The event listener attached will automatically be cleanuped once the component is unmounted.
15+
16+
This hook is exactly to [useEventListenerOnRef](/hooks/ui/useeventlisteneronref/), but with [`document`](https://developer.mozilla.org/en-US/docs/Web/API/Document) object as target.
17+
18+
<DemoWrapper title="useDocumentEventListener">
19+
<Example client:load />
20+
</DemoWrapper>
21+
22+
## Usage
23+
24+
Import the hook from `@abhushanaj/react-hooks` and use in required component.
25+
26+
```tsx title="./src/App.tsx" ins={1,4-6}
27+
import { useDocumentEventListener } from '@abhushanaj/react-hooks';
28+
29+
function App() {
30+
useDocumentEventListener('click', () => {
31+
alert('Click event triggered');
32+
});
33+
34+
return <p>Click anywhere on document to trigger the event.</p>;
35+
}
36+
37+
export default App;
38+
```
39+
40+
## API Reference
41+
42+
### Parameters
43+
44+
| Parameter | Type | Description | Default |
45+
| --------- | ----------------------------------------------- | ---------------------------------------------------------------------- | ----------- |
46+
| eventName | `string` | The name of the event to listen to. | N/A |
47+
| callback | `EventListenerOrEventListenerObject` | The event handler function to be executed when the event is triggered. | N/A |
48+
| options | `AddEventListenerOptions \| boolean` (optional) | Additional options for the event listener. | `undefined` |
49+
50+
**Note:** The `EventListenerOrEventListenerObject` and `AddEventListenerOptions` are standard types defined by [`lib.dom.d.ts`](https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts). You can refer the [MDN spec](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters) for additional information.

www/src/content/docs/hooks/window/useWindowEventListener.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function App() {
3535
});
3636

3737
return (
38-
<div className="flex flex-col gap-2">
38+
<div>
3939
<p>Y Scroll : {yScroll}px</p>
4040
</div>
4141
);

0 commit comments

Comments
 (0)