@brizy/ui
Version:
React elements in Brizy style
27 lines (26 loc) • 868 B
JavaScript
const nodeStack = new Map();
const handleClick = (e) => {
nodeStack.forEach(nodeCb => {
nodeCb(e);
});
};
export const attachClick = (id, node, cb) => {
var _a;
const _window = (_a = node === null || node === void 0 ? void 0 : node.ownerDocument.defaultView) === null || _a === void 0 ? void 0 : _a.window;
if (_window) {
if (nodeStack.size === 0) {
_window.addEventListener("click", handleClick);
}
nodeStack.set(id, cb);
}
};
export const detachClick = (id, node) => {
var _a;
const _window = (_a = node === null || node === void 0 ? void 0 : node.ownerDocument.defaultView) === null || _a === void 0 ? void 0 : _a.window;
if (_window) {
nodeStack.delete(id);
if (nodeStack.size === 0) {
_window.removeEventListener("click", handleClick);
}
}
};