@mado/hooks 0.0.1 → 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,17 @@
1
1
  # @mado/hooks
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b713fbe: Add useDebounce and useIsMounted hooks
8
+
9
+ ## 0.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - ff6ac1d: Add useEvent and useRerender hooks
14
+
3
15
  ## 0.0.1
4
16
 
5
17
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -1,14 +1,42 @@
1
1
  'use strict';
2
2
 
3
+ var useDebounce_cjs = require('./useDebounce.cjs');
4
+ var useEvent_cjs = require('./useEvent.cjs');
5
+ var useIsMounted_cjs = require('./useIsMounted.cjs');
3
6
  var useIsomorphicLayoutEffect_cjs = require('./useIsomorphicLayoutEffect.cjs');
7
+ var useRerender_cjs = require('./useRerender.cjs');
4
8
 
5
9
 
6
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
+ });
17
+ Object.keys(useEvent_cjs).forEach(function (k) {
18
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
19
+ enumerable: true,
20
+ get: function () { return useEvent_cjs[k]; }
21
+ });
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
+ });
7
29
  Object.keys(useIsomorphicLayoutEffect_cjs).forEach(function (k) {
8
30
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
9
31
  enumerable: true,
10
32
  get: function () { return useIsomorphicLayoutEffect_cjs[k]; }
11
33
  });
12
34
  });
35
+ Object.keys(useRerender_cjs).forEach(function (k) {
36
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
37
+ enumerable: true,
38
+ get: function () { return useRerender_cjs[k]; }
39
+ });
40
+ });
13
41
  //# sourceMappingURL=out.js.map
14
42
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc","sourcesContent":["export * from './useIsomorphicLayoutEffect';\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,2 +1,7 @@
1
+ export { useDebounce } from './useDebounce.cjs';
2
+ export { useEvent } from './useEvent.cjs';
3
+ export { useIsMounted } from './useIsMounted.cjs';
1
4
  export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.cjs';
5
+ export { useRerender } from './useRerender.cjs';
6
+ import '@mado/utils';
2
7
  import 'react';
package/dist/index.d.ts CHANGED
@@ -1,2 +1,7 @@
1
+ export { useDebounce } from './useDebounce.js';
2
+ export { useEvent } from './useEvent.js';
3
+ export { useIsMounted } from './useIsMounted.js';
1
4
  export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.js';
5
+ export { useRerender } from './useRerender.js';
6
+ import '@mado/utils';
2
7
  import 'react';
package/dist/index.js CHANGED
@@ -1,3 +1,7 @@
1
+ export * from './useDebounce.js';
2
+ export * from './useEvent.js';
3
+ export * from './useIsMounted.js';
1
4
  export * from './useIsomorphicLayoutEffect.js';
5
+ export * from './useRerender.js';
2
6
  //# sourceMappingURL=out.js.map
3
7
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc","sourcesContent":["export * from './useIsomorphicLayoutEffect';\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,17 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var useIsomorphicLayoutEffect_cjs = require('./useIsomorphicLayoutEffect.cjs');
5
+
6
+ // src/useEvent.ts
7
+ function useEvent(fn) {
8
+ const ref = react.useRef(fn);
9
+ useIsomorphicLayoutEffect_cjs.useIsomorphicLayoutEffect(() => {
10
+ ref.current = fn;
11
+ });
12
+ return react.useRef((...args) => (0, ref.current)(...args)).current;
13
+ }
14
+
15
+ exports.useEvent = useEvent;
16
+ //# sourceMappingURL=out.js.map
17
+ //# sourceMappingURL=useEvent.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useEvent.ts"],"names":[],"mappings":";AAAA,SAAS,cAAc;AAEvB,SAAS,iCAAiC;AAEnC,SAAS,SACd,IACmB;AACnB,QAAM,MAAM,OAAO,EAAE;AAErB,4BAA0B,MAAM;AAC9B,QAAI,UAAU;AAAA,EAChB,CAAC;AAED,SAAO,OAAO,IAAI,UAAa,GAAG,IAAI,SAAS,GAAG,IAAI,CAAC,EAAE;AAC3D","sourcesContent":["import { useRef } from 'react';\n\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';\n\nexport function useEvent<T extends unknown[], U>(\n fn: (...args: T) => U,\n): (...args: T) => U {\n const ref = useRef(fn);\n\n useIsomorphicLayoutEffect(() => {\n ref.current = fn;\n });\n\n return useRef((...args: T) => (0, ref.current)(...args)).current;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ declare function useEvent<T extends unknown[], U>(fn: (...args: T) => U): (...args: T) => U;
2
+
3
+ export { useEvent };
@@ -0,0 +1,3 @@
1
+ declare function useEvent<T extends unknown[], U>(fn: (...args: T) => U): (...args: T) => U;
2
+
3
+ export { useEvent };
@@ -0,0 +1,15 @@
1
+ import { useRef } from 'react';
2
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.js';
3
+
4
+ // src/useEvent.ts
5
+ function useEvent(fn) {
6
+ const ref = useRef(fn);
7
+ useIsomorphicLayoutEffect(() => {
8
+ ref.current = fn;
9
+ });
10
+ return useRef((...args) => (0, ref.current)(...args)).current;
11
+ }
12
+
13
+ export { useEvent };
14
+ //# sourceMappingURL=out.js.map
15
+ //# sourceMappingURL=useEvent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useEvent.ts"],"names":[],"mappings":";AAAA,SAAS,cAAc;AAEvB,SAAS,iCAAiC;AAEnC,SAAS,SACd,IACmB;AACnB,QAAM,MAAM,OAAO,EAAE;AAErB,4BAA0B,MAAM;AAC9B,QAAI,UAAU;AAAA,EAChB,CAAC;AAED,SAAO,OAAO,IAAI,UAAa,GAAG,IAAI,SAAS,GAAG,IAAI,CAAC,EAAE;AAC3D","sourcesContent":["import { useRef } from 'react';\n\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';\n\nexport function useEvent<T extends unknown[], U>(\n fn: (...args: T) => U,\n): (...args: T) => U {\n const ref = useRef(fn);\n\n useIsomorphicLayoutEffect(() => {\n ref.current = fn;\n });\n\n return useRef((...args: T) => (0, ref.current)(...args)).current;\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"]}
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ // src/useRerender.ts
6
+ var initialState = {};
7
+ function reducer() {
8
+ return {};
9
+ }
10
+ function useRerender() {
11
+ return react.useReducer(reducer, initialState)[1];
12
+ }
13
+
14
+ exports.useRerender = useRerender;
15
+ //# sourceMappingURL=out.js.map
16
+ //# sourceMappingURL=useRerender.cjs.map
@@ -0,0 +1 @@
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,5 @@
1
+ import { DispatchWithoutAction } from 'react';
2
+
3
+ declare function useRerender(): DispatchWithoutAction;
4
+
5
+ export { useRerender };
@@ -0,0 +1,5 @@
1
+ import { DispatchWithoutAction } from 'react';
2
+
3
+ declare function useRerender(): DispatchWithoutAction;
4
+
5
+ export { useRerender };
@@ -0,0 +1,14 @@
1
+ import { useReducer } from 'react';
2
+
3
+ // src/useRerender.ts
4
+ var initialState = {};
5
+ function reducer() {
6
+ return {};
7
+ }
8
+ function useRerender() {
9
+ return useReducer(reducer, initialState)[1];
10
+ }
11
+
12
+ export { useRerender };
13
+ //# sourceMappingURL=out.js.map
14
+ //# sourceMappingURL=useRerender.js.map
@@ -0,0 +1 @@
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.0.1",
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 +1,5 @@
1
+ export * from './useDebounce';
2
+ export * from './useEvent';
3
+ export * from './useIsMounted';
1
4
  export * from './useIsomorphicLayoutEffect';
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,15 @@
1
+ import { useRef } from 'react';
2
+
3
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
4
+
5
+ export function useEvent<T extends unknown[], U>(
6
+ fn: (...args: T) => U,
7
+ ): (...args: T) => U {
8
+ const ref = useRef(fn);
9
+
10
+ useIsomorphicLayoutEffect(() => {
11
+ ref.current = fn;
12
+ });
13
+
14
+ return useRef((...args: T) => (0, ref.current)(...args)).current;
15
+ }
@@ -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
+ }
@@ -0,0 +1,11 @@
1
+ import { type DispatchWithoutAction, useReducer } from 'react';
2
+
3
+ const initialState = {};
4
+
5
+ function reducer() {
6
+ return {};
7
+ }
8
+
9
+ export function useRerender(): DispatchWithoutAction {
10
+ return useReducer(reducer, initialState)[1];
11
+ }