PHP Classes

File: src/hooks/useOutsideClick.tsx

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   WordPress React Plugin Kit   src/hooks/useOutsideClick.tsx   Download  
File: src/hooks/useOutsideClick.tsx
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: WordPress React Plugin Kit
Environment to develop new WordPress plugins
Author: By
Last change:
Date: 1 year ago
Size: 1,223 bytes
 

Contents

Class file image Download
/** * External dependencies */ import { useEffect } from '@wordpress/element'; export default function useOutsideClick( triggerRef: any, areaRef: any, callback: () => void ) { // Close on outside click. useEffect(() => { const clickHandler = (e: any) => { if (areaRef === null || areaRef?.current === null) { if (triggerRef.current.contains(e.target as Node)) { return; } } else if ( triggerRef?.current?.contains(e.target as Node) || areaRef?.current?.contains(e.target as Node) ) { return; } callback(); }; document.addEventListener('click', clickHandler); return () => document.removeEventListener('click', clickHandler); }, [triggerRef, areaRef, callback]); // Close on escape key press. useEffect(() => { const keyHandler = ({ keyCode }) => { if (keyCode !== 27) return; callback(); }; document.addEventListener('keydown', keyHandler); return () => document.removeEventListener('keydown', keyHandler); }); return null; }