2020-07-30 19:30:45 +02:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2020-07-21 00:08:03 +02:00
|
|
|
|
|
|
|
export const noop = () => {};
|
|
|
|
|
|
|
|
export const useDidUpdate = (callback, deps) => {
|
|
|
|
const hasMount = useRef(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (hasMount.current) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
hasMount.current = true;
|
|
|
|
}
|
|
|
|
}, deps);
|
|
|
|
};
|
2020-07-29 20:15:55 +02:00
|
|
|
|
2020-07-29 21:01:40 +02:00
|
|
|
// Usage: const ref = useModalBehavior(() => setSomeState(false))
|
|
|
|
// place this ref on a component
|
|
|
|
export const useModalBehavior = (hideOverlay) => {
|
2020-07-29 20:15:55 +02:00
|
|
|
const ref = useRef({});
|
2020-07-29 21:01:40 +02:00
|
|
|
|
|
|
|
// Return values
|
2020-07-29 20:15:55 +02:00
|
|
|
const setRef = (r) => { ref.current = r; };
|
2020-08-03 19:48:10 +02:00
|
|
|
const [visible, setVisible] = useState(false);
|
2020-07-30 21:16:47 +02:00
|
|
|
const trigger = () => setVisible(!visible);
|
2020-07-29 21:01:40 +02:00
|
|
|
|
|
|
|
const hide = () => setVisible(false);
|
|
|
|
|
2020-07-29 20:15:55 +02:00
|
|
|
|
|
|
|
const handleClickOutside = ({ target }) => {
|
|
|
|
if (ref && ref.current && !ref.current.contains(target)) {
|
2020-07-29 21:01:40 +02:00
|
|
|
hide();
|
2020-07-29 20:15:55 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
|
|
return () => document.removeEventListener('mousedown', handleClickOutside);
|
|
|
|
}, [ref]);
|
|
|
|
|
2020-07-29 21:01:40 +02:00
|
|
|
return [visible, trigger, setRef];
|
|
|
|
};
|