@leancodepl/utils 9.6.5 → 9.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leancodepl/utils",
3
- "version": "9.6.5",
3
+ "version": "9.6.6",
4
4
  "license": "Apache-2.0",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -10,7 +10,7 @@
10
10
  "node": ">=18.0.0"
11
11
  },
12
12
  "dependencies": {
13
- "@leancodepl/api-date": "9.6.5",
13
+ "@leancodepl/api-date": "9.6.6",
14
14
  "tiny-invariant": ">=1.3.1"
15
15
  },
16
16
  "peerDependencies": {
package/src/index.d.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  export * from "./lib/types";
2
+ export * from "./lib/hooks";
2
3
  export * from "./lib/addPrefix";
3
- export * from "./lib/transformFirst";
4
- export * from "./lib/transformDeep";
5
4
  export * from "./lib/assertDefined";
6
- export * from "./lib/assertNotNull";
7
5
  export * from "./lib/assertNotEmpty";
6
+ export * from "./lib/assertNotNull";
7
+ export * from "./lib/downloadFile";
8
8
  export * from "./lib/ensureDefined";
9
- export * from "./lib/ensureNotNull";
10
9
  export * from "./lib/ensureNotEmpty";
11
- export * from "./lib/downloadFile";
12
- export * from "./lib/hooks/useRunInTask";
13
- export * from "./lib/hooks/useBoundRunInTask";
14
- export * from "./lib/hooks/useKeyByRoute";
15
- export * from "./lib/hooks/useSetUnset";
16
- export * from "./lib/hooks/useDialog";
17
- export * from "./lib/hooks/useSyncState";
10
+ export * from "./lib/ensureNotNull";
11
+ export * from "./lib/transformDeep";
12
+ export * from "./lib/transformFirst";
13
+ export * from "./lib/valueContext";
@@ -0,0 +1,6 @@
1
+ export * from "./useBoundRunInTask";
2
+ export * from "./useDialog";
3
+ export * from "./useKeyByRoute";
4
+ export * from "./useRunInTask";
5
+ export * from "./useSetUnset";
6
+ export * from "./useSyncState";
@@ -0,0 +1,52 @@
1
+ import { Dispatch, ReactNode, SetStateAction } from "react";
2
+ type ValueContextData<T> = [T | undefined, Dispatch<SetStateAction<T | undefined>>];
3
+ type ProviderProps<T> = {
4
+ children?: ReactNode;
5
+ initialValue?: T;
6
+ };
7
+ /**
8
+ * Creates a React context hook for managing optional state values with Provider and setter utilities.
9
+ * Returns a hook with attached Provider component and set function for declarative value management.
10
+ *
11
+ * @returns Hook function with attached Provider component and set function
12
+ * @example
13
+ * ```typescript
14
+ * import { mkValueContext } from "@leancodepl/utils";
15
+ *
16
+ * const useTheme = mkValueContext<string>();
17
+ *
18
+ * function App() {
19
+ * return (
20
+ * <useTheme.Provider initialValue="dark">
21
+ * <ThemeConsumer />
22
+ * </useTheme.Provider>
23
+ * );
24
+ * }
25
+ *
26
+ * function ThemeConsumer() {
27
+ * const [theme] = useTheme();
28
+ * return <div>Current theme: {theme}</div>;
29
+ * }
30
+ * ```
31
+ * @example
32
+ * ```typescript
33
+ * // Using set to declaratively set context value
34
+ * const useActiveUser = mkValueContext<string>();
35
+ *
36
+ * function UserProfile({ userId }: { userId: string }) {
37
+ * useActiveUser.set(userId); // Sets value on mount, clears on unmount
38
+ * return <div>Profile content</div>;
39
+ * }
40
+ *
41
+ * function UserBadge() {
42
+ * const [activeUserId] = useActiveUser();
43
+ * return <div>Active user: {activeUserId}</div>;
44
+ * }
45
+ * ```
46
+ */
47
+ export declare function mkValueContext<T>(): {
48
+ (): ValueContextData<T>;
49
+ Provider({ children, initialValue }: ProviderProps<T>): import("react/jsx-runtime").JSX.Element;
50
+ set(value: T | undefined): void;
51
+ };
52
+ export {};