UNPKG

@ozen-ui/kit

Version:

React component library

28 lines (27 loc) 997 B
import { useEffect } from 'react'; import { usePortalManager } from '../Portal'; export function useModalManager(ref, key, open) { var _a; var _b = usePortalManager(), portals = _b.portals, add = _b.add, remove = _b.remove; var index = 0; portals.forEach(function (portal, i) { if (portal.ref === ref) { index = i; } }); var refsClickOutside = index > -1 ? portals.slice(index + 1).map(function (_a) { var ref = _a.ref; return ref; }) : []; var filteredPortals = portals.filter(function (portal) { return portal.key === key; }); var isTop = ((_a = filteredPortals[filteredPortals.length - 1]) === null || _a === void 0 ? void 0 : _a.ref) === ref; useEffect(function () { if (open) { add({ ref: ref, key: key }); } return function () { remove(ref); }; }, [open, key]); return { portals: portals, refsClickOutside: refsClickOutside, isTop: isTop }; }