mirror of
https://github.com/spacedriveapp/spacedrive
synced 2024-07-07 05:33:27 +00:00
c65d92ee4c
* beginnings of app directory * settings mostly good * colocate way more components * flatten components folder * reexport QueryClientProvider from client * move CodeBlock back to interface * colocate Explorer, KeyManager + more * goddamn captialisation * get toasts out of components * please eslint * no more src directory * $ instead of : * added back RowHeader component * fix settings modal padding * more spacing, less margin * fix sidebar locations button * fix tags sidebar link * clean up back button * added margin to explorer context menu to prevent contact with edge of viewport * don't export QueryClientProvider from @sd/client * basic guidelines * import interface correctly * remove old demo data * fix onboarding layout * fix onboarding navigation * fix key manager settings button --------- Co-authored-by: Jamie Pine <ijamespine@me.com>
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { useEffect } from 'react';
|
|
|
|
// Improved version of https://usehooks.com/useOnClickOutside/
|
|
const useClickOutside = (ref: any, handler: any) => {
|
|
useEffect(() => {
|
|
let startedInside = false;
|
|
let startedWhenMounted = false;
|
|
|
|
const listener = (event: any) => {
|
|
// Do nothing if `mousedown` or `touchstart` started inside ref element
|
|
if (startedInside || !startedWhenMounted) return;
|
|
// Do nothing if clicking ref's element or descendent elements
|
|
if (!ref.current || ref.current.contains(event.target)) return;
|
|
|
|
handler(event);
|
|
};
|
|
|
|
const validateEventStart = (event: any) => {
|
|
startedWhenMounted = ref.current;
|
|
startedInside = ref.current && ref.current.contains(event.target);
|
|
};
|
|
|
|
document.addEventListener('mousedown', validateEventStart);
|
|
document.addEventListener('touchstart', validateEventStart);
|
|
document.addEventListener('click', listener);
|
|
|
|
return () => {
|
|
document.removeEventListener('mousedown', validateEventStart);
|
|
document.removeEventListener('touchstart', validateEventStart);
|
|
document.removeEventListener('click', listener);
|
|
};
|
|
}, [ref, handler]);
|
|
};
|
|
|
|
export default useClickOutside;
|