* Add custom popup * Render popup window with react portal * Handle scroll * Handle pointer scroll * Refactor types * Add useOutsideClick * Restore button styles * Update types * Add tests * Set multiple refs * Cleanup * Cleanup * Move offset const to the utils * Add useCallback in useScroll * Remove empty lines in useElementPosition * Change styled components to functional * Make sure popup window stays in view * Highlight trigger button when popup is open * Add uesPopupWindow hook * Make triangle larger * Use full trigger height * Apply new lint rules Co-authored-by: Jaco Greeff <jacogr@gmail.com>
25 lines
819 B
TypeScript
25 lines
819 B
TypeScript
// Copyright 2017-2021 @polkadot/react-components authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import { RefObject, useCallback, useEffect } from 'react';
|
|
|
|
function getClickedElement (refs: React.RefObject<HTMLDivElement>[], e: MouseEvent) {
|
|
return refs.find((ref) => ref.current && ref.current.contains(e.target as HTMLElement));
|
|
}
|
|
|
|
export const useOutsideClick = (elements: RefObject<HTMLDivElement>[], callback: () => void): void => {
|
|
const handleClick = useCallback((e: MouseEvent) => {
|
|
if (elements.length && !getClickedElement(elements, e)) {
|
|
callback();
|
|
}
|
|
}, [elements, callback]);
|
|
|
|
useEffect(() => {
|
|
document.addEventListener('click', handleClick);
|
|
|
|
return () => {
|
|
document.removeEventListener('click', handleClick);
|
|
};
|
|
}, [handleClick, callback]);
|
|
};
|