@mado/hooks 0.1.0 → 0.2.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @mado/hooks
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b713fbe: Add useDebounce and useIsMounted hooks
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.cjs CHANGED
@@ -1,17 +1,31 @@
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');
5
7
  var useRerender_cjs = require('./useRerender.cjs');
6
8
 
7
9
 
8
10
 
11
+ Object.keys(useDebounce_cjs).forEach(function (k) {
12
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
13
+ enumerable: true,
14
+ get: function () { return useDebounce_cjs[k]; }
15
+ });
16
+ });
9
17
  Object.keys(useEvent_cjs).forEach(function (k) {
10
18
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
11
19
  enumerable: true,
12
20
  get: function () { return useEvent_cjs[k]; }
13
21
  });
14
22
  });
23
+ Object.keys(useIsMounted_cjs).forEach(function (k) {
24
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
25
+ enumerable: true,
26
+ get: function () { return useIsMounted_cjs[k]; }
27
+ });
28
+ });
15
29
  Object.keys(useIsomorphicLayoutEffect_cjs).forEach(function (k) {
16
30
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
17
31
  enumerable: true,
@@ -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","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useRerender';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,4 +1,7 @@
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';
3
5
  export { useRerender } from './useRerender.cjs';
6
+ import '@mado/utils';
4
7
  import 'react';
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
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';
3
5
  export { useRerender } from './useRerender.js';
6
+ import '@mado/utils';
4
7
  import 'react';
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
+ export * from './useDebounce.js';
1
2
  export * from './useEvent.js';
3
+ export * from './useIsMounted.js';
2
4
  export * from './useIsomorphicLayoutEffect.js';
3
5
  export * from './useRerender.js';
4
6
  //# sourceMappingURL=out.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","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useRerender';\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,5 @@
1
+ import { Cancelable } from '@mado/utils';
2
+
3
+ declare function useDebounce<T extends any[], U>(fn: (...args: T) => U, wait?: number): ((...args: T) => U) & Cancelable;
4
+
5
+ export { useDebounce };
@@ -0,0 +1,5 @@
1
+ import { Cancelable } from '@mado/utils';
2
+
3
+ declare function useDebounce<T extends any[], U>(fn: (...args: T) => U, wait?: number): ((...args: T) => U) & Cancelable;
4
+
5
+ export { useDebounce };
@@ -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,3 @@
1
+ declare function useIsMounted(): boolean;
2
+
3
+ export { useIsMounted };
@@ -0,0 +1,3 @@
1
+ declare function useIsMounted(): boolean;
2
+
3
+ export { useIsMounted };
@@ -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"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useRerender.ts"],"names":[],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,IAAM,eAAe,CAAC;AAEtB,SAAS,UAAU;AACjB,SAAO,CAAC;AACV;AAEO,SAAS,cAAc;AAC5B,SAAO,WAAW,SAAS,YAAY,EAAE,CAAC;AAC5C","sourcesContent":["import { useReducer } from 'react';\n\nconst initialState = {};\n\nfunction reducer() {\n return {};\n}\n\nexport function useRerender() {\n return useReducer(reducer, initialState)[1];\n}\n"]}
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"]}
@@ -1,5 +1,5 @@
1
- import * as react from 'react';
1
+ import { DispatchWithoutAction } from 'react';
2
2
 
3
- declare function useRerender(): react.DispatchWithoutAction;
3
+ declare function useRerender(): DispatchWithoutAction;
4
4
 
5
5
  export { useRerender };
@@ -1,5 +1,5 @@
1
- import * as react from 'react';
1
+ import { DispatchWithoutAction } from 'react';
2
2
 
3
- declare function useRerender(): react.DispatchWithoutAction;
3
+ declare function useRerender(): DispatchWithoutAction;
4
4
 
5
5
  export { useRerender };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useRerender.ts"],"names":[],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,IAAM,eAAe,CAAC;AAEtB,SAAS,UAAU;AACjB,SAAO,CAAC;AACV;AAEO,SAAS,cAAc;AAC5B,SAAO,WAAW,SAAS,YAAY,EAAE,CAAC;AAC5C","sourcesContent":["import { useReducer } from 'react';\n\nconst initialState = {};\n\nfunction reducer() {\n return {};\n}\n\nexport function useRerender() {\n return useReducer(reducer, initialState)[1];\n}\n"]}
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mado/hooks",
3
- "version": "0.1.0",
3
+ "version": "0.2.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
@@ -1,3 +1,5 @@
1
+ export * from './useDebounce';
1
2
  export * from './useEvent';
3
+ export * from './useIsMounted';
2
4
  export * from './useIsomorphicLayoutEffect';
3
5
  export * from './useRerender';
@@ -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
+ }
@@ -0,0 +1,11 @@
1
+ import { useEffect, useState } from 'react';
2
+
3
+ export function useIsMounted(): boolean {
4
+ const [isMounted, setIsMounted] = useState(false);
5
+
6
+ useEffect(() => {
7
+ setIsMounted(true);
8
+ }, []);
9
+
10
+ return isMounted;
11
+ }
@@ -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
  }