UNPKG

react-gsi

Version:

React bindings for the 'Sign in With Google for Web' API

2 lines (1 loc) 2.36 kB
"use strict";var e=require("react/jsx-runtime"),t=require("react");function n(e){const{parent:n,configuration:r,onRender:o}=e;t.useEffect((()=>{google.accounts.id.renderButton(n,r),o?.()}),[n,r,o])}const r={type:"standard"};function o(e){return n(e),null}const c={type:"idle"};const s="https://accounts.google.com/gsi/client";function u(e={}){const{src:n=s}=e;return function(e){const{parent:n,onCreate:r}=e,[o,s]=t.useState(c);return t.useEffect((()=>{const e=document.createElement("script");return e.addEventListener("load",(e=>{s({type:"loaded",event:e})})),e.addEventListener("error",(e=>{s({type:"error",event:e})})),r?.(e),(n??document.head).append(e),s({type:"loading"}),function(){e.remove()}}),[r,n]),{status:o}}({onCreate:t.useCallback((e=>{e.src=n}),[n])})}const i={show:!0};const a=t.createContext(null);exports.GOOGLE_GSI_CLIENT_URL=s,exports.GSI_BUTTON_DEFAULT_CONFIGURATION=r,exports.GsiButton=function(n){const{configuration:c=r,fallback:s}=n,[u,i]=t.useState(null),[a,l]=t.useState(!1);return e.jsxs(e.Fragment,{children:[!a&&s,e.jsx("div",{ref:i,children:u&&e.jsx(o,{parent:u,configuration:c,onRender:function(){l(!0)}})})]})},exports.GsiClient=function(t){const{fallback:n,idle:r,loading:o,error:c,children:s}=t,{status:i}=u();switch(i.type){case"idle":return e.jsx(e.Fragment,{children:r?.(i)??n});case"loading":return e.jsx(e.Fragment,{children:o?.(i)??n});case"loaded":return e.jsx(e.Fragment,{children:s});case"error":return e.jsx(e.Fragment,{children:c?.(i)??n})}},exports.IdTokenContext=a,exports.IdTokenProvider=function(n){const{configuration:r,children:o}=n,c=r.callback,[s,u]=t.useState(null),i=t.useCallback((e=>{u(e),c?.(e)}),[c]);return t.useEffect((()=>{google.accounts.id.initialize({...r,callback:i})}),[r,i]),e.jsx(a.Provider,{value:s,children:o})},exports.isError=function(e){return"error"===e.type},exports.isIdle=function(e){return"idle"===e.type},exports.isLoaded=function(e){return"loaded"===e.type},exports.isLoading=function(e){return"loading"===e.type},exports.useGsiClient=u,exports.useIdToken=function(){return t.useContext(a)},exports.useOneTap=function(e=i){const{show:n=!0,onNotify:r}=e,o=t.useCallback((()=>{google.accounts.id.prompt(r)}),[r]),c=t.useCallback((()=>{google.accounts.id.cancel()}),[]);return t.useEffect((()=>(n&&o(),function(){c()})),[n,o,c]),{prompt:o,cancel:c}},exports.useRenderButton=n;