react-utterances-client
Version:
Another React component for using Utterances 🔮 on your website!
1 lines • 4.02 kB
JavaScript
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`react`);c=s(c);let l=require(`classcat`);l=s(l);let u=require(`use-sync-external-store/shim`),d=require(`react/jsx-runtime`);var f=`.utterances{box-sizing:border-box;width:100%;max-width:760px;margin-left:auto;margin-right:auto;position:relative}.utterances-frame{--lightningcss-light:initial;--lightningcss-dark: ;color-scheme:light;border:0;width:1px;min-width:100%;max-width:100%;height:100%;position:absolute;left:0;right:0}`;function p(e,t){window.dispatchEvent(new StorageEvent(`storage`,{key:e,newValue:t}))}var m=(e,t,n)=>{e.setItem(t,n),p(t,n)},h=(e,t)=>{e.removeItem(t),p(t)},g=(e,t)=>e.getItem(t),_=e=>(window.addEventListener(`storage`,e),()=>window.removeEventListener(`storage`,e)),v=()=>``,y=e=>e===`session`?window.sessionStorage:window.localStorage;function b(e){let t=`utterances-session`,n=(0,u.useSyncExternalStore)(_,()=>g(y(e),t),v),r=c.useCallback(r=>{try{let i=typeof r==`function`?r(n||``):r;i==null?h(y(e),t):m(y(e),t,i)}catch(e){console.warn(e)}},[t,n,e]);return c.useEffect(()=>{g(y(e),t)===null&&m(y(e),t,``)},[t,e]),[n||``,r]}var x=`preferred-color-scheme`,S=`https://utteranc.es`,C=`${S}/utterances.html`,w=({repo:e,theme:t=`github-light`,label:n,issueNumber:r,issueTerm:i,tokenStorage:a=`local`,loading:o=`lazy`,onLoad:s,onError:u,placeholder:p=!1,containerClassName:m,containerStyle:h,iframeClassName:g,iframeStyle:_})=>{let[v,y]=c.useState({}),[w,T]=c.useState(!1),[E,D]=b(a);c.useEffect(()=>{let e=document.createElement(`style`);return e.appendChild(document.createTextNode(f)),document.head.appendChild(e),()=>{document.head.removeChild(e)}},[]),c.useEffect(()=>{let a={};a.repo=e,t===x?a.theme=window.matchMedia(`(prefers-color-scheme: dark)`).matches?`github-dark`:`github-light`:a.theme=t,n&&(a.label=n),r&&(a[`issue-number`]=String(r)),i&&(Array.isArray(i)?a[`issue-term`]=i.join(` `):a[`issue-term`]=i);let o=new URL(location.href),s=o.searchParams.get(`utterances`);s&&(D(s),o.searchParams.delete(`utterances`),history.replaceState(void 0,document.title,o.href));let c=document.querySelector(`link[rel='canonical']`);a.url=c?c.href:o.origin+o.pathname+o.search,a.origin=o.origin,a.pathname=o.pathname.length<2?`index`:o.pathname.slice(1).replace(/\.\w+$/,``),a.title=document.title;let l=document.querySelector(`meta[name='description']`);a.description=l?l.content:``;let u=encodeURIComponent(a.description).length;u>1e3&&(a.description=a.description.slice(0,Math.floor(a.description.length*1e3/u)));let d=document.querySelector(`meta[property='og:title'],meta[name='og:title']`);return a[`og:title`]=d?d.content:``,a.session=s||E||``,y(a),()=>{y({})}},[e,t,n,r,i,a,E,D]);let O=c.useRef(null),k=c.useCallback(e=>{if(e.origin!==S)return;let t=e.data;t&&t.type===`resize`&&t.height&&O.current&&(O.current.style.height=`${t.height}px`)},[]);c.useEffect(()=>(window.addEventListener(`message`,k),()=>window.removeEventListener(`message`,k)),[k]);let A=c.useMemo(()=>JSON.stringify(v)!==`{}`,[v]),j=c.useCallback(e=>{T(!0),s&&s(e)},[s]),M=c.useMemo(()=>typeof p==`boolean`?p?(0,d.jsx)(`p`,{children:`Loading comments...`}):null:p,[p]);return(0,d.jsx)(`div`,{className:(0,l.default)([`utterances`,m]),ref:O,style:h,children:A&&(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(`iframe`,{className:(0,l.default)([`utterances-frame`,g]),title:`Comments`,scrolling:`no`,src:`${C}?${new URLSearchParams(v)}`,loading:p?`eager`:o,style:{display:!w&&p?`none`:`block`,..._},onLoad:j,onError:u}),!w&&M]})})};exports.Utterances=w;