this repo has no description
1import { useEffect } from 'preact/hooks';
2
3// NOTE: The order of initialized close watchers is important
4// Last one will intercept first if there are multiple/nested close watchers
5// So if this hook reruns, the previous close watcher will be destroyed, the new one will be created and the order will change
6function useCloseWatcher(fn, deps = []) {
7 if (!fn || typeof fn !== 'function') return;
8 useEffect(() => {
9 console.log('useCloseWatcher');
10 const watcher = new CloseWatcher();
11 watcher.addEventListener('close', fn);
12 return () => {
13 watcher.destroy();
14 };
15 }, deps);
16}
17
18export default window.CloseWatcher ? useCloseWatcher : () => {};