Inkspring / website / public / share.js
| 1 | /* Share (2026-11-21) |
| 2 | One small script shared by every one of our sites. |
| 3 | Any element with [data-pm-share] becomes a Share button: |
| 4 | - phones and tablets (touch) open the native share sheet (Web Share API) |
| 5 | - desktops get a small menu: Copy link, X, LinkedIn, Facebook, Email |
| 6 | (plus "More options" when the browser has its own share sheet) |
| 7 | Optional attributes: data-share-url, data-share-title, data-share-text. |
| 8 | A child with [data-pm-share-label] shows "Link copied" after a copy. */ |
| 9 | (function () { |
| 10 | if (typeof window === 'undefined' || window.__pmShareReady) return; |
| 11 | window.__pmShareReady = true; |
| 12 | |
| 13 | var CSS = [ |
| 14 | '.pm-share-btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;height:44px;padding:0 18px 0 15px;border-radius:999px;border:1px solid var(--pm-share-line,rgba(245,128,37,.6));background:var(--pm-share-bg,transparent);color:var(--pm-share-ink,inherit);font:inherit;font-size:15px;font-weight:600;line-height:1;cursor:pointer;white-space:nowrap;text-decoration:none;box-shadow:0 0 18px var(--pm-share-glow,rgba(245,128,37,.22));transition:border-color .2s,box-shadow .2s,transform .2s,background .2s}', |
| 15 | '.pm-share-btn:hover{border-color:var(--pm-share-accent,#f58025);box-shadow:0 0 26px var(--pm-share-glow-hover,rgba(245,128,37,.42))}', |
| 16 | '.pm-share-btn:active{transform:translateY(1px)}', |
| 17 | '.pm-share-btn:focus-visible{outline:2px solid var(--pm-share-accent,#f58025);outline-offset:3px}', |
| 18 | '.pm-share-btn svg{width:17px;height:17px;flex:none;color:var(--pm-share-accent,#f58025)}', |
| 19 | '.pm-share-btn.is-small{height:36px;padding:0 14px 0 12px;font-size:13.5px;gap:7px}', |
| 20 | '.pm-share-btn.is-small svg{width:15px;height:15px}', |
| 21 | '.pm-share-pop{position:fixed;z-index:2147483000;min-width:228px;padding:8px;border-radius:18px;background:rgba(16,18,24,.97);color:#f4f4f6;border:1px solid rgba(255,255,255,.12);box-shadow:0 24px 60px -18px rgba(0,0,0,.6),0 0 0 1px rgba(0,0,0,.25);font:500 14.5px/1.2 -apple-system,BlinkMacSystemFont,"Inter","Segoe UI",Roboto,sans-serif;backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);opacity:0;transform:translateY(-4px) scale(.98);transform-origin:top center;transition:opacity .14s,transform .14s}', |
| 22 | '.pm-share-pop.is-open{opacity:1;transform:none}', |
| 23 | '.pm-share-pop .pm-share-head{padding:8px 12px 8px;font-size:12px;letter-spacing:.08em;text-transform:uppercase;color:rgba(244,244,246,.6)}', |
| 24 | '.pm-share-pop a,.pm-share-pop button{display:flex;align-items:center;gap:12px;width:100%;padding:10px 12px;border:0;border-radius:999px;background:transparent;color:inherit;font:inherit;text-align:left;text-decoration:none;cursor:pointer}', |
| 25 | '.pm-share-pop a:hover,.pm-share-pop button:hover,.pm-share-pop a:focus-visible,.pm-share-pop button:focus-visible{background:rgba(255,255,255,.09);outline:none}', |
| 26 | '.pm-share-pop svg{width:18px;height:18px;flex:none;color:var(--pm-pop-accent,#f58025)}', |
| 27 | '.pm-share-pop svg[fill=currentColor],.pm-share-pop svg[fill=currentColor] *{fill:currentColor!important;stroke:none!important}', |
| 28 | '.pm-share-pop svg[fill=none],.pm-share-pop svg[fill=none] *,.pm-share-btn svg[fill=none],.pm-share-btn svg[fill=none] *{fill:none!important;stroke:currentColor!important}', |
| 29 | '.pm-share-pop .pm-share-sep{height:1px;margin:6px 10px;background:rgba(255,255,255,.1)}', |
| 30 | '.pm-share-toast{position:fixed;left:50%;bottom:28px;z-index:2147483001;transform:translate(-50%,12px);padding:12px 20px;border-radius:999px;background:rgba(16,18,24,.96);color:#fff;font:600 14px/1 -apple-system,BlinkMacSystemFont,"Inter","Segoe UI",Roboto,sans-serif;border:1px solid rgba(245,128,37,.55);box-shadow:0 0 22px rgba(245,128,37,.3);opacity:0;transition:opacity .2s,transform .2s;pointer-events:none}', |
| 31 | '.pm-share-toast.is-on{opacity:1;transform:translate(-50%,0)}' |
| 32 | ].join('\n'); |
| 33 | |
| 34 | var ICON = { |
| 35 | link: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 13a5 5 0 0 0 7.07 0l3-3a5 5 0 0 0-7.07-7.07l-1.5 1.5"/><path d="M14 11a5 5 0 0 0-7.07 0l-3 3a5 5 0 0 0 7.07 7.07l1.5-1.5"/></svg>', |
| 36 | check: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg>', |
| 37 | x: '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M18.24 2.25h3.31l-7.23 8.26 8.5 11.24h-6.66l-5.21-6.82-5.97 6.82H1.67l7.73-8.84L1.25 2.25h6.83l4.71 6.23 5.45-6.23Zm-1.16 17.52h1.83L7.08 4.13H5.12l11.96 15.64Z"/></svg>', |
| 38 | linkedin: '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M20.45 20.45h-3.56v-5.57c0-1.33-.02-3.04-1.85-3.04-1.85 0-2.14 1.45-2.14 2.94v5.67H9.35V9h3.41v1.56h.05c.48-.9 1.64-1.85 3.37-1.85 3.6 0 4.27 2.37 4.27 5.46v6.28ZM5.34 7.43a2.06 2.06 0 1 1 0-4.13 2.06 2.06 0 0 1 0 4.13ZM7.12 20.45H3.56V9h3.56v11.45ZM22.22 0H1.77C.79 0 0 .77 0 1.73v20.54C0 23.23.79 24 1.77 24h20.45c.98 0 1.78-.77 1.78-1.73V1.73C24 .77 23.2 0 22.22 0Z"/></svg>', |
| 39 | facebook: '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M24 12.07C24 5.41 18.63 0 12 0S0 5.41 0 12.07C0 18.1 4.39 23.1 10.13 24v-8.44H7.08v-3.49h3.05V9.41c0-3.02 1.79-4.69 4.53-4.69 1.31 0 2.68.24 2.68.24v2.97h-1.51c-1.49 0-1.96.93-1.96 1.89v2.25h3.33l-.53 3.49h-2.8V24C19.61 23.1 24 18.1 24 12.07Z"/></svg>', |
| 40 | mail: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="2.5" y="4.5" width="19" height="15" rx="3"/><path d="m3.5 6.5 8.5 6 8.5-6"/></svg>', |
| 41 | more: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3v12"/><path d="m7.5 7.5 4.5-4.5 4.5 4.5"/><path d="M5 12v6.5A2.5 2.5 0 0 0 7.5 21h9a2.5 2.5 0 0 0 2.5-2.5V12"/></svg>' |
| 42 | }; |
| 43 | |
| 44 | function injectCss() { |
| 45 | if (document.getElementById('pm-share-css')) return; |
| 46 | var s = document.createElement('style'); |
| 47 | s.id = 'pm-share-css'; |
| 48 | s.textContent = CSS; |
| 49 | // in <body>, not <head>: some frameworks (Next.js) rebuild <head> after load |
| 50 | (document.body || document.documentElement).insertBefore(s, (document.body || document.documentElement).firstChild); |
| 51 | } |
| 52 | |
| 53 | function dataFor(btn) { |
| 54 | var d = btn.dataset || {}; |
| 55 | var canon = document.querySelector('link[rel="canonical"]'); |
| 56 | var url = d.shareUrl || (canon && canon.href) || location.href; |
| 57 | var title = d.shareTitle || document.title || ''; |
| 58 | var text = d.shareText || ''; |
| 59 | return { url: url, title: title, text: text }; |
| 60 | } |
| 61 | |
| 62 | function isTouch() { |
| 63 | try { |
| 64 | return window.matchMedia('(pointer: coarse)').matches || /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent); |
| 65 | } catch (e) { return false; } |
| 66 | } |
| 67 | |
| 68 | var toastTimer; |
| 69 | function toast(msg) { |
| 70 | var t = document.querySelector('.pm-share-toast'); |
| 71 | if (!t) { |
| 72 | t = document.createElement('div'); |
| 73 | t.className = 'pm-share-toast'; |
| 74 | t.setAttribute('role', 'status'); |
| 75 | t.setAttribute('aria-live', 'polite'); |
| 76 | document.body.appendChild(t); |
| 77 | } |
| 78 | t.textContent = msg; |
| 79 | requestAnimationFrame(function () { t.classList.add('is-on'); }); |
| 80 | clearTimeout(toastTimer); |
| 81 | toastTimer = setTimeout(function () { t.classList.remove('is-on'); }, 2200); |
| 82 | } |
| 83 | |
| 84 | function flashLabel(btn) { |
| 85 | var label = btn && btn.querySelector('[data-pm-share-label]'); |
| 86 | if (!label) return; |
| 87 | if (!label.dataset.pmOrig) label.dataset.pmOrig = label.textContent; |
| 88 | label.textContent = 'Link copied'; |
| 89 | clearTimeout(label.__pmT); |
| 90 | label.__pmT = setTimeout(function () { label.textContent = label.dataset.pmOrig; }, 2200); |
| 91 | } |
| 92 | |
| 93 | function copy(text) { |
| 94 | if (navigator.clipboard && window.isSecureContext) { |
| 95 | return navigator.clipboard.writeText(text); |
| 96 | } |
| 97 | return new Promise(function (resolve, reject) { |
| 98 | var ta = document.createElement('textarea'); |
| 99 | ta.value = text; |
| 100 | ta.setAttribute('readonly', ''); |
| 101 | ta.style.position = 'fixed'; |
| 102 | ta.style.opacity = '0'; |
| 103 | document.body.appendChild(ta); |
| 104 | ta.select(); |
| 105 | try { document.execCommand('copy') ? resolve() : reject(); } catch (e) { reject(e); } |
| 106 | document.body.removeChild(ta); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | function doCopy(btn, data) { |
| 111 | copy(data.url).then(function () { |
| 112 | flashLabel(btn); |
| 113 | toast('Link copied'); |
| 114 | }, function () { |
| 115 | window.prompt('Copy this link:', data.url); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | var pop = null, popBtn = null; |
| 120 | function closePop() { |
| 121 | if (!pop) return; |
| 122 | var p = pop; |
| 123 | pop = null; |
| 124 | if (popBtn) popBtn.setAttribute('aria-expanded', 'false'); |
| 125 | popBtn = null; |
| 126 | p.classList.remove('is-open'); |
| 127 | setTimeout(function () { if (p.parentNode) p.parentNode.removeChild(p); }, 160); |
| 128 | } |
| 129 | |
| 130 | function place(p, btn) { |
| 131 | var r = btn.getBoundingClientRect(); |
| 132 | var w = p.offsetWidth, h = p.offsetHeight; |
| 133 | var vw = document.documentElement.clientWidth, vh = window.innerHeight; |
| 134 | var left = Math.min(Math.max(12, r.left + r.width / 2 - w / 2), vw - w - 12); |
| 135 | var top = r.bottom + 10; |
| 136 | if (top + h > vh - 12 && r.top - h - 10 > 12) top = r.top - h - 10; |
| 137 | p.style.left = left + 'px'; |
| 138 | p.style.top = Math.max(12, top) + 'px'; |
| 139 | } |
| 140 | |
| 141 | function openPop(btn, data) { |
| 142 | closePop(); |
| 143 | var enc = encodeURIComponent; |
| 144 | var line = data.text ? data.title + ' - ' + data.text : data.title; |
| 145 | var items = [ |
| 146 | { k: 'copy', icon: ICON.link, label: 'Copy link' }, |
| 147 | { k: 'x', icon: ICON.x, label: 'Post on X', href: 'https://x.com/intent/post?text=' + enc(line) + '&url=' + enc(data.url) }, |
| 148 | { k: 'linkedin', icon: ICON.linkedin, label: 'Share on LinkedIn', href: 'https://www.linkedin.com/sharing/share-offsite/?url=' + enc(data.url) }, |
| 149 | { k: 'facebook', icon: ICON.facebook, label: 'Share on Facebook', href: 'https://www.facebook.com/sharer/sharer.php?u=' + enc(data.url) }, |
| 150 | { k: 'email', icon: ICON.mail, label: 'Email', href: 'mailto:?subject=' + enc(data.title) + '&body=' + enc((data.text ? data.text + '\n\n' : '') + data.url) } |
| 151 | ]; |
| 152 | if (navigator.share) items.push({ k: 'more', icon: ICON.more, label: 'More options', sep: true }); |
| 153 | |
| 154 | var p = document.createElement('div'); |
| 155 | p.className = 'pm-share-pop'; |
| 156 | p.setAttribute('role', 'menu'); |
| 157 | p.setAttribute('aria-label', 'Share'); |
| 158 | var html = '<div class="pm-share-head">Share</div>'; |
| 159 | items.forEach(function (it) { |
| 160 | if (it.sep) html += '<div class="pm-share-sep"></div>'; |
| 161 | if (it.href) { |
| 162 | var ext = it.k !== 'email'; |
| 163 | html += '<a role="menuitem" data-k="' + it.k + '" href="' + it.href.replace(/"/g, '"') + '"' + (ext ? ' target="_blank" rel="noopener noreferrer"' : '') + '>' + it.icon + '<span>' + it.label + '</span></a>'; |
| 164 | } else { |
| 165 | html += '<button type="button" role="menuitem" data-k="' + it.k + '">' + it.icon + '<span>' + it.label + '</span></button>'; |
| 166 | } |
| 167 | }); |
| 168 | p.innerHTML = html; |
| 169 | try { |
| 170 | var acc = getComputedStyle(btn).getPropertyValue('--pm-share-accent').trim(); |
| 171 | if (acc) p.style.setProperty('--pm-pop-accent', acc); |
| 172 | } catch (e) {} |
| 173 | document.body.appendChild(p); |
| 174 | place(p, btn); |
| 175 | pop = p; |
| 176 | popBtn = btn; |
| 177 | btn.setAttribute('aria-expanded', 'true'); |
| 178 | requestAnimationFrame(function () { p.classList.add('is-open'); }); |
| 179 | var first = p.querySelector('[role="menuitem"]'); |
| 180 | if (first) first.focus({ preventScroll: true }); |
| 181 | |
| 182 | p.addEventListener('click', function (e) { |
| 183 | var el = e.target.closest('[data-k]'); |
| 184 | if (!el) return; |
| 185 | var k = el.getAttribute('data-k'); |
| 186 | if (k === 'copy') { |
| 187 | e.preventDefault(); |
| 188 | doCopy(btn, data); |
| 189 | el.innerHTML = ICON.check + '<span>Copied</span>'; |
| 190 | setTimeout(closePop, 650); |
| 191 | } else if (k === 'more') { |
| 192 | e.preventDefault(); |
| 193 | closePop(); |
| 194 | navigator.share(data).catch(function () {}); |
| 195 | } else if (k === 'x' || k === 'linkedin' || k === 'facebook') { |
| 196 | e.preventDefault(); |
| 197 | window.open(el.href, 'pm-share', 'noopener,noreferrer,width=640,height=640'); |
| 198 | closePop(); |
| 199 | } else { |
| 200 | setTimeout(closePop, 50); |
| 201 | } |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | function onClick(e) { |
| 206 | var btn = e.target.closest && e.target.closest('[data-pm-share]'); |
| 207 | if (!btn) { |
| 208 | if (pop && !pop.contains(e.target)) closePop(); |
| 209 | return; |
| 210 | } |
| 211 | e.preventDefault(); |
| 212 | e.stopPropagation(); |
| 213 | injectCss(); |
| 214 | if (pop && popBtn === btn) { closePop(); return; } |
| 215 | var data = dataFor(btn); |
| 216 | if (navigator.share && isTouch()) { |
| 217 | navigator.share(data).catch(function (err) { |
| 218 | if (err && err.name === 'AbortError') return; |
| 219 | doCopy(btn, data); |
| 220 | }); |
| 221 | return; |
| 222 | } |
| 223 | openPop(btn, data); |
| 224 | } |
| 225 | |
| 226 | function init() { |
| 227 | injectCss(); |
| 228 | // re-add the styles if a framework swaps the page out after load |
| 229 | setTimeout(injectCss, 1500); |
| 230 | setTimeout(injectCss, 4000); |
| 231 | document.addEventListener('click', onClick, true); |
| 232 | document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && pop) { var b = popBtn; closePop(); if (b) b.focus(); } }); |
| 233 | window.addEventListener('resize', closePop); |
| 234 | window.addEventListener('scroll', function () { if (pop && popBtn) place(pop, popBtn); }, { passive: true }); |
| 235 | } |
| 236 | |
| 237 | if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init); |
| 238 | else init(); |
| 239 | })(); |