@1hook/use-is-online 0.0.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ declare function useIsOnline(): boolean;
2
+
3
+ export { useIsOnline };
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ import React from 'react';
2
+ import { isServer } from '@1hook/utils/is-server';
3
+
4
+ // src/index.ts
5
+ var listeners = /* @__PURE__ */ new Set();
6
+ if (!isServer) {
7
+ window.addEventListener(
8
+ "online",
9
+ () => {
10
+ listeners.forEach((l) => l(true));
11
+ },
12
+ { passive: true }
13
+ );
14
+ window.addEventListener(
15
+ "offline",
16
+ () => {
17
+ listeners.forEach((l) => l(false));
18
+ },
19
+ { passive: true }
20
+ );
21
+ }
22
+ function useIsOnline() {
23
+ const [isOnline, setIsOnline] = React.useState(isServer || navigator.onLine);
24
+ React.useEffect(() => {
25
+ listeners.add(setIsOnline);
26
+ return () => {
27
+ listeners.delete(setIsOnline);
28
+ };
29
+ }, []);
30
+ return isOnline;
31
+ }
32
+
33
+ export { useIsOnline };
34
+ //# sourceMappingURL=index.js.map
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAGA,IAAM,SAAA,uBAAgB,GAA+B,EAAA;AAErD,IAAI,CAAC,QAAU,EAAA;AACb,EAAO,MAAA,CAAA,gBAAA;AAAA,IACL,QAAA;AAAA,IACA,MAAM;AACJ,MAAA,SAAA,CAAU,OAAQ,CAAA,CAAC,CAAM,KAAA,CAAA,CAAE,IAAI,CAAC,CAAA;AAAA,KAClC;AAAA,IACA,EAAE,SAAS,IAAK;AAAA,GAClB;AACA,EAAO,MAAA,CAAA,gBAAA;AAAA,IACL,SAAA;AAAA,IACA,MAAM;AACJ,MAAA,SAAA,CAAU,OAAQ,CAAA,CAAC,CAAM,KAAA,CAAA,CAAE,KAAK,CAAC,CAAA;AAAA,KACnC;AAAA,IACA,EAAE,SAAS,IAAK;AAAA,GAClB;AACF;AAEO,SAAS,WAAc,GAAA;AAC5B,EAAM,MAAA,CAAC,UAAU,WAAW,CAAA,GAAI,MAAM,QAAS,CAAA,QAAA,IAAY,UAAU,MAAM,CAAA;AAE3E,EAAA,KAAA,CAAM,UAAU,MAAM;AACpB,IAAA,SAAA,CAAU,IAAI,WAAW,CAAA;AACzB,IAAA,OAAO,MAAM;AACX,MAAA,SAAA,CAAU,OAAO,WAAW,CAAA;AAAA,KAC9B;AAAA,GACF,EAAG,EAAE,CAAA;AAEL,EAAO,OAAA,QAAA;AACT","file":"index.js","sourcesContent":["import React from 'react'\nimport { isServer } from '@1hook/utils/is-server'\n\nconst listeners = new Set<(online: boolean) => void>()\n\nif (!isServer) {\n window.addEventListener(\n 'online',\n () => {\n listeners.forEach((l) => l(true))\n },\n { passive: true },\n )\n window.addEventListener(\n 'offline',\n () => {\n listeners.forEach((l) => l(false))\n },\n { passive: true },\n )\n}\n\nexport function useIsOnline() {\n const [isOnline, setIsOnline] = React.useState(isServer || navigator.onLine)\n\n React.useEffect(() => {\n listeners.add(setIsOnline)\n return () => {\n listeners.delete(setIsOnline)\n }\n }, [])\n\n return isOnline\n}\n"]}
Binary file
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@1hook/use-is-online",
3
+ "version": "0.0.1-beta.1",
4
+ "files": [
5
+ "dist",
6
+ "README.md"
7
+ ],
8
+ "type": "module",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "peerDependencies": {
17
+ "@types/react": ">=18.0.0 || >=19.0.0",
18
+ "@types/react-dom": ">=18.0.0 || >=19.0.0",
19
+ "react": ">=18.0.0 || >=19.0.0",
20
+ "react-dom": ">=18.0.0 || >=19.0.0"
21
+ },
22
+ "dependencies": {
23
+ "@1hook/utils": "0.0.1-beta.1"
24
+ },
25
+ "tag": "beta",
26
+ "scripts": {
27
+ "build": "NODE_ENV=production tsup",
28
+ "dev": "concurrently \"tsup --watch\" \"vitest\"",
29
+ "test": "vitest run && tsc --noEmit",
30
+ "lint": "eslint . && tsc --noEmit",
31
+ "lint:eslint": "eslint .",
32
+ "lint:typescript": "tsc --noEmit"
33
+ }
34
+ }