@mado/hooks 0.1.0 → 0.3.0
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.
- package/CHANGELOG.md +12 -0
- package/dist/index.cjs +28 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/useDebounce.cjs +21 -0
- package/dist/useDebounce.cjs.map +1 -0
- package/dist/useDebounce.d.cts +5 -0
- package/dist/useDebounce.d.ts +5 -0
- package/dist/useDebounce.js +19 -0
- package/dist/useDebounce.js.map +1 -0
- package/dist/useIsMounted.cjs +16 -0
- package/dist/useIsMounted.cjs.map +1 -0
- package/dist/useIsMounted.d.cts +3 -0
- package/dist/useIsMounted.d.ts +3 -0
- package/dist/useIsMounted.js +14 -0
- package/dist/useIsMounted.js.map +1 -0
- package/dist/useOnline.cjs +31 -0
- package/dist/useOnline.cjs.map +1 -0
- package/dist/useOnline.d.cts +3 -0
- package/dist/useOnline.d.ts +3 -0
- package/dist/useOnline.js +29 -0
- package/dist/useOnline.js.map +1 -0
- package/dist/useRerender.cjs.map +1 -1
- package/dist/useRerender.d.cts +2 -2
- package/dist/useRerender.d.ts +2 -2
- package/dist/useRerender.js.map +1 -1
- package/dist/useVisibilityState.cjs +27 -0
- package/dist/useVisibilityState.cjs.map +1 -0
- package/dist/useVisibilityState.d.cts +3 -0
- package/dist/useVisibilityState.d.ts +3 -0
- package/dist/useVisibilityState.js +25 -0
- package/dist/useVisibilityState.js.map +1 -0
- package/package.json +4 -1
- package/src/index.ts +4 -0
- package/src/useDebounce.ts +21 -0
- package/src/useIsMounted.ts +11 -0
- package/src/useOnline.ts +28 -0
- package/src/useRerender.ts +2 -2
- package/src/useVisibilityState.ts +24 -0
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,28 +1,56 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var useDebounce_cjs = require('./useDebounce.cjs');
|
|
3
4
|
var useEvent_cjs = require('./useEvent.cjs');
|
|
5
|
+
var useIsMounted_cjs = require('./useIsMounted.cjs');
|
|
4
6
|
var useIsomorphicLayoutEffect_cjs = require('./useIsomorphicLayoutEffect.cjs');
|
|
7
|
+
var useOnline_cjs = require('./useOnline.cjs');
|
|
5
8
|
var useRerender_cjs = require('./useRerender.cjs');
|
|
9
|
+
var useVisibilityState_cjs = require('./useVisibilityState.cjs');
|
|
6
10
|
|
|
7
11
|
|
|
8
12
|
|
|
13
|
+
Object.keys(useDebounce_cjs).forEach(function (k) {
|
|
14
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return useDebounce_cjs[k]; }
|
|
17
|
+
});
|
|
18
|
+
});
|
|
9
19
|
Object.keys(useEvent_cjs).forEach(function (k) {
|
|
10
20
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
11
21
|
enumerable: true,
|
|
12
22
|
get: function () { return useEvent_cjs[k]; }
|
|
13
23
|
});
|
|
14
24
|
});
|
|
25
|
+
Object.keys(useIsMounted_cjs).forEach(function (k) {
|
|
26
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () { return useIsMounted_cjs[k]; }
|
|
29
|
+
});
|
|
30
|
+
});
|
|
15
31
|
Object.keys(useIsomorphicLayoutEffect_cjs).forEach(function (k) {
|
|
16
32
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
17
33
|
enumerable: true,
|
|
18
34
|
get: function () { return useIsomorphicLayoutEffect_cjs[k]; }
|
|
19
35
|
});
|
|
20
36
|
});
|
|
37
|
+
Object.keys(useOnline_cjs).forEach(function (k) {
|
|
38
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () { return useOnline_cjs[k]; }
|
|
41
|
+
});
|
|
42
|
+
});
|
|
21
43
|
Object.keys(useRerender_cjs).forEach(function (k) {
|
|
22
44
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
23
45
|
enumerable: true,
|
|
24
46
|
get: function () { return useRerender_cjs[k]; }
|
|
25
47
|
});
|
|
26
48
|
});
|
|
49
|
+
Object.keys(useVisibilityState_cjs).forEach(function (k) {
|
|
50
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
get: function () { return useVisibilityState_cjs[k]; }
|
|
53
|
+
});
|
|
54
|
+
});
|
|
27
55
|
//# sourceMappingURL=out.js.map
|
|
28
56
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useEvent';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useRerender';\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useOnline';\nexport * from './useRerender';\nexport * from './useVisibilityState';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
export { useDebounce } from './useDebounce.cjs';
|
|
1
2
|
export { useEvent } from './useEvent.cjs';
|
|
3
|
+
export { useIsMounted } from './useIsMounted.cjs';
|
|
2
4
|
export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.cjs';
|
|
5
|
+
export { useOnline } from './useOnline.cjs';
|
|
3
6
|
export { useRerender } from './useRerender.cjs';
|
|
7
|
+
export { useVisibilityState } from './useVisibilityState.cjs';
|
|
8
|
+
import '@mado/utils';
|
|
4
9
|
import 'react';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
export { useDebounce } from './useDebounce.js';
|
|
1
2
|
export { useEvent } from './useEvent.js';
|
|
3
|
+
export { useIsMounted } from './useIsMounted.js';
|
|
2
4
|
export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.js';
|
|
5
|
+
export { useOnline } from './useOnline.js';
|
|
3
6
|
export { useRerender } from './useRerender.js';
|
|
7
|
+
export { useVisibilityState } from './useVisibilityState.js';
|
|
8
|
+
import '@mado/utils';
|
|
4
9
|
import 'react';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
export * from './useDebounce.js';
|
|
1
2
|
export * from './useEvent.js';
|
|
3
|
+
export * from './useIsMounted.js';
|
|
2
4
|
export * from './useIsomorphicLayoutEffect.js';
|
|
5
|
+
export * from './useOnline.js';
|
|
3
6
|
export * from './useRerender.js';
|
|
7
|
+
export * from './useVisibilityState.js';
|
|
4
8
|
//# sourceMappingURL=out.js.map
|
|
5
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useEvent';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useRerender';\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useOnline';\nexport * from './useRerender';\nexport * from './useVisibilityState';\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@mado/utils');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var useEvent_cjs = require('./useEvent.cjs');
|
|
6
|
+
|
|
7
|
+
// src/useDebounce.ts
|
|
8
|
+
function useDebounce(fn, wait) {
|
|
9
|
+
const callback = useEvent_cjs.useEvent(fn);
|
|
10
|
+
const debounced = react.useMemo(() => utils.debounce(callback, wait), [callback, wait]);
|
|
11
|
+
react.useEffect(() => {
|
|
12
|
+
return () => {
|
|
13
|
+
debounced.cancel();
|
|
14
|
+
};
|
|
15
|
+
}, [debounced]);
|
|
16
|
+
return debounced;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
exports.useDebounce = useDebounce;
|
|
20
|
+
//# sourceMappingURL=out.js.map
|
|
21
|
+
//# sourceMappingURL=useDebounce.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useDebounce.ts"],"names":[],"mappings":";AAAA,SAA0B,gBAAgB;AAC1C,SAAS,WAAW,eAAe;AAEnC,SAAS,gBAAgB;AAElB,SAAS,YACd,IACA,MACkC;AAClC,QAAM,WAAW,SAAS,EAAE;AAE5B,QAAM,YAAY,QAAQ,MAAM,SAAS,UAAU,IAAI,GAAG,CAAC,UAAU,IAAI,CAAC;AAE1E,YAAU,MAAM;AACd,WAAO,MAAM;AACX,gBAAU,OAAO;AAAA,IACnB;AAAA,EACF,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO;AACT","sourcesContent":["import { type Cancelable, debounce } from '@mado/utils';\nimport { useEffect, useMemo } from 'react';\n\nimport { useEvent } from './useEvent';\n\nexport function useDebounce<T extends any[], U>(\n fn: (...args: T) => U,\n wait?: number,\n): ((...args: T) => U) & Cancelable {\n const callback = useEvent(fn);\n\n const debounced = useMemo(() => debounce(callback, wait), [callback, wait]);\n\n useEffect(() => {\n return () => {\n debounced.cancel();\n };\n }, [debounced]);\n\n return debounced;\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { debounce } from '@mado/utils';
|
|
2
|
+
import { useMemo, useEffect } from 'react';
|
|
3
|
+
import { useEvent } from './useEvent.js';
|
|
4
|
+
|
|
5
|
+
// src/useDebounce.ts
|
|
6
|
+
function useDebounce(fn, wait) {
|
|
7
|
+
const callback = useEvent(fn);
|
|
8
|
+
const debounced = useMemo(() => debounce(callback, wait), [callback, wait]);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
return () => {
|
|
11
|
+
debounced.cancel();
|
|
12
|
+
};
|
|
13
|
+
}, [debounced]);
|
|
14
|
+
return debounced;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { useDebounce };
|
|
18
|
+
//# sourceMappingURL=out.js.map
|
|
19
|
+
//# sourceMappingURL=useDebounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useDebounce.ts"],"names":[],"mappings":";AAAA,SAA0B,gBAAgB;AAC1C,SAAS,WAAW,eAAe;AAEnC,SAAS,gBAAgB;AAElB,SAAS,YACd,IACA,MACkC;AAClC,QAAM,WAAW,SAAS,EAAE;AAE5B,QAAM,YAAY,QAAQ,MAAM,SAAS,UAAU,IAAI,GAAG,CAAC,UAAU,IAAI,CAAC;AAE1E,YAAU,MAAM;AACd,WAAO,MAAM;AACX,gBAAU,OAAO;AAAA,IACnB;AAAA,EACF,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO;AACT","sourcesContent":["import { type Cancelable, debounce } from '@mado/utils';\nimport { useEffect, useMemo } from 'react';\n\nimport { useEvent } from './useEvent';\n\nexport function useDebounce<T extends any[], U>(\n fn: (...args: T) => U,\n wait?: number,\n): ((...args: T) => U) & Cancelable {\n const callback = useEvent(fn);\n\n const debounced = useMemo(() => debounce(callback, wait), [callback, wait]);\n\n useEffect(() => {\n return () => {\n debounced.cancel();\n };\n }, [debounced]);\n\n return debounced;\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// src/useIsMounted.ts
|
|
6
|
+
function useIsMounted() {
|
|
7
|
+
const [isMounted, setIsMounted] = react.useState(false);
|
|
8
|
+
react.useEffect(() => {
|
|
9
|
+
setIsMounted(true);
|
|
10
|
+
}, []);
|
|
11
|
+
return isMounted;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.useIsMounted = useIsMounted;
|
|
15
|
+
//# sourceMappingURL=out.js.map
|
|
16
|
+
//# sourceMappingURL=useIsMounted.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useIsMounted.ts"],"names":[],"mappings":";AAAA,SAAS,WAAW,gBAAgB;AAE7B,SAAS,eAAwB;AACtC,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAEhD,YAAU,MAAM;AACd,iBAAa,IAAI;AAAA,EACnB,GAAG,CAAC,CAAC;AAEL,SAAO;AACT","sourcesContent":["import { useEffect, useState } from 'react';\n\nexport function useIsMounted(): boolean {\n const [isMounted, setIsMounted] = useState(false);\n\n useEffect(() => {\n setIsMounted(true);\n }, []);\n\n return isMounted;\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/useIsMounted.ts
|
|
4
|
+
function useIsMounted() {
|
|
5
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
setIsMounted(true);
|
|
8
|
+
}, []);
|
|
9
|
+
return isMounted;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { useIsMounted };
|
|
13
|
+
//# sourceMappingURL=out.js.map
|
|
14
|
+
//# sourceMappingURL=useIsMounted.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useIsMounted.ts"],"names":[],"mappings":";AAAA,SAAS,WAAW,gBAAgB;AAE7B,SAAS,eAAwB;AACtC,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAEhD,YAAU,MAAM;AACd,iBAAa,IAAI;AAAA,EACnB,GAAG,CAAC,CAAC;AAEL,SAAO;AACT","sourcesContent":["import { useEffect, useState } from 'react';\n\nexport function useIsMounted(): boolean {\n const [isMounted, setIsMounted] = useState(false);\n\n useEffect(() => {\n setIsMounted(true);\n }, []);\n\n return isMounted;\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// src/useOnline.ts
|
|
6
|
+
function subscribe(onStoreChange) {
|
|
7
|
+
window.addEventListener("online", onStoreChange, {
|
|
8
|
+
passive: true
|
|
9
|
+
});
|
|
10
|
+
window.addEventListener("offline", onStoreChange, {
|
|
11
|
+
passive: true
|
|
12
|
+
});
|
|
13
|
+
return () => {
|
|
14
|
+
window.removeEventListener("online", onStoreChange);
|
|
15
|
+
window.removeEventListener("offline", onStoreChange);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function getSnapshot() {
|
|
19
|
+
return navigator.onLine;
|
|
20
|
+
}
|
|
21
|
+
function getServerSnapshot() {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function useOnline() {
|
|
25
|
+
return react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
26
|
+
}
|
|
27
|
+
/* istanbul ignore next -- @preserve */
|
|
28
|
+
|
|
29
|
+
exports.useOnline = useOnline;
|
|
30
|
+
//# sourceMappingURL=out.js.map
|
|
31
|
+
//# sourceMappingURL=useOnline.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useOnline.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,SAAO,iBAAiB,UAAU,eAAe;AAAA,IAC/C,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,WAAW,eAAe;AAAA,IAChD,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,WAAO,oBAAoB,UAAU,aAAa;AAClD,WAAO,oBAAoB,WAAW,aAAa;AAAA,EACrD;AACF;AAEA,SAAS,cAAuB;AAC9B,SAAO,UAAU;AACnB;AAGA,SAAS,oBAA6B;AACpC,SAAO;AACT;AAEO,SAAS,YAAqB;AACnC,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'react';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n window.addEventListener('online', onStoreChange, {\n passive: true,\n });\n window.addEventListener('offline', onStoreChange, {\n passive: true,\n });\n\n return () => {\n window.removeEventListener('online', onStoreChange);\n window.removeEventListener('offline', onStoreChange);\n };\n}\n\nfunction getSnapshot(): boolean {\n return navigator.onLine;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): boolean {\n return true;\n}\n\nexport function useOnline(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/useOnline.ts
|
|
4
|
+
function subscribe(onStoreChange) {
|
|
5
|
+
window.addEventListener("online", onStoreChange, {
|
|
6
|
+
passive: true
|
|
7
|
+
});
|
|
8
|
+
window.addEventListener("offline", onStoreChange, {
|
|
9
|
+
passive: true
|
|
10
|
+
});
|
|
11
|
+
return () => {
|
|
12
|
+
window.removeEventListener("online", onStoreChange);
|
|
13
|
+
window.removeEventListener("offline", onStoreChange);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function getSnapshot() {
|
|
17
|
+
return navigator.onLine;
|
|
18
|
+
}
|
|
19
|
+
function getServerSnapshot() {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
function useOnline() {
|
|
23
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
24
|
+
}
|
|
25
|
+
/* istanbul ignore next -- @preserve */
|
|
26
|
+
|
|
27
|
+
export { useOnline };
|
|
28
|
+
//# sourceMappingURL=out.js.map
|
|
29
|
+
//# sourceMappingURL=useOnline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useOnline.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,SAAO,iBAAiB,UAAU,eAAe;AAAA,IAC/C,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,WAAW,eAAe;AAAA,IAChD,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,WAAO,oBAAoB,UAAU,aAAa;AAClD,WAAO,oBAAoB,WAAW,aAAa;AAAA,EACrD;AACF;AAEA,SAAS,cAAuB;AAC9B,SAAO,UAAU;AACnB;AAGA,SAAS,oBAA6B;AACpC,SAAO;AACT;AAEO,SAAS,YAAqB;AACnC,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'react';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n window.addEventListener('online', onStoreChange, {\n passive: true,\n });\n window.addEventListener('offline', onStoreChange, {\n passive: true,\n });\n\n return () => {\n window.removeEventListener('online', onStoreChange);\n window.removeEventListener('offline', onStoreChange);\n };\n}\n\nfunction getSnapshot(): boolean {\n return navigator.onLine;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): boolean {\n return true;\n}\n\nexport function useOnline(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
|
package/dist/useRerender.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useRerender.ts"],"names":[],"mappings":";AAAA,
|
|
1
|
+
{"version":3,"sources":["../src/useRerender.ts"],"names":[],"mappings":";AAAA,SAAqC,kBAAkB;AAEvD,IAAM,eAAe,CAAC;AAEtB,SAAS,UAAU;AACjB,SAAO,CAAC;AACV;AAEO,SAAS,cAAqC;AACnD,SAAO,WAAW,SAAS,YAAY,EAAE,CAAC;AAC5C","sourcesContent":["import { type DispatchWithoutAction, useReducer } from 'react';\n\nconst initialState = {};\n\nfunction reducer() {\n return {};\n}\n\nexport function useRerender(): DispatchWithoutAction {\n return useReducer(reducer, initialState)[1];\n}\n"]}
|
package/dist/useRerender.d.cts
CHANGED
package/dist/useRerender.d.ts
CHANGED
package/dist/useRerender.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useRerender.ts"],"names":[],"mappings":";AAAA,
|
|
1
|
+
{"version":3,"sources":["../src/useRerender.ts"],"names":[],"mappings":";AAAA,SAAqC,kBAAkB;AAEvD,IAAM,eAAe,CAAC;AAEtB,SAAS,UAAU;AACjB,SAAO,CAAC;AACV;AAEO,SAAS,cAAqC;AACnD,SAAO,WAAW,SAAS,YAAY,EAAE,CAAC;AAC5C","sourcesContent":["import { type DispatchWithoutAction, useReducer } from 'react';\n\nconst initialState = {};\n\nfunction reducer() {\n return {};\n}\n\nexport function useRerender(): DispatchWithoutAction {\n return useReducer(reducer, initialState)[1];\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// src/useVisibilityState.ts
|
|
6
|
+
function subscribe(onStoreChange) {
|
|
7
|
+
document.addEventListener("visibilitychange", onStoreChange, {
|
|
8
|
+
passive: true
|
|
9
|
+
});
|
|
10
|
+
return () => {
|
|
11
|
+
document.removeEventListener("visibilitychange", onStoreChange);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function getSnapshot() {
|
|
15
|
+
return document.visibilityState;
|
|
16
|
+
}
|
|
17
|
+
function getServerSnapshot() {
|
|
18
|
+
return "visible";
|
|
19
|
+
}
|
|
20
|
+
function useVisibilityState() {
|
|
21
|
+
return react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
22
|
+
}
|
|
23
|
+
/* istanbul ignore next -- @preserve */
|
|
24
|
+
|
|
25
|
+
exports.useVisibilityState = useVisibilityState;
|
|
26
|
+
//# sourceMappingURL=out.js.map
|
|
27
|
+
//# sourceMappingURL=useVisibilityState.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useVisibilityState.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,WAAS,iBAAiB,oBAAoB,eAAe;AAAA,IAC3D,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,aAAS,oBAAoB,oBAAoB,aAAa;AAAA,EAChE;AACF;AAEA,SAAS,cAAuC;AAC9C,SAAO,SAAS;AAClB;AAGA,SAAS,oBAA6C;AACpD,SAAO;AACT;AAEO,SAAS,qBAA8C;AAC5D,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'react';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n document.addEventListener('visibilitychange', onStoreChange, {\n passive: true,\n });\n\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n };\n}\n\nfunction getSnapshot(): DocumentVisibilityState {\n return document.visibilityState;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): DocumentVisibilityState {\n return 'visible';\n}\n\nexport function useVisibilityState(): DocumentVisibilityState {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/useVisibilityState.ts
|
|
4
|
+
function subscribe(onStoreChange) {
|
|
5
|
+
document.addEventListener("visibilitychange", onStoreChange, {
|
|
6
|
+
passive: true
|
|
7
|
+
});
|
|
8
|
+
return () => {
|
|
9
|
+
document.removeEventListener("visibilitychange", onStoreChange);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function getSnapshot() {
|
|
13
|
+
return document.visibilityState;
|
|
14
|
+
}
|
|
15
|
+
function getServerSnapshot() {
|
|
16
|
+
return "visible";
|
|
17
|
+
}
|
|
18
|
+
function useVisibilityState() {
|
|
19
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
20
|
+
}
|
|
21
|
+
/* istanbul ignore next -- @preserve */
|
|
22
|
+
|
|
23
|
+
export { useVisibilityState };
|
|
24
|
+
//# sourceMappingURL=out.js.map
|
|
25
|
+
//# sourceMappingURL=useVisibilityState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useVisibilityState.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,WAAS,iBAAiB,oBAAoB,eAAe;AAAA,IAC3D,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,aAAS,oBAAoB,oBAAoB,aAAa;AAAA,EAChE;AACF;AAEA,SAAS,cAAuC;AAC9C,SAAO,SAAS;AAClB;AAGA,SAAS,oBAA6C;AACpD,SAAO;AACT;AAEO,SAAS,qBAA8C;AAC5D,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'react';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n document.addEventListener('visibilitychange', onStoreChange, {\n passive: true,\n });\n\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n };\n}\n\nfunction getSnapshot(): DocumentVisibilityState {\n return document.visibilityState;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): DocumentVisibilityState {\n return 'visible';\n}\n\nexport function useVisibilityState(): DocumentVisibilityState {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mado/hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -45,6 +45,9 @@
|
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
47
47
|
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@mado/utils": "^0.2.0"
|
|
50
|
+
},
|
|
48
51
|
"devDependencies": {
|
|
49
52
|
"react": "^18.2.0",
|
|
50
53
|
"react-dom": "^18.2.0"
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Cancelable, debounce } from '@mado/utils';
|
|
2
|
+
import { useEffect, useMemo } from 'react';
|
|
3
|
+
|
|
4
|
+
import { useEvent } from './useEvent';
|
|
5
|
+
|
|
6
|
+
export function useDebounce<T extends any[], U>(
|
|
7
|
+
fn: (...args: T) => U,
|
|
8
|
+
wait?: number,
|
|
9
|
+
): ((...args: T) => U) & Cancelable {
|
|
10
|
+
const callback = useEvent(fn);
|
|
11
|
+
|
|
12
|
+
const debounced = useMemo(() => debounce(callback, wait), [callback, wait]);
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
return () => {
|
|
16
|
+
debounced.cancel();
|
|
17
|
+
};
|
|
18
|
+
}, [debounced]);
|
|
19
|
+
|
|
20
|
+
return debounced;
|
|
21
|
+
}
|
package/src/useOnline.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
|
|
3
|
+
function subscribe(onStoreChange: VoidFunction): VoidFunction {
|
|
4
|
+
window.addEventListener('online', onStoreChange, {
|
|
5
|
+
passive: true,
|
|
6
|
+
});
|
|
7
|
+
window.addEventListener('offline', onStoreChange, {
|
|
8
|
+
passive: true,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
return () => {
|
|
12
|
+
window.removeEventListener('online', onStoreChange);
|
|
13
|
+
window.removeEventListener('offline', onStoreChange);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function getSnapshot(): boolean {
|
|
18
|
+
return navigator.onLine;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* istanbul ignore next -- @preserve */
|
|
22
|
+
function getServerSnapshot(): boolean {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useOnline(): boolean {
|
|
27
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
28
|
+
}
|
package/src/useRerender.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useReducer } from 'react';
|
|
1
|
+
import { type DispatchWithoutAction, useReducer } from 'react';
|
|
2
2
|
|
|
3
3
|
const initialState = {};
|
|
4
4
|
|
|
@@ -6,6 +6,6 @@ function reducer() {
|
|
|
6
6
|
return {};
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export function useRerender() {
|
|
9
|
+
export function useRerender(): DispatchWithoutAction {
|
|
10
10
|
return useReducer(reducer, initialState)[1];
|
|
11
11
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
|
|
3
|
+
function subscribe(onStoreChange: VoidFunction): VoidFunction {
|
|
4
|
+
document.addEventListener('visibilitychange', onStoreChange, {
|
|
5
|
+
passive: true,
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
return () => {
|
|
9
|
+
document.removeEventListener('visibilitychange', onStoreChange);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getSnapshot(): DocumentVisibilityState {
|
|
14
|
+
return document.visibilityState;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* istanbul ignore next -- @preserve */
|
|
18
|
+
function getServerSnapshot(): DocumentVisibilityState {
|
|
19
|
+
return 'visible';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useVisibilityState(): DocumentVisibilityState {
|
|
23
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
24
|
+
}
|