UNPKG

@parsonic/share-button

Version:

Native share button web component

3 lines (2 loc) 1.09 kB
(()=>{function o(s,t){return document.querySelector(`meta[property='og:${s}']`)?.getAttribute("content")??t}var n=class s extends HTMLElement{static register(t="share-button"){"share"in navigator&&customElements.define(t,s)}connectedCallback(){let t=document.createElement("button");t.textContent=this.dataset.buttonLabel??"Share",t.setAttribute("part","button");let a=document.createElement("slot");a.setAttribute("name","button"),a.appendChild(t),this.attachShadow({mode:"open"}).appendChild(a),a.addEventListener("click",()=>{let{url:i=o("url",location.href),title:u=o("title",document.title),text:l=o("description"),shareEventName:c="share",resultEventName:r="shareResult"}=this.dataset,e={url:i,text:l,title:u};navigator.canShare(e)&&this.dispatchEvent(new CustomEvent(c,{cancelable:!0,bubbles:!0,detail:e}))&&navigator.share(e).then(()=>{this.dispatchEvent(new CustomEvent(r,{bubbles:!0,detail:{result:"success",data:e}}))}).catch(d=>{this.dispatchEvent(new CustomEvent(r,{bubbles:!0,detail:{result:"error",data:e,error:d}}))})})}};n.register();})(); //# sourceMappingURL=min.js.map