UNPKG

@oddbird/popup-polyfill

Version:
2 lines (1 loc) 2.33 kB
function s(){return typeof HTMLElement<"u"&&typeof HTMLElement.prototype=="object"&&"popUp"in HTMLElement.prototype}var a="Not supported on element that does not have valid popup attribute";function l(){let p=new WeakSet;Object.defineProperties(HTMLElement.prototype,{popUp:{enumerable:!0,configurable:!0,get(){let t=(this.getAttribute("popup")||"").toLowerCase();return t==="hint"?"hint":t==="manual"?"manual":t===""||t=="auto"?"auto":null},set(t){this.setAttribute("popup",t)}},defaultOpen:{enumerable:!0,configurable:!0,get(){return this.hasAttribute("defaultopen")},set(t){this.toggleAttribute("defaultopen",t)}},showPopUp:{enumerable:!0,configurable:!0,value(){if(!this.popUp)throw new DOMException(a,"NotSupportedError");if(p.has(this))throw new DOMException("Invalid on already-showing popups","InvalidStateError");this.classList.add(":open"),p.add(this),this.popUp==="auto"&&(this.hasAttribute("autofocus")?this:this.querySelector("[autofocus]"))?.focus()}},hidePopUp:{enumerable:!0,configurable:!0,value(){if(!this.popUp)throw new DOMException(a,"NotSupportedError");if(!p.has(this))throw new DOMException("Invalid on already-hidden popups","InvalidStateError");this.classList.remove(":open"),p.delete(this)}}});function r(){let t=document.querySelector("[popup=auto i][defaultopen]");t instanceof HTMLElement&&t.showPopUp();for(let o of document.querySelectorAll("[popup=manual i][defaultopen]"))o instanceof HTMLElement&&o.showPopUp()}document.readyState==="complete"||document.readyState==="interactive"?r():document.addEventListener("DOMContentLoaded",r,{once:!0}),document.addEventListener("click",t=>{let o=t.target;if(!(o instanceof HTMLElement))return;let n=o.ownerDocument,e=o.closest("[popup]"),u=o instanceof HTMLButtonElement;u&&o.hasAttribute("popupshowtarget")?(e=n.getElementById(o.getAttribute("popupshowtarget")||""),e&&e.popUp&&!p.has(e)&&e.showPopUp()):u&&o.hasAttribute("popuphidetarget")?(e=n.getElementById(o.getAttribute("popuphidetarget")||""),e&&e.popUp&&p.has(e)&&e.hidePopUp()):u&&o.hasAttribute("popuptoggletarget")&&(e=n.getElementById(o.getAttribute("popuptoggletarget")||""),e&&e.popUp&&(p.has(e)?e.hidePopUp():e.showPopUp()));for(let i of n.querySelectorAll('[popup="" i].\\:open, [popup=auto i].\\:open, [popup=hint i].\\:open'))i instanceof HTMLElement&&i!==e&&i.hidePopUp()})}s()||l();