@kopexa/react-utils 1.1.0 → 2.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/README.md +81 -0
- 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 +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @kopexa/react-utils
|
|
2
|
+
|
|
3
|
+
A powerful utility toolkit for advanced React development, part of the Kopexa Sight Design System.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
`@kopexa/react-utils` provides essential utilities for building robust, scalable, and maintainable React applications. It includes advanced context creation and ref management utilities, designed for modern React (v19+) and seamless integration with the Kopexa Sight ecosystem.
|
|
10
|
+
|
|
11
|
+
- **Author:** Kopexa (<https://kopexa.com>)
|
|
12
|
+
- **License:** MIT
|
|
13
|
+
- **Repository:** [kopexa-grc/sight](https://github.com/kopexa-grc/sight)
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **createContext**: Type-safe, ergonomic context creation with strict mode and custom error messages.
|
|
18
|
+
- **mergeRefs**: Effortlessly merge multiple refs (callback and object) for advanced component composition.
|
|
19
|
+
- **TypeScript-first**: Fully typed APIs for maximum safety and DX.
|
|
20
|
+
- **Zero dependencies**: Only peer-depends on React.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
pnpm add @kopexa/react-utils
|
|
26
|
+
# or
|
|
27
|
+
yarn add @kopexa/react-utils
|
|
28
|
+
# or
|
|
29
|
+
npm install @kopexa/react-utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
### `createContext<T>(options?: CreateContextOptions<T>): [Provider, useContext, Context]`
|
|
35
|
+
|
|
36
|
+
A strict, ergonomic alternative to React's context API.
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
import { createContext } from '@kopexa/react-utils';
|
|
40
|
+
|
|
41
|
+
const [Provider, useMyContext] = createContext<number>({
|
|
42
|
+
name: 'MyContext',
|
|
43
|
+
strict: true,
|
|
44
|
+
hookName: 'useMyContext',
|
|
45
|
+
providerName: 'MyProvider',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
function MyComponent() {
|
|
49
|
+
const value = useMyContext();
|
|
50
|
+
// ...
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Options
|
|
55
|
+
- `strict` (default: true): Throws if context is missing.
|
|
56
|
+
- `hookName`, `providerName`, `errorMessage`, `name`, `defaultValue`: Customize error and context behavior.
|
|
57
|
+
|
|
58
|
+
### `mergeRefs<T>(...refs: Ref<T>[]): Ref<T>`
|
|
59
|
+
|
|
60
|
+
Merge multiple refs (callback or object) into a single ref for advanced component composition.
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import { mergeRefs } from '@kopexa/react-utils';
|
|
64
|
+
|
|
65
|
+
function MyInput(props, ref) {
|
|
66
|
+
return <input ref={mergeRefs(ref, props.innerRef)} />;
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Best Practices
|
|
71
|
+
|
|
72
|
+
- Use `createContext` for all new context providers in your app or library for safer, more maintainable code.
|
|
73
|
+
- Use `mergeRefs` when you need to forward refs to multiple consumers (e.g., HOCs, hooks, portals).
|
|
74
|
+
|
|
75
|
+
## Why Kopexa Sight?
|
|
76
|
+
|
|
77
|
+
Kopexa Sight is dedicated to building reliable, developer-friendly open source tools. This package is designed for clarity, stability, and best practices, inspired by the Google API Design Guide.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
MIT © Kopexa
|
|
@@ -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