Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hook useOnClickOutside #1006

Merged
merged 9 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ export * from './utils/useFileInput/useFileInput';
export {useActionHandlers} from './utils/useActionHandlers';
export {useUniqId} from './utils/useUniqId';
export {getLayersCount} from './utils/LayerManager';
export * from './utils/useOutsideClick';
32 changes: 32 additions & 0 deletions src/components/utils/useOutsideClick/__tests__/Demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React from 'react';

import {useOutsideClick} from '../useOutsideClick';

export const Demo = () => {
const observerRef = React.useRef(null);
const [status, setStatus] = React.useState<1 | 0>(0);

const handleOutsideClick = () => {
setStatus(0);
};

const handleClick = () => {
setStatus(1);
};

useOutsideClick({
ref: observerRef,
handler: handleOutsideClick,
});

return (
<div>
<h1>{status}</h1>
<div ref={observerRef} onClick={handleClick}>
{'Target'}
</div>
<div>{'Outside'}</div>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import {Demo} from './Demo';

test('Check useOutsideClick correct work', async () => {
render(<Demo />);

expect(screen.getByRole('heading')).toHaveTextContent('0');

await userEvent.click(screen.getByText('Target'));

expect(screen.getByRole('heading')).toHaveTextContent('1');

await userEvent.click(screen.getByText('Outside'));

expect(screen.getByRole('heading')).toHaveTextContent('0');
});
2 changes: 2 additions & 0 deletions src/components/utils/useOutsideClick/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {useOutsideClick} from './useOutsideClick';
export type {UseOutsideClickProps} from './useOutsideClick';
amje marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 36 additions & 0 deletions src/components/utils/useOutsideClick/useOutsideClick.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';

export interface UseOutsideClickProps<T> {
ref: React.RefObject<T>;
handler?: () => void;
}

type UseOutsideClickType = <K extends HTMLElement>(props: UseOutsideClickProps<K>) => void;

/**
* Hook for observing clicks outside a given target
*
* @param ref - purpose of observation
* @param handler - callback when a click is triggered outside the observation target
*
* @return - nothing
*/
export const useOutsideClick: UseOutsideClickType = ({ref, handler}) => {
React.useEffect(() => {
const callback = (e: MouseEvent | TouchEvent) => {
const elem = ref?.current;

if (elem && !elem.contains(e.target as Node) && handler) {
handler();
}
};

window.addEventListener('mouseup', callback, {capture: true});
window.addEventListener('touchend', callback, {capture: true});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be consistent here, mousedown and touchstart or mouseup and touchend


return () => {
window.removeEventListener('mouseup', callback, {capture: true});
window.removeEventListener('touchend', callback, {capture: true});
};
}, [handler, ref]);
};