@hairy/react-lib 1.47.0 → 1.50.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/dist/index.d.ts DELETED
@@ -1,374 +0,0 @@
1
- import { AnyFn, BooleanLike, Deferred, PromiseFn, PromiseType } from '@hairy/utils';
2
- import * as react from 'react';
3
- import { JSX, ReactNode, PropsWithChildren, ReactElement, FC, ComponentClass, useCallback, useEffect, useInsertionEffect, useReducer, useRef, useState, DetailedHTMLProps, HTMLAttributes } from 'react';
4
- import * as valtio from 'valtio';
5
-
6
- type Value = string | boolean | undefined | null;
7
- type Mapping = Record<string, any>;
8
- interface ArgumentArray extends Array<Argument> {
9
- }
10
- interface ReadonlyArgumentArray extends ReadonlyArray<Argument> {
11
- }
12
- type Argument = Value | Mapping | ArgumentArray | ReadonlyArgumentArray;
13
- /**
14
- * A simple JavaScript utility for conditionally joining classNames together.
15
- */
16
- declare function cls(...args: ArgumentArray): string;
17
- declare namespace cls {
18
- var parse: (arg: any) => string;
19
- var append: (value: any, newClass: any) => any;
20
- }
21
-
22
- /**
23
- * @requires `Trigger` component to be mounted in the tree.
24
- *
25
- * @example
26
- * ```tsx
27
- * // Obtain externally
28
- * import { track } from '@hairy/lib-react'
29
- * const context = await track(() => useContext(YourContext))
30
- * console.log(context) // { ... }
31
- */
32
- declare function track<T extends AnyFn>(fn: T, ...args: Parameters<T>): Promise<ReturnType<T>>;
33
-
34
- type WrapperTag = keyof JSX.IntrinsicElements | Function;
35
- type WrapperProps<As extends keyof JSX.IntrinsicElements | React.FC | unknown> = {
36
- /** @deprecated use `as` instead */
37
- tag?: As;
38
- as?: As;
39
- } & (As extends keyof JSX.IntrinsicElements ? JSX.IntrinsicElements[As] : unknown) & (As extends React.FC<infer P> ? P : unknown);
40
- declare function wrapper(asChild: any, props: unknown, children?: React.ReactNode): react.ReactNode;
41
-
42
- type CaseProps<Kag> = WrapperProps<Kag> & {
43
- cond?: BooleanLike;
44
- children?: ReactNode;
45
- };
46
- declare function Case<Tag extends WrapperTag>(props: CaseProps<Tag>): ReactNode;
47
-
48
- type DefaultProps<Tag> = WrapperProps<Tag> & {
49
- children?: ReactNode;
50
- };
51
- declare function Default<Tag extends WrapperTag>(props: DefaultProps<Tag>): ReactNode;
52
-
53
- type IfProps<Kag> = WrapperProps<Kag> & {
54
- cond?: BooleanLike;
55
- then?: ReactNode;
56
- else?: ReactNode;
57
- children?: ReactNode;
58
- };
59
- declare function If<K extends WrapperTag>(props: IfProps<K>): ReactNode;
60
-
61
- type ElseProps<Tag> = IfProps<Tag>;
62
- declare function Else<Tag extends WrapperTag>(props: ElseProps<Tag>): react.ReactNode;
63
-
64
- interface SwitchProps extends PropsWithChildren {
65
- value?: BooleanLike;
66
- }
67
- declare function Switch(props: SwitchProps): ReactElement<unknown, string | react.JSXElementConstructor<any>> | null;
68
-
69
- type ThenProps<Tag> = IfProps<Tag>;
70
- declare function Then<Tag extends WrapperTag>(props: ThenProps<Tag>): react.ReactNode;
71
-
72
- type UnlessProps<Tag> = WrapperProps<Tag> & {
73
- cond?: BooleanLike;
74
- then?: ReactNode;
75
- else?: ReactNode;
76
- children?: ReactNode;
77
- };
78
- declare function Unless<Tag extends WrapperTag>(props: UnlessProps<Tag>): ReactNode;
79
-
80
- interface InjectComponent<P> {
81
- component: FC<P> | ComponentClass<P>;
82
- props?: P;
83
- }
84
- interface InjectorProps {
85
- install: (FC<any> | InjectComponent<any> | ComponentClass<any>)[];
86
- children?: ReactNode;
87
- }
88
- declare function Injector(props: InjectorProps): ReactNode;
89
-
90
- interface Exposer {
91
- deferred: Deferred<any>;
92
- args: any[];
93
- fn: AnyFn;
94
- id: number;
95
- }
96
- /**
97
- * @example
98
- * ```tsx
99
- * import { Trigger } from '@hairy/lib-react'
100
- *
101
- * // Use triggers to capture context
102
- * function App() {
103
- * return (
104
- * <YourContext.Provider>
105
- * <Trigger />
106
- * </YourContext.Provider>
107
- * )
108
- * }
109
- *
110
- * // Obtain externally
111
- * import { track } from '@hairy/lib-react'
112
- * const context = await track(() => useContext(YourContext))
113
- * console.log(context) // { ... }
114
- * ```
115
- */
116
- declare function Trigger(): ReactNode[];
117
- declare namespace Trigger {
118
- var id: number;
119
- var tasks: Map<number, Exposer> & {
120
- $$valtioSnapshot: Omit<Map<number, Exposer>, "clear" | "set" | "delete">;
121
- };
122
- }
123
-
124
- declare const tryUseCallback: typeof useCallback;
125
-
126
- declare const tryUseEffect: typeof useEffect;
127
-
128
- declare const tryUseInsertionEffect: typeof useInsertionEffect;
129
-
130
- declare const tryUseReducer: typeof useReducer;
131
-
132
- declare const tryUseRef: typeof useRef;
133
-
134
- declare const tryUseState: typeof useState;
135
-
136
- declare function tryUseUpdate(): () => void;
137
-
138
- declare function useAsyncCallback<T extends PromiseFn>(fun: T): readonly [boolean, T, Error | undefined];
139
-
140
- type AsyncState<T> = {
141
- loading: boolean;
142
- error?: undefined;
143
- value?: undefined;
144
- } | {
145
- loading: true;
146
- error?: Error | undefined;
147
- value?: T;
148
- } | {
149
- loading: false;
150
- error: Error;
151
- value?: undefined;
152
- } | {
153
- loading: false;
154
- error?: undefined;
155
- value: T;
156
- };
157
- type StateFromFnReturningPromise<T extends PromiseFn = PromiseFn> = AsyncState<PromiseType<ReturnType<T>>>;
158
- declare type AsyncStateReturn<T extends PromiseFn = PromiseFn> = [
159
- StateFromFnReturningPromise<T>,
160
- T
161
- ];
162
-
163
- type UseAsyncStateOptions<T extends AnyFn> = {
164
- immediate?: boolean;
165
- initial?: PromiseType<ReturnType<T>>;
166
- deps?: any[];
167
- } | {
168
- immediate?: boolean;
169
- initial: PromiseType<ReturnType<T>>;
170
- deps?: any[];
171
- };
172
- declare function useAsyncState<T extends PromiseFn>(fun: T, deps?: any[], options?: UseAsyncStateOptions<T>): AsyncStateReturn<T>;
173
-
174
- declare function useDebounce<T>(value: T, delay: number): T;
175
-
176
- interface EventBusListener<T = any> {
177
- (event: T): void;
178
- }
179
- declare function useEventBus<T>(key: string): {
180
- on: (listener: EventBusListener<T>) => void;
181
- emit: (event?: T) => void;
182
- off: (listener: EventBusListener) => void;
183
- };
184
-
185
- interface FetchResponseInterceptCallback {
186
- (response: Response, init: RequestInit | undefined): Response | Promise<Response>;
187
- }
188
- interface FetchRequestInterceptCallback {
189
- (fetch: typeof window.fetch, input: RequestInfo | URL, init?: RequestInit | undefined): Response | Promise<Response>;
190
- }
191
- declare function useFetchResponseIntercept(intercept: FetchResponseInterceptCallback): void;
192
- declare function useFetchRequestIntercept(intercept: FetchRequestInterceptCallback): void;
193
-
194
- declare function useMounted(): boolean;
195
-
196
- interface UseOffsetPaginationOptions {
197
- total?: number;
198
- page?: number;
199
- pageSize?: number;
200
- onChange?: (pagination: Pagination) => void;
201
- onPageSizeChange?: (pagination: Pagination) => void;
202
- onPageCountChange?: (pagination: Pagination) => void;
203
- }
204
- interface Pagination {
205
- pageSizeChange: (limit: number) => void;
206
- pageChange: (page: number) => void;
207
- next: () => void;
208
- prev: () => void;
209
- page: number;
210
- pageSize: number;
211
- isFirstPage: boolean;
212
- isLastPage: boolean;
213
- pageCount: number;
214
- total: number;
215
- }
216
- declare function useOffsetPagination(options: UseOffsetPaginationOptions): Pagination;
217
-
218
- declare function useUpdate(): () => void;
219
-
220
- interface UseWatchCallback<T = any> {
221
- (value: T, oldValue: T): void;
222
- }
223
- interface UseWatchOptions {
224
- immediate?: boolean;
225
- }
226
- declare function useWatch<T extends any[]>(source: readonly [...T], callback: UseWatchCallback<[...T]>, options?: UseWatchOptions): void;
227
- declare function useWatch<T>(source: T, callback: UseWatchCallback<T>, options?: UseWatchOptions): void;
228
-
229
- declare function useWhenever<T>(source: T, cb: UseWatchCallback<Exclude<T, null | undefined>>, options?: UseWatchOptions): void;
230
-
231
- interface PersistantOptions {
232
- id: string;
233
- storage?: Storage;
234
- pick?: string[];
235
- }
236
- /**
237
- *
238
- * @deprecated please use `valtio-define`
239
- */
240
- declare function proxyWithPersistant<T extends object>(key: string, initialObject?: T, options?: Omit<PersistantOptions, 'key'>): T;
241
- declare function proxyWithPersistant<T extends object>(options: PersistantOptions, initialObject?: T): T;
242
-
243
- type Actions<S> = Record<string, (this: S, ...args: any) => any>;
244
- type Getters<S> = Record<string, (this: S) => any>;
245
- type ActionsOmitThisParameter<A extends Actions<any>> = {
246
- [K in keyof A]: (...args: Parameters<A[K]>) => ReturnType<A[K]>;
247
- };
248
- interface Status {
249
- finished: boolean;
250
- loading: boolean;
251
- error: Error | null;
252
- }
253
- type ActionsStatus<A extends Actions<any>> = Status & {
254
- [K in keyof A]: Status;
255
- };
256
- type GettersReturnType<G extends Getters<any>> = {
257
- [K in keyof G]: ReturnType<G[K]>;
258
- };
259
- interface StoreDefine<S extends object, A extends Actions<S>, G extends Getters<S>> {
260
- state: (() => S) | S;
261
- actions?: A;
262
- getters?: G;
263
- }
264
- interface StoreOptions {
265
- persist?: string | PersistantOptions;
266
- }
267
- interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
268
- <T>(fn: (state: S & GettersReturnType<G>) => T): T;
269
- status: <T>(fn: (status: ActionsStatus<A>) => T) => T;
270
- }
271
- interface StoreSubscribe<S, A extends Actions<S>, G extends Getters<S>> {
272
- (listener: (state: S & GettersReturnType<G>) => void): () => void;
273
- status: (listener: (status: ActionsStatus<A>) => void) => () => void;
274
- }
275
- interface StorePatch<S, G extends Getters<S>> {
276
- (patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
277
- }
278
- type Store<S, A extends Actions<S>, G extends Getters<S>> = {
279
- $subscribe: StoreSubscribe<S, A, G>;
280
- $patch: StorePatch<S, G>;
281
- $state: S & GettersReturnType<G> & ActionsOmitThisParameter<A>;
282
- $actions: ActionsOmitThisParameter<A>;
283
- $getters: GettersReturnType<G>;
284
- $status: ActionsStatus<A>;
285
- $signal: StoreSignal<S, A, G>;
286
- } & ActionsOmitThisParameter<A>;
287
-
288
- /**
289
- * @description Define a store
290
- * @deprecated please use `valtio-define`
291
- * @example
292
- * ```tsx
293
- * const store = defineStore({
294
- * state: () => ({ count: 0 }),
295
- * actions: {
296
- * increment() {
297
- * this.count++
298
- * },
299
- * },
300
- * })
301
- *
302
- * store.increment()
303
- * console.log(store.$state.count) // 1
304
- *
305
- * function Component() {
306
- * const store = useStore(store)
307
- * return (
308
- * <div>
309
- * <button onClick={store.increment}>Increment</button>
310
- * <div>{store.count}</div>
311
- * </div>
312
- * )
313
- * }
314
- *
315
- * ```
316
- */
317
- declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: StoreDefine<S, A, G>, options?: StoreOptions): Store<S, A, G>;
318
-
319
- type StoreAsyncInitial<T extends AnyFn> = ReturnType<T> extends Promise<infer U> ? U : ReturnType<T>;
320
- interface StoreAsyncOptions<T extends AnyFn> {
321
- initial?: StoreAsyncInitial<T>;
322
- persist?: string | Omit<PersistantOptions, 'pick'> | undefined;
323
- immediate?: boolean;
324
- }
325
- interface StoreAsyncInitialOptions<T extends AnyFn> extends StoreAsyncOptions<T> {
326
- initial: StoreAsyncInitial<T>;
327
- }
328
- type StoreAsync<T extends AnyFn, Initial = StoreAsyncInitial<T> | undefined> = Store<{
329
- value: Initial;
330
- error: Error | null | undefined;
331
- loading: boolean;
332
- finished: boolean;
333
- }, {
334
- refetch: (...args: Parameters<T>) => ReturnType<T>;
335
- reset: (value?: StoreAsyncInitial<T>) => void;
336
- }, {}>;
337
- /**
338
- * @description Define a store async
339
- * @deprecated please use `valtio-define`
340
- * @example
341
- * ```tsx
342
- * const store = defineStoreAsync(
343
- * () => fetch('https://api.example.com/data').then(response => response.json()),
344
- * {
345
- * initial: [],
346
- * // data will be obtained immediately upon defining the store
347
- * immediate: true,
348
- * },
349
- * )
350
- * ```
351
- */
352
- declare function defineStoreAsync<T extends AnyFn>(fetch: T, options: StoreAsyncInitialOptions<T>): StoreAsync<T, StoreAsyncInitial<T>>;
353
- declare function defineStoreAsync<T extends AnyFn>(fetch: T, options?: StoreAsyncOptions<T>): StoreAsync<T>;
354
- /**
355
- * @deprecated
356
- * use defineStoreAsync instead
357
- */
358
- declare const defienAsyncStore: typeof defineStoreAsync;
359
-
360
- /**
361
- *
362
- * @deprecated please use `valtio-define`
363
- */
364
- declare function useStatus<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<ActionsStatus<A>>;
365
-
366
- /**
367
- *
368
- * @deprecated please use `valtio-define`
369
- */
370
- declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<S & GettersReturnType<G> & ActionsOmitThisParameter<A>>;
371
-
372
- type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
373
-
374
- export { type Argument, type ArgumentArray, Case, type CaseProps, Default, type DefaultProps, Else, type ElseProps, type EventBusListener, type Exposer, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type Pagination, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StoreAsync, type StoreAsyncInitial, type StoreAsyncInitialOptions, type StoreAsyncOptions, Switch, type SwitchProps, Then, type ThenProps, Trigger, Unless, type UnlessProps, type UseAsyncStateOptions, type UseOffsetPaginationOptions, type UseWatchCallback, type UseWatchOptions, type Value, type WrapperProps, type WrapperTag, cls, defienAsyncStore, defineStore, defineStoreAsync, proxyWithPersistant, track, tryUseCallback, tryUseEffect, tryUseInsertionEffect, tryUseReducer, tryUseRef, tryUseState, tryUseUpdate, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useOffsetPagination, useStatus, useStore, useUpdate, useWatch, useWhenever, wrapper };