@zag-js/dom-utils
Advanced tools
Comparing version 0.0.0-dev-20220616105639 to 0.0.0-dev-20220616113129
@@ -179,66 +179,2 @@ "use strict"; | ||
// src/global-listener.ts | ||
function getListenerElements() { | ||
; | ||
globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map(); | ||
return globalThis.__listenerElements__; | ||
} | ||
function getListenerCache() { | ||
; | ||
globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map(); | ||
return globalThis.__listenerCache__; | ||
} | ||
function addGlobalEventListener(node, type, handler, options) { | ||
var _a; | ||
if (!node) | ||
return noop; | ||
const hash = JSON.stringify({ type, options }); | ||
const listenerElements = getListenerElements(); | ||
const listenerCache = getListenerCache(); | ||
const group = listenerElements.get(node); | ||
if (!listenerElements.has(node)) { | ||
const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]); | ||
listenerElements.set(node, group2); | ||
} else if (group == null ? void 0 : group.has(hash)) { | ||
(_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler); | ||
} else { | ||
group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler])); | ||
} | ||
function attach(node2) { | ||
var _a2, _b; | ||
function listener(event) { | ||
var _a3; | ||
const group2 = listenerElements.get(node2); | ||
(_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event)); | ||
} | ||
if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) { | ||
listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]])); | ||
node2.addEventListener(type, listener, options); | ||
return; | ||
} | ||
if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) { | ||
(_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener); | ||
node2.addEventListener(type, listener, options); | ||
} | ||
} | ||
attach(node); | ||
return function remove() { | ||
var _a2, _b, _c, _d; | ||
if (!listenerElements.has(node)) | ||
return; | ||
const group2 = listenerElements.get(node); | ||
(_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler); | ||
if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) { | ||
const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash); | ||
node.removeEventListener(type, listener, options); | ||
group2 == null ? void 0 : group2.delete(hash); | ||
(_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash); | ||
if ((group2 == null ? void 0 : group2.size) === 0) { | ||
listenerElements.delete(node); | ||
listenerCache.delete(node); | ||
} | ||
} | ||
}; | ||
} | ||
// src/listener.ts | ||
@@ -256,5 +192,8 @@ var isRef = (v) => hasProp(v, "current"); | ||
} | ||
function addDomEvent(target, event, listener, options) { | ||
function addDomEvent(target, eventName, handler, options) { | ||
const node = isRef(target) ? target.current : runIfFn(target); | ||
return addGlobalEventListener(node, event, listener, options); | ||
node == null ? void 0 : node.addEventListener(eventName, handler, options); | ||
return () => { | ||
node == null ? void 0 : node.removeEventListener(eventName, handler, options); | ||
}; | ||
} | ||
@@ -261,0 +200,0 @@ function addPointerEvent(target, event, listener, options) { |
@@ -8,3 +8,3 @@ import { AnyPointerEvent, DOMEventTarget, EventMap, PointerEventInfo } from "./listener.types"; | ||
}; | ||
export declare function addDomEvent<K extends keyof EventMap>(target: DOMEventTarget, event: K, listener: (event: EventMap[K]) => void, options?: boolean | AddEventListenerOptions): () => void; | ||
export declare function addDomEvent<K extends keyof EventMap>(target: DOMEventTarget, eventName: K, handler: (event: EventMap[K]) => void, options?: boolean | AddEventListenerOptions): () => void; | ||
export declare function addPointerEvent<K extends keyof EventMap>(target: DOMEventTarget, event: K, listener: (event: EventMap[K], info: PointerEventInfo) => void, options?: boolean | AddEventListenerOptions): () => void; | ||
@@ -11,0 +11,0 @@ export declare function extractClientInfo(event: AnyPointerEvent): { |
{ | ||
"name": "@zag-js/dom-utils", | ||
"version": "0.0.0-dev-20220616105639", | ||
"version": "0.0.0-dev-20220616113129", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83338
32
2438