UNPKG

react-gsi

Version:

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

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