@spark-ui/hooks 10.0.6
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 +10 -0
- package/LICENSE.md +21 -0
- package/README.md +18 -0
- package/dist/use-combined-state/index.d.mts +12 -0
- package/dist/use-combined-state/index.d.ts +12 -0
- package/dist/use-combined-state/index.js +62 -0
- package/dist/use-combined-state/index.js.map +1 -0
- package/dist/use-combined-state/index.mjs +25 -0
- package/dist/use-combined-state/index.mjs.map +1 -0
- package/dist/use-merge-refs/index.d.mts +7 -0
- package/dist/use-merge-refs/index.d.ts +7 -0
- package/dist/use-merge-refs/index.js +60 -0
- package/dist/use-merge-refs/index.js.map +1 -0
- package/dist/use-merge-refs/index.mjs +31 -0
- package/dist/use-merge-refs/index.mjs.map +1 -0
- package/dist/use-mounted-state/index.d.mts +3 -0
- package/dist/use-mounted-state/index.d.ts +3 -0
- package/dist/use-mounted-state/index.js +44 -0
- package/dist/use-mounted-state/index.js.map +1 -0
- package/dist/use-mounted-state/index.mjs +17 -0
- package/dist/use-mounted-state/index.mjs.map +1 -0
- package/package.json +53 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +11 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [10.0.6](https://github.com/leboncoin/spark-web/compare/v10.0.5...v10.0.6) (2025-03-25)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **hooks:** grouped hooks packages in a single package ([83397e1](https://github.com/leboncoin/spark-web/commit/83397e16cc8d2410abbd1d55296b4764728b47ad))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Leboncoin.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Hooks
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
> npm install @spark-ui/hooks
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
> import { useMergeRefs } from '@spark-ui/hooks/use-merge-refs'
|
|
10
|
+
|
|
11
|
+
[](https://sparkui.vercel.app/?path=/docs/hooks-usemergerefs--docs)
|
|
12
|
+
[](https://github.com/leboncoin/spark-web/issues/new?&projects=4&template=bug-report.yml&assignees=&labels=hook)
|
|
13
|
+
[](https://www.npmjs.com/package/@spark-ui/hooks)
|
|
14
|
+
|
|
15
|
+
This package is part of the [`@spark-ui`](https://github.com/leboncoin/spark-web) react-js user interface component library project.
|
|
16
|
+
|
|
17
|
+
[](https://github.com/leboncoin/spark-web/issues?q=is%3Aopen+label%3Ahook)
|
|
18
|
+
[](https://github.com/leboncoin/spark-web/blob/main/packages/hooks/LICENSE.md)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This hook must be used when a component has both a controlled and uncontrolled mode.
|
|
3
|
+
* It will take care of updating the state value when a controlled mode (prop) is updated.
|
|
4
|
+
*/
|
|
5
|
+
declare function useCombinedState<T>(controlledValue?: T, defaultValue?: T, onChange?: (nextValue: T) => void): [
|
|
6
|
+
T | undefined,
|
|
7
|
+
(newValue: T, forceFlag?: (prev: T, next: T) => boolean) => void,
|
|
8
|
+
boolean,
|
|
9
|
+
T | undefined
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export { useCombinedState };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This hook must be used when a component has both a controlled and uncontrolled mode.
|
|
3
|
+
* It will take care of updating the state value when a controlled mode (prop) is updated.
|
|
4
|
+
*/
|
|
5
|
+
declare function useCombinedState<T>(controlledValue?: T, defaultValue?: T, onChange?: (nextValue: T) => void): [
|
|
6
|
+
T | undefined,
|
|
7
|
+
(newValue: T, forceFlag?: (prev: T, next: T) => boolean) => void,
|
|
8
|
+
boolean,
|
|
9
|
+
T | undefined
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export { useCombinedState };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/use-combined-state/index.ts
|
|
31
|
+
var use_combined_state_exports = {};
|
|
32
|
+
__export(use_combined_state_exports, {
|
|
33
|
+
useCombinedState: () => useCombinedState
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(use_combined_state_exports);
|
|
36
|
+
|
|
37
|
+
// src/use-combined-state/useCombinedState.tsx
|
|
38
|
+
var import_lodash = __toESM(require("lodash.isequal"));
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
function useCombinedState(controlledValue, defaultValue, onChange) {
|
|
41
|
+
const isControlled = controlledValue !== void 0;
|
|
42
|
+
const { current: initialValue } = (0, import_react.useRef)(isControlled ? controlledValue : defaultValue);
|
|
43
|
+
const [innerValue, setInnerValue] = (0, import_react.useState)(defaultValue);
|
|
44
|
+
const value = isControlled ? controlledValue : innerValue;
|
|
45
|
+
const updater = (0, import_react.useCallback)(
|
|
46
|
+
(next, shouldUpdateProp = (prevValue, nextValue) => !(0, import_lodash.default)(prevValue, nextValue)) => {
|
|
47
|
+
const nextValue = typeof next !== "function" ? next : next(value);
|
|
48
|
+
const shouldUpdate = shouldUpdateProp(value, nextValue);
|
|
49
|
+
if (shouldUpdate && !isControlled) {
|
|
50
|
+
setInnerValue(nextValue);
|
|
51
|
+
}
|
|
52
|
+
onChange && onChange(nextValue);
|
|
53
|
+
},
|
|
54
|
+
[isControlled, value, onChange]
|
|
55
|
+
);
|
|
56
|
+
return [value, updater, isControlled, initialValue];
|
|
57
|
+
}
|
|
58
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
+
0 && (module.exports = {
|
|
60
|
+
useCombinedState
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-combined-state/index.ts","../../src/use-combined-state/useCombinedState.tsx"],"sourcesContent":["export { useCombinedState } from './useCombinedState'\n","import isEqual from 'lodash.isequal'\nimport { SetStateAction, useCallback, useRef, useState } from 'react'\n\n/**\n * This hook must be used when a component has both a controlled and uncontrolled mode.\n * It will take care of updating the state value when a controlled mode (prop) is updated.\n */\nexport function useCombinedState<T>(\n controlledValue?: T,\n defaultValue?: T,\n onChange?: (nextValue: T) => void\n): [\n T | undefined,\n (newValue: T, forceFlag?: (prev: T, next: T) => boolean) => void,\n boolean,\n T | undefined,\n] {\n const isControlled = controlledValue !== undefined\n const { current: initialValue } = useRef(isControlled ? controlledValue : defaultValue)\n\n const [innerValue, setInnerValue] = useState(defaultValue as T)\n const value = isControlled ? (controlledValue as T) : innerValue\n\n const updater = useCallback(\n (\n next: SetStateAction<T>,\n shouldUpdateProp = (prevValue: T, nextValue: T) => !isEqual(prevValue, nextValue)\n ) => {\n const nextValue = typeof next !== 'function' ? next : (next as (value: T) => T)(value)\n const shouldUpdate = shouldUpdateProp(value, nextValue)\n if (shouldUpdate && !isControlled) {\n setInnerValue(nextValue)\n }\n onChange && onChange(nextValue)\n },\n [isControlled, value, onChange]\n )\n\n return [value, updater, isControlled, initialValue]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAoB;AACpB,mBAA8D;AAMvD,SAAS,iBACd,iBACA,cACA,UAMA;AACA,QAAM,eAAe,oBAAoB;AACzC,QAAM,EAAE,SAAS,aAAa,QAAI,qBAAO,eAAe,kBAAkB,YAAY;AAEtF,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,YAAiB;AAC9D,QAAM,QAAQ,eAAgB,kBAAwB;AAEtD,QAAM,cAAU;AAAA,IACd,CACE,MACA,mBAAmB,CAAC,WAAc,cAAiB,KAAC,cAAAA,SAAQ,WAAW,SAAS,MAC7E;AACH,YAAM,YAAY,OAAO,SAAS,aAAa,OAAQ,KAAyB,KAAK;AACrF,YAAM,eAAe,iBAAiB,OAAO,SAAS;AACtD,UAAI,gBAAgB,CAAC,cAAc;AACjC,sBAAc,SAAS;AAAA,MACzB;AACA,kBAAY,SAAS,SAAS;AAAA,IAChC;AAAA,IACA,CAAC,cAAc,OAAO,QAAQ;AAAA,EAChC;AAEA,SAAO,CAAC,OAAO,SAAS,cAAc,YAAY;AACpD;","names":["isEqual"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/use-combined-state/useCombinedState.tsx
|
|
2
|
+
import isEqual from "lodash.isequal";
|
|
3
|
+
import { useCallback, useRef, useState } from "react";
|
|
4
|
+
function useCombinedState(controlledValue, defaultValue, onChange) {
|
|
5
|
+
const isControlled = controlledValue !== void 0;
|
|
6
|
+
const { current: initialValue } = useRef(isControlled ? controlledValue : defaultValue);
|
|
7
|
+
const [innerValue, setInnerValue] = useState(defaultValue);
|
|
8
|
+
const value = isControlled ? controlledValue : innerValue;
|
|
9
|
+
const updater = useCallback(
|
|
10
|
+
(next, shouldUpdateProp = (prevValue, nextValue) => !isEqual(prevValue, nextValue)) => {
|
|
11
|
+
const nextValue = typeof next !== "function" ? next : next(value);
|
|
12
|
+
const shouldUpdate = shouldUpdateProp(value, nextValue);
|
|
13
|
+
if (shouldUpdate && !isControlled) {
|
|
14
|
+
setInnerValue(nextValue);
|
|
15
|
+
}
|
|
16
|
+
onChange && onChange(nextValue);
|
|
17
|
+
},
|
|
18
|
+
[isControlled, value, onChange]
|
|
19
|
+
);
|
|
20
|
+
return [value, updater, isControlled, initialValue];
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
useCombinedState
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-combined-state/useCombinedState.tsx"],"sourcesContent":["import isEqual from 'lodash.isequal'\nimport { SetStateAction, useCallback, useRef, useState } from 'react'\n\n/**\n * This hook must be used when a component has both a controlled and uncontrolled mode.\n * It will take care of updating the state value when a controlled mode (prop) is updated.\n */\nexport function useCombinedState<T>(\n controlledValue?: T,\n defaultValue?: T,\n onChange?: (nextValue: T) => void\n): [\n T | undefined,\n (newValue: T, forceFlag?: (prev: T, next: T) => boolean) => void,\n boolean,\n T | undefined,\n] {\n const isControlled = controlledValue !== undefined\n const { current: initialValue } = useRef(isControlled ? controlledValue : defaultValue)\n\n const [innerValue, setInnerValue] = useState(defaultValue as T)\n const value = isControlled ? (controlledValue as T) : innerValue\n\n const updater = useCallback(\n (\n next: SetStateAction<T>,\n shouldUpdateProp = (prevValue: T, nextValue: T) => !isEqual(prevValue, nextValue)\n ) => {\n const nextValue = typeof next !== 'function' ? next : (next as (value: T) => T)(value)\n const shouldUpdate = shouldUpdateProp(value, nextValue)\n if (shouldUpdate && !isControlled) {\n setInnerValue(nextValue)\n }\n onChange && onChange(nextValue)\n },\n [isControlled, value, onChange]\n )\n\n return [value, updater, isControlled, initialValue]\n}\n"],"mappings":";AAAA,OAAO,aAAa;AACpB,SAAyB,aAAa,QAAQ,gBAAgB;AAMvD,SAAS,iBACd,iBACA,cACA,UAMA;AACA,QAAM,eAAe,oBAAoB;AACzC,QAAM,EAAE,SAAS,aAAa,IAAI,OAAO,eAAe,kBAAkB,YAAY;AAEtF,QAAM,CAAC,YAAY,aAAa,IAAI,SAAS,YAAiB;AAC9D,QAAM,QAAQ,eAAgB,kBAAwB;AAEtD,QAAM,UAAU;AAAA,IACd,CACE,MACA,mBAAmB,CAAC,WAAc,cAAiB,CAAC,QAAQ,WAAW,SAAS,MAC7E;AACH,YAAM,YAAY,OAAO,SAAS,aAAa,OAAQ,KAAyB,KAAK;AACrF,YAAM,eAAe,iBAAiB,OAAO,SAAS;AACtD,UAAI,gBAAgB,CAAC,cAAc;AACjC,sBAAc,SAAS;AAAA,MACzB;AACA,kBAAY,SAAS,SAAS;AAAA,IAChC;AAAA,IACA,CAAC,cAAc,OAAO,QAAQ;AAAA,EAChC;AAEA,SAAO,CAAC,OAAO,SAAS,cAAc,YAAY;AACpD;","names":[]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ref, RefCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
declare function assignRef<T>(ref: Ref<T> | null | undefined, value: T): void;
|
|
4
|
+
declare function mergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
|
|
5
|
+
declare function useMergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
|
|
6
|
+
|
|
7
|
+
export { assignRef, mergeRefs, useMergeRefs };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ref, RefCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
declare function assignRef<T>(ref: Ref<T> | null | undefined, value: T): void;
|
|
4
|
+
declare function mergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
|
|
5
|
+
declare function useMergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
|
|
6
|
+
|
|
7
|
+
export { assignRef, mergeRefs, useMergeRefs };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/use-merge-refs/index.ts
|
|
21
|
+
var use_merge_refs_exports = {};
|
|
22
|
+
__export(use_merge_refs_exports, {
|
|
23
|
+
assignRef: () => assignRef,
|
|
24
|
+
mergeRefs: () => mergeRefs,
|
|
25
|
+
useMergeRefs: () => useMergeRefs
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(use_merge_refs_exports);
|
|
28
|
+
|
|
29
|
+
// src/use-merge-refs/useMergeRefs.tsx
|
|
30
|
+
var import_react = require("react");
|
|
31
|
+
function assignRef(ref, value) {
|
|
32
|
+
if (ref == null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (typeof ref === "function") {
|
|
36
|
+
ref(value);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
;
|
|
41
|
+
ref.current = value;
|
|
42
|
+
} catch {
|
|
43
|
+
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function mergeRefs(...refs) {
|
|
47
|
+
return (value) => {
|
|
48
|
+
refs.forEach((ref) => assignRef(ref, value));
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function useMergeRefs(...refs) {
|
|
52
|
+
return (0, import_react.useMemo)(() => mergeRefs(...refs), refs);
|
|
53
|
+
}
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
assignRef,
|
|
57
|
+
mergeRefs,
|
|
58
|
+
useMergeRefs
|
|
59
|
+
});
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-merge-refs/index.ts","../../src/use-merge-refs/useMergeRefs.tsx"],"sourcesContent":["export { useMergeRefs, assignRef, mergeRefs } from './useMergeRefs'\n","import { Ref, RefCallback, RefObject, useMemo } from 'react'\n\nexport function assignRef<T>(ref: Ref<T> | null | undefined, value: T) {\n if (ref == null) {\n return\n }\n\n if (typeof ref === 'function') {\n ref(value)\n\n return\n }\n\n try {\n ;(ref as RefObject<T | null>).current = value\n } catch {\n throw new Error(`Cannot assign value '${value}' to ref '${ref}'`)\n }\n}\n\nexport function mergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T> {\n return value => {\n refs.forEach(ref => assignRef(ref, value))\n }\n}\n\nexport function useMergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return useMemo(() => mergeRefs(...refs), refs)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAqD;AAE9C,SAAS,UAAa,KAAgC,OAAU;AACrE,MAAI,OAAO,MAAM;AACf;AAAA,EACF;AAEA,MAAI,OAAO,QAAQ,YAAY;AAC7B,QAAI,KAAK;AAET;AAAA,EACF;AAEA,MAAI;AACF;AAAC,IAAC,IAA4B,UAAU;AAAA,EAC1C,QAAQ;AACN,UAAM,IAAI,MAAM,wBAAwB,KAAK,aAAa,GAAG,GAAG;AAAA,EAClE;AACF;AAEO,SAAS,aAAgB,MAA8C;AAC5E,SAAO,WAAS;AACd,SAAK,QAAQ,SAAO,UAAU,KAAK,KAAK,CAAC;AAAA,EAC3C;AACF;AAEO,SAAS,gBAAmB,MAA8B;AAE/D,aAAO,sBAAQ,MAAM,UAAU,GAAG,IAAI,GAAG,IAAI;AAC/C;","names":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/use-merge-refs/useMergeRefs.tsx
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
function assignRef(ref, value) {
|
|
4
|
+
if (ref == null) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
if (typeof ref === "function") {
|
|
8
|
+
ref(value);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
;
|
|
13
|
+
ref.current = value;
|
|
14
|
+
} catch {
|
|
15
|
+
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function mergeRefs(...refs) {
|
|
19
|
+
return (value) => {
|
|
20
|
+
refs.forEach((ref) => assignRef(ref, value));
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function useMergeRefs(...refs) {
|
|
24
|
+
return useMemo(() => mergeRefs(...refs), refs);
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
assignRef,
|
|
28
|
+
mergeRefs,
|
|
29
|
+
useMergeRefs
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-merge-refs/useMergeRefs.tsx"],"sourcesContent":["import { Ref, RefCallback, RefObject, useMemo } from 'react'\n\nexport function assignRef<T>(ref: Ref<T> | null | undefined, value: T) {\n if (ref == null) {\n return\n }\n\n if (typeof ref === 'function') {\n ref(value)\n\n return\n }\n\n try {\n ;(ref as RefObject<T | null>).current = value\n } catch {\n throw new Error(`Cannot assign value '${value}' to ref '${ref}'`)\n }\n}\n\nexport function mergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T> {\n return value => {\n refs.forEach(ref => assignRef(ref, value))\n }\n}\n\nexport function useMergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return useMemo(() => mergeRefs(...refs), refs)\n}\n"],"mappings":";AAAA,SAAsC,eAAe;AAE9C,SAAS,UAAa,KAAgC,OAAU;AACrE,MAAI,OAAO,MAAM;AACf;AAAA,EACF;AAEA,MAAI,OAAO,QAAQ,YAAY;AAC7B,QAAI,KAAK;AAET;AAAA,EACF;AAEA,MAAI;AACF;AAAC,IAAC,IAA4B,UAAU;AAAA,EAC1C,QAAQ;AACN,UAAM,IAAI,MAAM,wBAAwB,KAAK,aAAa,GAAG,GAAG;AAAA,EAClE;AACF;AAEO,SAAS,aAAgB,MAA8C;AAC5E,SAAO,WAAS;AACd,SAAK,QAAQ,SAAO,UAAU,KAAK,KAAK,CAAC;AAAA,EAC3C;AACF;AAEO,SAAS,gBAAmB,MAA8B;AAE/D,SAAO,QAAQ,MAAM,UAAU,GAAG,IAAI,GAAG,IAAI;AAC/C;","names":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/use-mounted-state/index.ts
|
|
21
|
+
var use_mounted_state_exports = {};
|
|
22
|
+
__export(use_mounted_state_exports, {
|
|
23
|
+
useMountedState: () => useMountedState
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_mounted_state_exports);
|
|
26
|
+
|
|
27
|
+
// src/use-mounted-state/useMountedState.tsx
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
function useMountedState() {
|
|
30
|
+
const mountedRef = (0, import_react.useRef)(false);
|
|
31
|
+
const get = (0, import_react.useCallback)(() => mountedRef.current, []);
|
|
32
|
+
(0, import_react.useEffect)(() => {
|
|
33
|
+
mountedRef.current = true;
|
|
34
|
+
return () => {
|
|
35
|
+
mountedRef.current = false;
|
|
36
|
+
};
|
|
37
|
+
}, []);
|
|
38
|
+
return get;
|
|
39
|
+
}
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
useMountedState
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-mounted-state/index.ts","../../src/use-mounted-state/useMountedState.tsx"],"sourcesContent":["export { useMountedState } from './useMountedState'\n","import { useCallback, useEffect, useRef } from 'react'\n\nexport function useMountedState(): () => boolean {\n const mountedRef = useRef(false)\n const get = useCallback(() => mountedRef.current, [])\n\n useEffect(() => {\n mountedRef.current = true\n\n return () => {\n mountedRef.current = false\n }\n }, [])\n\n return get\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA+C;AAExC,SAAS,kBAAiC;AAC/C,QAAM,iBAAa,qBAAO,KAAK;AAC/B,QAAM,UAAM,0BAAY,MAAM,WAAW,SAAS,CAAC,CAAC;AAEpD,8BAAU,MAAM;AACd,eAAW,UAAU;AAErB,WAAO,MAAM;AACX,iBAAW,UAAU;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/use-mounted-state/useMountedState.tsx
|
|
2
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
3
|
+
function useMountedState() {
|
|
4
|
+
const mountedRef = useRef(false);
|
|
5
|
+
const get = useCallback(() => mountedRef.current, []);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
mountedRef.current = true;
|
|
8
|
+
return () => {
|
|
9
|
+
mountedRef.current = false;
|
|
10
|
+
};
|
|
11
|
+
}, []);
|
|
12
|
+
return get;
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
useMountedState
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-mounted-state/useMountedState.tsx"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react'\n\nexport function useMountedState(): () => boolean {\n const mountedRef = useRef(false)\n const get = useCallback(() => mountedRef.current, [])\n\n useEffect(() => {\n mountedRef.current = true\n\n return () => {\n mountedRef.current = false\n }\n }, [])\n\n return get\n}\n"],"mappings":";AAAA,SAAS,aAAa,WAAW,cAAc;AAExC,SAAS,kBAAiC;AAC/C,QAAM,aAAa,OAAO,KAAK;AAC/B,QAAM,MAAM,YAAY,MAAM,WAAW,SAAS,CAAC,CAAC;AAEpD,YAAU,MAAM;AACd,eAAW,UAAU;AAErB,WAAO,MAAM;AACX,iBAAW,UAAU;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spark-ui/hooks",
|
|
3
|
+
"version": "10.0.6",
|
|
4
|
+
"description": "Common hooks for Spark UI",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./*": {
|
|
7
|
+
"types": "./dist/*/index.d.ts",
|
|
8
|
+
"import": "./dist/*/index.mjs",
|
|
9
|
+
"require": "./dist/*/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"typesVersions": {
|
|
13
|
+
"*": {
|
|
14
|
+
"*": [
|
|
15
|
+
"./dist/*/index.d.ts"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"@spark-ui",
|
|
24
|
+
"react",
|
|
25
|
+
"hook"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@types/lodash.isequal": "4.5.8",
|
|
32
|
+
"lodash.isequal": "4.5.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^19.0",
|
|
36
|
+
"react-dom": "^19.0"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/leboncoin/spark-web.git",
|
|
41
|
+
"directory": "packages/hooks"
|
|
42
|
+
},
|
|
43
|
+
"config": {
|
|
44
|
+
"title": "use-merge-refs",
|
|
45
|
+
"category": "hooks"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/leboncoin/spark-web/issues?q=is%3Aopen+label%3Ahooks"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://sparkui.vercel.app",
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"gitHead": "368c1c6bf209978dfd8ca660edb8250b545846df"
|
|
53
|
+
}
|
package/tsconfig.json
ADDED