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

Create useClickedOutside hook #128

Open
leroykorterink opened this issue Apr 5, 2023 · 1 comment
Open

Create useClickedOutside hook #128

leroykorterink opened this issue Apr 5, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@leroykorterink
Copy link
Collaborator

leroykorterink commented Apr 5, 2023

We should have a util that makes it easy to detect clicks outside an element. The hook will most likely be used in modal components.

The hook should work with elements from state and RefObjects, a callback function should be called when the element or any of it's descendants are not clicked. The hook shouldn't have any additional properties.

Function signature

function useOutsideClick<T extends Unreffable<T>>(
  target: T,
  callback: (this: Unref<T>, event: MouseEvent) => void
): void;

Portal example

import { useOutsideClick } from '@mediamonks/react-hooks';
import styles from './MyComponent.module.scss';

export type MyComponentProps = {
  onClose(): void;
}

export const MyComponent = ensuredForwardRef<MyComponentProps, HTMLDivElement>((props, ref): ReactNode => {
  useOutsideClick(ref, () => {
    console.log("Element is not clicked");
  });

  return (
    <div ref={ref} className={styles.myComponent}>My content</div>,
  );
});
@leroykorterink leroykorterink added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Apr 5, 2023
@ThaNarie
Copy link
Member

ThaNarie commented Apr 5, 2023

The muban hook has some eventListener options, might be useful to have here as well?
https://mubanjs.github.io/muban-hooks/?path=/story/useclickedoutside-docs--page

React-use has options that define which types of interactions is should work on, might also be useful?
https://github.com/streamich/react-use/blob/master/docs/useClickAway.md

@leroykorterink leroykorterink changed the title Create useOutsideClick hook Create useOutsideInteraction hook Apr 5, 2023
@leroykorterink leroykorterink changed the title Create useOutsideInteraction hook Create useClickedOutside hook May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants