-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hooks): Combine debounced hooks into single hook.
- Combine useDebouncedResize and useDebouncedScroll into useDebouncedEvent. - Support mousedown events now. - Update other hooks to use useDebouncedEvent.
- Loading branch information
Showing
8 changed files
with
55 additions
and
58 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
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,37 @@ | ||
import { useEffect } from 'react'; | ||
import { debounce } from 'es-toolkit/compat'; | ||
|
||
interface UseDebouncedEventOptions { | ||
events?: Array<'resize' | 'scroll' | 'mousedown'>; | ||
delay?: number; | ||
} | ||
|
||
const useDebouncedEvent = <T extends Event>( | ||
callback: (event: T) => void, | ||
{ events = ['scroll'], delay = 100 }: UseDebouncedEventOptions | ||
) => { | ||
useEffect(() => { | ||
if (typeof globalThis === 'undefined') return; // Skip in SSR | ||
|
||
// Use a debounced callback | ||
const debouncedCallback = debounce( | ||
(event: Event) => callback(event as T), | ||
delay | ||
); | ||
|
||
// Add event listeners | ||
for (const event of events) | ||
globalThis.addEventListener(event, debouncedCallback as EventListener); | ||
|
||
// Remove event listeners | ||
return () => { | ||
for (const event of events) | ||
globalThis.removeEventListener( | ||
event, | ||
debouncedCallback as EventListener | ||
); | ||
}; | ||
}, [callback, events, delay]); | ||
}; | ||
|
||
export default useDebouncedEvent; |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,19 @@ | ||
import { useEffect } from 'react'; | ||
import { debounce } from 'es-toolkit/compat'; | ||
import { useDebouncedEvent } from '@/hooks'; | ||
|
||
const useOutsideClick = ( | ||
reference: React.RefObject<HTMLElement | null>, | ||
callback: () => void | ||
) => { | ||
useEffect(() => { | ||
const handleClickOutside = debounce((event: MouseEvent) => { | ||
if ( | ||
reference.current && | ||
!reference.current.contains(event.target as Node) | ||
) { | ||
callback(); | ||
} | ||
}, 100); // 100ms debounce delay for click | ||
const handleClickOutside = (event: MouseEvent) => { | ||
if ( | ||
reference.current && | ||
!reference.current.contains(event.target as Node) | ||
) { | ||
callback(); | ||
} | ||
}; | ||
|
||
document.addEventListener('mousedown', handleClickOutside); | ||
return () => { | ||
handleClickOutside.cancel(); | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
}; | ||
}, [reference, callback]); | ||
useDebouncedEvent(handleClickOutside, { events: ['mousedown'] }); | ||
}; | ||
|
||
export default useOutsideClick; |
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
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