@kopexa/react-utils 0.0.0 → 1.0.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/dist/context.d.ts +16 -0
- package/dist/context.js +58 -0
- package/dist/dom.d.ts +17 -0
- package/dist/dom.js +45 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +53 -3
- package/package.json +3 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface CreateContextOptions<T> {
|
|
2
|
+
strict?: boolean;
|
|
3
|
+
hookName?: string;
|
|
4
|
+
providerName?: string;
|
|
5
|
+
errorMessage?: string;
|
|
6
|
+
name?: string;
|
|
7
|
+
defaultValue?: T;
|
|
8
|
+
}
|
|
9
|
+
type CreateContextReturn<T> = [
|
|
10
|
+
React.Provider<T>,
|
|
11
|
+
() => T,
|
|
12
|
+
React.Context<T>
|
|
13
|
+
];
|
|
14
|
+
declare function createContext<T>(options?: CreateContextOptions<T>): CreateContextReturn<T>;
|
|
15
|
+
|
|
16
|
+
export { type CreateContextOptions, type CreateContextReturn, createContext };
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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/context.ts
|
|
21
|
+
var context_exports = {};
|
|
22
|
+
__export(context_exports, {
|
|
23
|
+
createContext: () => createContext
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(context_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
function getErrorMessage(hook, provider) {
|
|
28
|
+
return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
|
|
29
|
+
}
|
|
30
|
+
function createContext(options = {}) {
|
|
31
|
+
const {
|
|
32
|
+
name,
|
|
33
|
+
strict = true,
|
|
34
|
+
hookName = "useContext",
|
|
35
|
+
providerName = "Provider",
|
|
36
|
+
errorMessage,
|
|
37
|
+
defaultValue
|
|
38
|
+
} = options;
|
|
39
|
+
const Context = (0, import_react.createContext)(defaultValue);
|
|
40
|
+
Context.displayName = name;
|
|
41
|
+
function useContext() {
|
|
42
|
+
const context = (0, import_react.useContext)(Context);
|
|
43
|
+
if (!context && strict) {
|
|
44
|
+
const error = new Error(
|
|
45
|
+
errorMessage ?? getErrorMessage(hookName, providerName)
|
|
46
|
+
);
|
|
47
|
+
error.name = "ContextError";
|
|
48
|
+
Error.captureStackTrace?.(error, useContext);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
return context;
|
|
52
|
+
}
|
|
53
|
+
return [Context.Provider, useContext, Context];
|
|
54
|
+
}
|
|
55
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
56
|
+
0 && (module.exports = {
|
|
57
|
+
createContext
|
|
58
|
+
});
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Ref, RefCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A function that merges React refs into one.
|
|
5
|
+
* Supports both functions and ref objects created using createRef() and useRef().
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* ```tsx
|
|
9
|
+
* <div ref={mergeRefs(ref1, ref2, ref3)} />
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @param {(Ref<T> | undefined)[]} inputRefs Array of refs
|
|
13
|
+
* @returns {Ref<T> | RefCallback<T>} Merged refs
|
|
14
|
+
*/
|
|
15
|
+
declare function mergeRefs<T>(...inputRefs: (Ref<T> | undefined)[]): Ref<T> | RefCallback<T>;
|
|
16
|
+
|
|
17
|
+
export { mergeRefs };
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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/dom.ts
|
|
21
|
+
var dom_exports = {};
|
|
22
|
+
__export(dom_exports, {
|
|
23
|
+
mergeRefs: () => mergeRefs
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(dom_exports);
|
|
26
|
+
function mergeRefs(...inputRefs) {
|
|
27
|
+
const filteredInputRefs = inputRefs.filter(Boolean);
|
|
28
|
+
if (filteredInputRefs.length <= 1) {
|
|
29
|
+
const firstRef = filteredInputRefs[0];
|
|
30
|
+
return firstRef || null;
|
|
31
|
+
}
|
|
32
|
+
return function mergedRefs(ref) {
|
|
33
|
+
for (const inputRef of filteredInputRefs) {
|
|
34
|
+
if (typeof inputRef === "function") {
|
|
35
|
+
inputRef(ref);
|
|
36
|
+
} else if (inputRef) {
|
|
37
|
+
inputRef.current = ref;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
mergeRefs
|
|
45
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export { createContext } from './context.js';
|
|
2
|
+
export { mergeRefs } from './dom.js';
|
|
3
|
+
import 'react';
|
package/dist/index.js
CHANGED
|
@@ -20,11 +20,61 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
|
|
23
|
+
createContext: () => createContext,
|
|
24
|
+
mergeRefs: () => mergeRefs
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(index_exports);
|
|
26
|
-
|
|
27
|
+
|
|
28
|
+
// src/context.ts
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
function getErrorMessage(hook, provider) {
|
|
31
|
+
return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
|
|
32
|
+
}
|
|
33
|
+
function createContext(options = {}) {
|
|
34
|
+
const {
|
|
35
|
+
name,
|
|
36
|
+
strict = true,
|
|
37
|
+
hookName = "useContext",
|
|
38
|
+
providerName = "Provider",
|
|
39
|
+
errorMessage,
|
|
40
|
+
defaultValue
|
|
41
|
+
} = options;
|
|
42
|
+
const Context = (0, import_react.createContext)(defaultValue);
|
|
43
|
+
Context.displayName = name;
|
|
44
|
+
function useContext() {
|
|
45
|
+
const context = (0, import_react.useContext)(Context);
|
|
46
|
+
if (!context && strict) {
|
|
47
|
+
const error = new Error(
|
|
48
|
+
errorMessage ?? getErrorMessage(hookName, providerName)
|
|
49
|
+
);
|
|
50
|
+
error.name = "ContextError";
|
|
51
|
+
Error.captureStackTrace?.(error, useContext);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
return context;
|
|
55
|
+
}
|
|
56
|
+
return [Context.Provider, useContext, Context];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/dom.ts
|
|
60
|
+
function mergeRefs(...inputRefs) {
|
|
61
|
+
const filteredInputRefs = inputRefs.filter(Boolean);
|
|
62
|
+
if (filteredInputRefs.length <= 1) {
|
|
63
|
+
const firstRef = filteredInputRefs[0];
|
|
64
|
+
return firstRef || null;
|
|
65
|
+
}
|
|
66
|
+
return function mergedRefs(ref) {
|
|
67
|
+
for (const inputRef of filteredInputRefs) {
|
|
68
|
+
if (typeof inputRef === "function") {
|
|
69
|
+
inputRef(ref);
|
|
70
|
+
} else if (inputRef) {
|
|
71
|
+
inputRef.current = ref;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
27
76
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
77
|
0 && (module.exports = {
|
|
29
|
-
|
|
78
|
+
createContext,
|
|
79
|
+
mergeRefs
|
|
30
80
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kopexa/react-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A set of utilities for react on client side",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-utils"
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"url": "https://github.com/kopexa-grc/sight/issues"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"react": ">=
|
|
28
|
+
"react": ">=19.0.0-rc.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@kopexa/shared-utils": "
|
|
31
|
+
"@kopexa/shared-utils": "1.0.0"
|
|
32
32
|
},
|
|
33
33
|
"clean-package": "../../../clean-package.config.json",
|
|
34
34
|
"module": "dist/index.mjs",
|