@siberiacancode/reactuse 0.2.0 → 0.2.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.
Files changed (25) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/helpers/createContext/createContext.cjs.map +1 -1
  3. package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs +2 -0
  4. package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs.map +1 -0
  5. package/dist/cjs/helpers/createStore/createStore.cjs +1 -1
  6. package/dist/cjs/helpers/createStore/createStore.cjs.map +1 -1
  7. package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs +2 -0
  8. package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs.map +1 -0
  9. package/dist/cjs/index.cjs +1 -1
  10. package/dist/esm/helpers/createContext/createContext.mjs +2 -6
  11. package/dist/esm/helpers/createContext/createContext.mjs.map +1 -1
  12. package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs +54 -0
  13. package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs.map +1 -0
  14. package/dist/esm/helpers/createStore/createStore.mjs +16 -23
  15. package/dist/esm/helpers/createStore/createStore.mjs.map +1 -1
  16. package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs +63 -0
  17. package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs.map +1 -0
  18. package/dist/esm/index.mjs +328 -321
  19. package/dist/esm/index.mjs.map +1 -1
  20. package/dist/types/helpers/createReactiveContext/createReactiveContext.d.ts +46 -0
  21. package/dist/types/helpers/createStore/createStore.d.ts +1 -1
  22. package/dist/types/helpers/index.d.ts +1 -0
  23. package/dist/types/hooks/index.d.ts +1 -0
  24. package/dist/types/hooks/useUrlSearchParams/useUrlSearchParams.d.ts +39 -0
  25. package/package.json +17 -18
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,46 @@
1
+ import { Context, Provider, RefObject } from 'react';
2
+ /** The create reactive context options type */
3
+ export interface CreateReactiveContextOptions {
4
+ /** Display name for the context (useful for debugging) */
5
+ name?: string;
6
+ /** Whether to throw an error if context is used outside of Provider */
7
+ strict?: boolean;
8
+ }
9
+ /** The create reactive context return type */
10
+ export interface CreateReactiveContextReturn<Value> {
11
+ /** The context instance */
12
+ instance: Context<ReactiveContextValue<Value>>;
13
+ /** The Provider component for the context */
14
+ Provider: Provider<Value>;
15
+ /** A hook to select a part of the context state */
16
+ useSelector: <Selected>(selector?: (state: Value) => Selected) => Selected;
17
+ }
18
+ type ContextListener<Value> = (value: Value) => void;
19
+ interface ReactiveContextValue<Value> {
20
+ /** The listeners for the context */
21
+ listeners: Set<ContextListener<Value>>;
22
+ /** The value for the context */
23
+ value: RefObject<Value>;
24
+ }
25
+ /**
26
+ * @name createReactiveContext
27
+ * @description - Creates a typed context selector with optimized updates for state selection
28
+ * @category Helpers
29
+ *
30
+ * @template Value - The type of value that will be stored in the context
31
+ * @param {Value | undefined} [defaultValue] - Default value for the context
32
+ * @param {CreateReactiveContextOptions<Value>} [options] - Additional options for context creation
33
+ * @returns {CreateReactiveContextReturn<Value>} Object containing context utilities and components
34
+ *
35
+ * @example
36
+ * const { Provider, useSelector, instance } = createReactiveContext<number>(0);
37
+ */
38
+ export declare const createReactiveContext: <Value extends Record<string, any>>(defaultValue?: Value | undefined, options?: CreateReactiveContextOptions) => {
39
+ instance: Context<ReactiveContextValue<Value>>;
40
+ Provider: Provider<Value>;
41
+ useSelector: {
42
+ (): Value;
43
+ <SelectedValue>(selector: (state: Value) => SelectedValue): SelectedValue;
44
+ };
45
+ };
46
+ export {};
@@ -22,7 +22,7 @@ export interface StoreApi<Value> {
22
22
  * increment: () => set(state => ({ count: state.count + 1 }))
23
23
  * }));
24
24
  */
25
- export declare function createStore<Value>(createState: StateCreator<Value> | Value): {
25
+ export declare const createStore: <Value>(createState: StateCreator<Value> | Value) => {
26
26
  set: (action: SetStateAction<Value>) => void;
27
27
  get: () => Value;
28
28
  use: <Selected>(selector: (state: Value) => Selected) => Selected;
@@ -1,2 +1,3 @@
1
1
  export * from './createContext/createContext';
2
+ export * from './createReactiveContext/createReactiveContext';
2
3
  export * from './createStore/createStore';
@@ -125,6 +125,7 @@ export * from './useTimeout/useTimeout';
125
125
  export * from './useTimer/useTimer';
126
126
  export * from './useToggle/useToggle';
127
127
  export * from './useUnmount/useUnmount';
128
+ export * from './useUrlSearchParams/useUrlSearchParams';
128
129
  export * from './useVibrate/useVibrate';
129
130
  export * from './useWakeLock/useWakeLock';
130
131
  export * from './useWebSocket/useWebSocket';
@@ -0,0 +1,39 @@
1
+ export declare const getUrlSearchParams: (mode?: UrlSearchParamsMode) => URLSearchParams;
2
+ export declare const createQueryString: (searchParams: URLSearchParams, mode: UrlSearchParamsMode) => string;
3
+ export declare const setUrlSearchParams: <Params extends UrlParams>(mode: UrlSearchParamsMode, params: Partial<Params>, write?: "push" | "replace") => URLSearchParams;
4
+ /** The url params type */
5
+ export type UrlParams = Record<string, any>;
6
+ /** The url search params mod */
7
+ export type UrlSearchParamsMode = 'hash-params' | 'hash' | 'history';
8
+ /** The use url search params set options type */
9
+ export interface UseUrlSearchParamsSetOptions {
10
+ /** The mode to use for writing to the URL */
11
+ write?: 'push' | 'replace';
12
+ }
13
+ /** The use url search params options type */
14
+ export interface UseUrlSearchParamsOptions {
15
+ /** The mode to use for writing to the URL */
16
+ mode?: UrlSearchParamsMode;
17
+ /** The mode to use for writing to the URL */
18
+ write?: 'push' | 'replace';
19
+ }
20
+ /** The use url search params return type */
21
+ export interface UseUrlSearchParamsReturn<Params extends UrlParams> {
22
+ value: Params;
23
+ set: (params: Partial<Params>) => void;
24
+ }
25
+ /**
26
+ * @name useUrlSearchParams
27
+ * @description - Hook that provides reactive URLSearchParams
28
+ * @category Browser
29
+ *
30
+ * @overload
31
+ * @template Value The type of the url param values
32
+ * @param {UrlSearchParamsMode} mode The URL mode
33
+ * @param {UseUrlSearchParamsOptions<Value>} [options] The URL mode
34
+ * @returns {UseUrlSearchParamsReturn<Value>} The object with value and function for change value
35
+ *
36
+ * @example
37
+ * const { value, set } = useUrlSearchParams('history');
38
+ */
39
+ export declare const useUrlSearchParams: <Params extends UrlParams, SearchParams extends string | UrlParams | URLSearchParams = UrlParams>(initialValue?: SearchParams, options?: UseUrlSearchParamsOptions) => UseUrlSearchParamsReturn<Params>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@siberiacancode/reactuse",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "The ultimate collection of react hooks",
5
5
  "author": {
6
6
  "name": "SIBERIA CAN CODE 🧊",
@@ -39,17 +39,6 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "scripts": {
43
- "prepublishOnly": "pnpm unit-test run && pnpm build",
44
- "build": "shx rm -rf dist && vite build",
45
- "build:js": "tsc --project tsconfig.build.json && prettier --write src/bundle",
46
- "lint": "eslint . --fix",
47
- "lint-inspector": "npx @eslint/config-inspector@latest",
48
- "format": "prettier --write .",
49
- "pretty": "pnpm lint && pnpm format",
50
- "unit-test": "vitest",
51
- "lint-staged": "lint-staged"
52
- },
53
42
  "peerDependencies": {
54
43
  "@types/react": "^18",
55
44
  "react": "^17 || ^18 || ^19",
@@ -68,22 +57,32 @@
68
57
  "@testing-library/dom": "^10.4.0",
69
58
  "@testing-library/react": "^16.3.0",
70
59
  "@types/dom-speech-recognition": "^0.0.6",
71
- "@types/react": "^19.1.3",
72
- "@types/react-dom": "^19.1.3",
60
+ "@types/react": "^19.1.5",
61
+ "@types/react-dom": "^19.1.5",
73
62
  "@types/web-bluetooth": "^0.0.21",
74
- "@vitejs/plugin-react": "^4.4.1",
63
+ "@vitejs/plugin-react": "^4.5.0",
75
64
  "core-js": "^3.42.0",
76
65
  "react": "^19.1.0",
77
66
  "react-dom": "^19.1.0",
78
67
  "shx": "^0.4.0",
79
68
  "vite": "^6.3.5",
80
- "vite-plugin-dts": "^4.5.3",
81
- "vitest": "^3.1.3"
69
+ "vite-plugin-dts": "^4.5.4",
70
+ "vitest": "^3.1.4"
82
71
  },
83
72
  "lint-staged": {
84
73
  "*.{js,ts,tsx}": [
85
74
  "eslint --fix",
86
75
  "prettier --write"
87
76
  ]
77
+ },
78
+ "scripts": {
79
+ "build": "shx rm -rf dist && vite build",
80
+ "build:js": "tsc --project tsconfig.build.json && prettier --write src/bundle",
81
+ "lint": "eslint . --fix",
82
+ "lint-inspector": "npx @eslint/config-inspector@latest",
83
+ "format": "prettier --write .",
84
+ "pretty": "pnpm lint && pnpm format",
85
+ "unit-test": "vitest",
86
+ "lint-staged": "lint-staged"
88
87
  }
89
- }
88
+ }