@kopexa/react-utils 1.1.1 → 2.0.1
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/chunk-2DAVPPDT.mjs +40 -0
- package/dist/chunk-FGSQ5KEN.mjs +23 -0
- package/dist/context.d.mts +16 -0
- package/dist/context.js +4 -2
- package/dist/context.mjs +7 -0
- package/dist/dom.d.mts +17 -0
- package/dist/dom.js +1 -0
- package/dist/dom.mjs +7 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.js +4 -2
- package/dist/index.mjs +11 -0
- package/package.json +2 -2
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/context.ts
|
|
4
|
+
import {
|
|
5
|
+
createContext as createReactContext,
|
|
6
|
+
useContext as useReactContext
|
|
7
|
+
} from "react";
|
|
8
|
+
function getErrorMessage(hook, provider) {
|
|
9
|
+
return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
|
|
10
|
+
}
|
|
11
|
+
function createContext(options = {}) {
|
|
12
|
+
const {
|
|
13
|
+
name,
|
|
14
|
+
strict = true,
|
|
15
|
+
hookName = "useContext",
|
|
16
|
+
providerName = "Provider",
|
|
17
|
+
errorMessage,
|
|
18
|
+
defaultValue
|
|
19
|
+
} = options;
|
|
20
|
+
const Context = createReactContext(defaultValue);
|
|
21
|
+
Context.displayName = name;
|
|
22
|
+
function useContext() {
|
|
23
|
+
var _a;
|
|
24
|
+
const context = useReactContext(Context);
|
|
25
|
+
if (!context && strict) {
|
|
26
|
+
const error = new Error(
|
|
27
|
+
errorMessage != null ? errorMessage : getErrorMessage(hookName, providerName)
|
|
28
|
+
);
|
|
29
|
+
error.name = "ContextError";
|
|
30
|
+
(_a = Error.captureStackTrace) == null ? void 0 : _a.call(Error, error, useContext);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
return context;
|
|
34
|
+
}
|
|
35
|
+
return [Context.Provider, useContext, Context];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
createContext
|
|
40
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/dom.ts
|
|
4
|
+
function mergeRefs(...inputRefs) {
|
|
5
|
+
const filteredInputRefs = inputRefs.filter(Boolean);
|
|
6
|
+
if (filteredInputRefs.length <= 1) {
|
|
7
|
+
const firstRef = filteredInputRefs[0];
|
|
8
|
+
return firstRef || null;
|
|
9
|
+
}
|
|
10
|
+
return function mergedRefs(ref) {
|
|
11
|
+
for (const inputRef of filteredInputRefs) {
|
|
12
|
+
if (typeof inputRef === "function") {
|
|
13
|
+
inputRef(ref);
|
|
14
|
+
} else if (inputRef) {
|
|
15
|
+
inputRef.current = ref;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
mergeRefs
|
|
23
|
+
};
|
|
@@ -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
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
"use strict";
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -39,13 +40,14 @@ function createContext(options = {}) {
|
|
|
39
40
|
const Context = (0, import_react.createContext)(defaultValue);
|
|
40
41
|
Context.displayName = name;
|
|
41
42
|
function useContext() {
|
|
43
|
+
var _a;
|
|
42
44
|
const context = (0, import_react.useContext)(Context);
|
|
43
45
|
if (!context && strict) {
|
|
44
46
|
const error = new Error(
|
|
45
|
-
errorMessage
|
|
47
|
+
errorMessage != null ? errorMessage : getErrorMessage(hookName, providerName)
|
|
46
48
|
);
|
|
47
49
|
error.name = "ContextError";
|
|
48
|
-
Error.captureStackTrace
|
|
50
|
+
(_a = Error.captureStackTrace) == null ? void 0 : _a.call(Error, error, useContext);
|
|
49
51
|
throw error;
|
|
50
52
|
}
|
|
51
53
|
return context;
|
package/dist/context.mjs
ADDED
package/dist/dom.d.mts
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
CHANGED
package/dist/dom.mjs
ADDED
package/dist/index.d.mts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
"use strict";
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -42,13 +43,14 @@ function createContext(options = {}) {
|
|
|
42
43
|
const Context = (0, import_react.createContext)(defaultValue);
|
|
43
44
|
Context.displayName = name;
|
|
44
45
|
function useContext() {
|
|
46
|
+
var _a;
|
|
45
47
|
const context = (0, import_react.useContext)(Context);
|
|
46
48
|
if (!context && strict) {
|
|
47
49
|
const error = new Error(
|
|
48
|
-
errorMessage
|
|
50
|
+
errorMessage != null ? errorMessage : getErrorMessage(hookName, providerName)
|
|
49
51
|
);
|
|
50
52
|
error.name = "ContextError";
|
|
51
|
-
Error.captureStackTrace
|
|
53
|
+
(_a = Error.captureStackTrace) == null ? void 0 : _a.call(Error, error, useContext);
|
|
52
54
|
throw error;
|
|
53
55
|
}
|
|
54
56
|
return context;
|
package/dist/index.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kopexa/react-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "A set of utilities for react on client side",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-utils"
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"react": ">=19.0.0-rc.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@kopexa/shared-utils": "1.1.
|
|
31
|
+
"@kopexa/shared-utils": "1.1.1"
|
|
32
32
|
},
|
|
33
33
|
"clean-package": "../../../clean-package.config.json",
|
|
34
34
|
"module": "dist/index.mjs",
|