UNPKG

2.07 kBSource Map (JSON)View Raw
1{"version":3,"sources":["useOnlineState.js"],"names":["useOnlineState","isSupported","window","navigator","onLine","isOnline","setIsOnline","whenOnline","capture","whenOffline","console","warn"],"mappings":";;;;;;;AAAA;;AACA;;;;;;;;;;;;AAMA,IAAMA,cAAc,GAAG,SAAjBA,cAAiB,GAAM;AAK3B,MAAMC,WAAW,GAAG,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,cAAcA,MAAnE;;AAL2B,kBAMK,qBAASD,WAAW,GAAGE,SAAS,CAACC,MAAb,GAAsB,IAA1C,CANL;AAAA;AAAA,MAMpBC,QANoB;AAAA,MAMVC,WANU;;AAO3B,MAAMC,UAAU,GAAG,gCAAe,QAAf,EAAyB;AAAEC,IAAAA,OAAO,EAAE;AAAX,GAAzB,CAAnB;AACA,MAAMC,WAAW,GAAG,gCAAe,SAAf,EAA0B;AAAED,IAAAA,OAAO,EAAE;AAAX,GAA1B,CAApB;;AAEA,MAAI,CAACP,WAAL,EAAkB;AAEhBS,IAAAA,OAAO,CAACC,IAAR,CAAa,0GAAb;AACA,WAAON,QAAP;AACD;;AAEDE,EAAAA,UAAU,CAAC,YAAM;AACfD,IAAAA,WAAW,CAAC,IAAD,CAAX;AACD,GAFS,CAAV;AAIAG,EAAAA,WAAW,CAAC,YAAM;AAChBH,IAAAA,WAAW,CAAC,KAAD,CAAX;AACD,GAFU,CAAX;AAIA,SAAOD,QAAP;AACD,CAzBD;;eA2BeL,c","sourcesContent":["import { useState } from 'react';\nimport useGlobalEvent from './useGlobalEvent';\n\n/**\n * Uses the [Navigator online API](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine) to define\n * whether the browser is connected or not.\n */\nconst useOnlineState = () => {\n /**\n * If the browser doesn't support the `navigator.onLine` state, the hook will always return true\n * assuming the app is already online.\n */\n const isSupported = typeof window !== 'undefined' && 'ononline' in window;\n const [isOnline, setIsOnline] = useState(isSupported ? navigator.onLine : true);\n const whenOnline = useGlobalEvent('online', { capture: true });\n const whenOffline = useGlobalEvent('offline', { capture: true });\n\n if (!isSupported) {\n // eslint-disable-next-line max-len, no-console\n console.warn('The current device does not support the \\'online/offline\\' events, you should avoid using useOnlineState');\n return isOnline;\n }\n\n whenOnline(() => {\n setIsOnline(true);\n });\n\n whenOffline(() => {\n setIsOnline(false);\n });\n\n return isOnline;\n};\n\nexport default useOnlineState;\n"],"file":"useOnlineState.js"}
\No newline at end of file