mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-11 10:43:20 +00:00
36 lines
767 B
TypeScript
36 lines
767 B
TypeScript
import { Component, createRef } from 'inferno';
|
|
|
|
type Props = {
|
|
onOutsideClick: () => void;
|
|
};
|
|
|
|
export class TrackOutsideClicks extends Component<Props> {
|
|
ref = createRef<HTMLDivElement>();
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
this.handleOutsideClick = this.handleOutsideClick.bind(this);
|
|
|
|
document.addEventListener('click', this.handleOutsideClick);
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
document.removeEventListener('click', this.handleOutsideClick);
|
|
}
|
|
|
|
handleOutsideClick(event: MouseEvent) {
|
|
if (!(event.target instanceof Node)) {
|
|
return;
|
|
}
|
|
|
|
if (this.ref.current && !this.ref.current.contains(event.target)) {
|
|
this.props.onOutsideClick();
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return <div ref={this.ref}>{this.props.children}</div>;
|
|
}
|
|
}
|