@siberiacancode/reactuse 0.1.1 → 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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/helpers/createContext/createContext.cjs +2 -0
  3. package/dist/cjs/helpers/createContext/createContext.cjs.map +1 -0
  4. package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs +2 -0
  5. package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs.map +1 -0
  6. package/dist/cjs/helpers/createStore/createStore.cjs +2 -0
  7. package/dist/cjs/helpers/createStore/createStore.cjs.map +1 -0
  8. package/dist/cjs/hooks/useClipboard/useClipboard.cjs.map +1 -1
  9. package/dist/cjs/hooks/useField/useField.cjs +1 -1
  10. package/dist/cjs/hooks/useField/useField.cjs.map +1 -1
  11. package/dist/cjs/hooks/useGamepad/useGamepad.cjs.map +1 -1
  12. package/dist/cjs/hooks/useIntersectionObserver/useIntersectionObserver.cjs.map +1 -1
  13. package/dist/cjs/hooks/useMemory/useMemory.cjs +1 -1
  14. package/dist/cjs/hooks/useMemory/useMemory.cjs.map +1 -1
  15. package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs +2 -0
  16. package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs.map +1 -0
  17. package/dist/cjs/index.cjs +1 -1
  18. package/dist/esm/helpers/createContext/createContext.mjs +34 -0
  19. package/dist/esm/helpers/createContext/createContext.mjs.map +1 -0
  20. package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs +54 -0
  21. package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs.map +1 -0
  22. package/dist/esm/helpers/createStore/createStore.mjs +25 -0
  23. package/dist/esm/helpers/createStore/createStore.mjs.map +1 -0
  24. package/dist/esm/hooks/useClipboard/useClipboard.mjs.map +1 -1
  25. package/dist/esm/hooks/useField/useField.mjs +1 -3
  26. package/dist/esm/hooks/useField/useField.mjs.map +1 -1
  27. package/dist/esm/hooks/useGamepad/useGamepad.mjs +7 -2
  28. package/dist/esm/hooks/useGamepad/useGamepad.mjs.map +1 -1
  29. package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs.map +1 -1
  30. package/dist/esm/hooks/useMemory/useMemory.mjs +14 -10
  31. package/dist/esm/hooks/useMemory/useMemory.mjs.map +1 -1
  32. package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs +63 -0
  33. package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs.map +1 -0
  34. package/dist/esm/index.mjs +330 -319
  35. package/dist/esm/index.mjs.map +1 -1
  36. package/dist/types/helpers/createContext/createContext.d.ts +48 -0
  37. package/dist/types/helpers/createReactiveContext/createReactiveContext.d.ts +46 -0
  38. package/dist/types/helpers/createStore/createStore.d.ts +31 -0
  39. package/dist/types/helpers/index.d.ts +3 -0
  40. package/dist/types/hooks/index.d.ts +1 -0
  41. package/dist/types/hooks/useClipboard/useClipboard.d.ts +1 -1
  42. package/dist/types/hooks/useIntersectionObserver/useIntersectionObserver.d.ts +1 -1
  43. package/dist/types/hooks/useUrlSearchParams/useUrlSearchParams.d.ts +39 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/package.json +19 -20
@@ -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,48 @@
1
+ import { JSX, ReactNode } from 'react';
2
+ /** The create context options type */
3
+ export interface CreateContextOptions {
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 context value type */
10
+ export interface ContextValue<Value> {
11
+ /** The context value */
12
+ value: Value | undefined;
13
+ /** The context set function */
14
+ set: (value: Value) => void;
15
+ }
16
+ /** The provider props type */
17
+ export interface ProviderProps<Value> {
18
+ /** The children */
19
+ children: ReactNode;
20
+ /** The initial value */
21
+ initialValue?: Value;
22
+ }
23
+ /** The create context return type */
24
+ export interface CreateContextReturn<Value> {
25
+ /** The context instance */
26
+ instance: React.Context<ContextValue<Value>>;
27
+ /** The provider component */
28
+ Provider: (props: ProviderProps<Value>) => JSX.Element;
29
+ /** The selector hook */
30
+ useSelect: {
31
+ <Selected>(selector: (value: Value) => Selected): Selected;
32
+ (): ContextValue<Value>;
33
+ };
34
+ }
35
+ /**
36
+ * @name createContext
37
+ * @description - Creates a typed context with additional utilities
38
+ * @category Helpers
39
+ *
40
+ * @template Value - The type of value that will be stored in the context
41
+ * @param {Value | undefined} [defaultValue] - Default value for the context
42
+ * @param {CreateContextOptions<Value>} [options] - Additional options for context creation
43
+ * @returns {CreateContextReturn<Value>} Object containing context utilities and components
44
+ *
45
+ * @example
46
+ * const { useSelect, instance, Provider } = createContext<number>(0);
47
+ */
48
+ export declare const createContext: <Value>(defaultValue?: Value | undefined, options?: CreateContextOptions) => CreateContextReturn<Value>;
@@ -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 {};
@@ -0,0 +1,31 @@
1
+ type SetStateAction<Value> = ((prev: Value) => Value) | Value;
2
+ type StateCreator<Value> = (set: (action: SetStateAction<Value>) => void, get: () => Value) => Value;
3
+ export interface StoreApi<Value> {
4
+ getInitialState: () => Value;
5
+ getState: () => Value;
6
+ setState: (action: SetStateAction<Value>) => void;
7
+ subscribe: (listener: (state: Value, prevState: Value) => void) => () => void;
8
+ }
9
+ /**
10
+ * @name createStore
11
+ * @description - Creates a store with state management capabilities
12
+ * @category Helpers
13
+ *
14
+ * @template Value - The type of the store state
15
+ *
16
+ * @param {StateCreator<Value>} createState - Function that initializes the store state
17
+ * @returns {StoreApi<Value>} - Object containing store methods and hook for accessing state
18
+ *
19
+ * @example
20
+ * const { set, get, use, subscribe } = createStore((set) => ({
21
+ * count: 0,
22
+ * increment: () => set(state => ({ count: state.count + 1 }))
23
+ * }));
24
+ */
25
+ export declare const createStore: <Value>(createState: StateCreator<Value> | Value) => {
26
+ set: (action: SetStateAction<Value>) => void;
27
+ get: () => Value;
28
+ use: <Selected>(selector: (state: Value) => Selected) => Selected;
29
+ subscribe: (listener: (state: Value, prevState: Value) => void) => () => boolean;
30
+ };
31
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from './createContext/createContext';
2
+ export * from './createReactiveContext/createReactiveContext';
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';
@@ -21,6 +21,6 @@ export interface UseCopyToClipboardParams {
21
21
  * @returns {UseCopyToClipboardReturn} An object containing the boolean state value and utility functions to manipulate the state
22
22
  *
23
23
  * @example
24
- * const { supported, value, copy } = useClipboard();
24
+ * const { value, copy } = useClipboard();
25
25
  */
26
26
  export declare const useClipboard: (params?: UseCopyToClipboardParams) => UseCopyToClipboardReturn;
@@ -29,7 +29,7 @@ export interface UseIntersectionObserver {
29
29
  * @param {boolean} [options.enabled=true] The IntersectionObserver options
30
30
  * @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected
31
31
  * @param {HookTarget} [options.root] The root element to observe
32
- * @returns {UseIntersectionObserverReturn} An object containing the state and the supported status
32
+ * @returns {UseIntersectionObserverReturn} An object containing the state
33
33
  *
34
34
  * @example
35
35
  * const { ref, entry, inView } = useIntersectionObserver();
@@ -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>;
@@ -1,2 +1,3 @@
1
+ export * from './helpers';
1
2
  export * from './hooks';
2
3
  export * from './utils/helpers';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@siberiacancode/reactuse",
3
- "version": "0.1.1",
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.2",
72
- "@types/react-dom": "^19.1.2",
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",
75
- "core-js": "^3.41.0",
63
+ "@vitejs/plugin-react": "^4.5.0",
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
- "vite": "^6.3.2",
80
- "vite-plugin-dts": "^4.5.3",
81
- "vitest": "^3.1.2"
68
+ "vite": "^6.3.5",
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
+ }