UNPKG

@aotearoan/neon

Version:

Neon is a lightweight design library of Vue 3 components with minimal dependencies.

50 lines (49 loc) 2.13 kB
var n = Object.defineProperty; var o = (s, e, t) => e in s ? n(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var i = (s, e, t) => (o(s, typeof e != "symbol" ? e + "" : e, t), t); class d { /** * Initialise utility to listen to keyboard, mouse & touch events. * * @param target The popup <em>closable</em> element. * @param closeCallback Callback function triggered when an event fires that should close the target * element. */ constructor(e, t) { i(this, "target"); i(this, "closeCallback"); i(this, "_open", !1); this.target = e, this.closeCallback = t, document.addEventListener("keydown", this.escapeKeyListener.bind(this), { passive: !0 }), document.addEventListener("mousedown", this.handleOutsideClick.bind(this), { passive: !0 }), document.addEventListener("touchstart", this.handleOutsideClick.bind(this), { passive: !0 }); } /** * Inform the class that the target is now open. This adds the global <em>neon-closable--open</em> class to the * document body to prevent page scrolling while a popup is open. */ open() { this._open = !0, document.body.classList.add("neon-closable--open"); } /** * Destroy the listeners. Call this in the parent component's onUnmounted method. */ destroy() { document.removeEventListener("keydown", this.escapeKeyListener.bind(this)), document.removeEventListener("mousedown", this.handleOutsideClick.bind(this)), document.removeEventListener("touchstart", this.handleOutsideClick.bind(this)); } escapeKeyListener(e) { e.key === "Escape" && this.close(); } /** * Handle closing of the target element. This triggers the closeCallback & removes the global * <em>neon-closable--open</em> class from the document body. */ close() { this.closeCallback(), this._open && (document.body.classList.remove("neon-closable--open"), this._open = !1); } handleOutsideClick(e) { const t = e.target && e.target; return t && !this.target.contains(t) && this.close(), !0; } } export { d as NeonClosableUtils }; //# sourceMappingURL=NeonClosableUtils.es.js.map