utils.ts
737 Bytes
import { useEffect, useId } from 'react';
export function cx(...values: Array<string | false | null | undefined>) {
return values.filter(Boolean).join(' ');
}
export function notifyPopupOpen(eventName: string, id: string) {
window.dispatchEvent(new CustomEvent(eventName, { detail: id }));
}
export function useExclusivePopup(eventName: string, setOpen: (open: boolean) => void) {
const id = useId();
useEffect(() => {
const closeOtherPopup = (event: Event) => {
if ((event as CustomEvent<string>).detail !== id) setOpen(false);
};
window.addEventListener(eventName, closeOtherPopup);
return () => window.removeEventListener(eventName, closeOtherPopup);
}, [eventName, id, setOpen]);
return id;
}