p5.js-web-editor/client/utils/custom-hooks.js

34 lines
761 B
JavaScript
Raw Normal View History

2020-07-21 00:08:03 +02:00
import React, { useEffect, useRef } from 'react';
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
export const useHideOnBlur = (hideOverlay) => {
const ref = useRef({});
const setRef = (r) => { ref.current = r; };
const handleClickOutside = ({ target }) => {
if (ref && ref.current && !ref.current.contains(target)) {
hideOverlay();
}
};
useEffect(() => {
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, [ref]);
return setRef;
};