@proedis/react 1.0.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 (44) hide show
  1. package/LICENSE +9 -0
  2. package/cjs/hooks/useAutoControlledState.js +97 -0
  3. package/cjs/hooks/useClickOutside.js +62 -0
  4. package/cjs/hooks/useDebouncedCallback.js +62 -0
  5. package/cjs/hooks/useElementType.js +40 -0
  6. package/cjs/hooks/useEnhancedEffect.js +35 -0
  7. package/cjs/hooks/useEvent.js +45 -0
  8. package/cjs/hooks/useForkRef.js +42 -0
  9. package/cjs/hooks/useSafeState.js +54 -0
  10. package/cjs/hooks/useSyncedRef.js +42 -0
  11. package/cjs/hooks/useUnmountEffect.js +22 -0
  12. package/cjs/hooks/useWindowSize.js +81 -0
  13. package/cjs/index.js +29 -0
  14. package/cjs/utils/contextBuilder.js +56 -0
  15. package/esm/hooks/useAutoControlledState.js +76 -0
  16. package/esm/hooks/useClickOutside.js +60 -0
  17. package/esm/hooks/useDebouncedCallback.js +41 -0
  18. package/esm/hooks/useElementType.js +38 -0
  19. package/esm/hooks/useEnhancedEffect.js +14 -0
  20. package/esm/hooks/useEvent.js +43 -0
  21. package/esm/hooks/useForkRef.js +21 -0
  22. package/esm/hooks/useSafeState.js +33 -0
  23. package/esm/hooks/useSyncedRef.js +21 -0
  24. package/esm/hooks/useUnmountEffect.js +20 -0
  25. package/esm/hooks/useWindowSize.js +60 -0
  26. package/esm/index.js +12 -0
  27. package/esm/utils/contextBuilder.js +35 -0
  28. package/package.json +39 -0
  29. package/tsconfig.declaration.tsbuildinfo +1 -0
  30. package/types/hooks/index.d.ts +11 -0
  31. package/types/hooks/useAutoControlledState.d.ts +30 -0
  32. package/types/hooks/useClickOutside.d.ts +9 -0
  33. package/types/hooks/useDebouncedCallback.d.ts +16 -0
  34. package/types/hooks/useElementType.d.ts +25 -0
  35. package/types/hooks/useEnhancedEffect.d.ts +8 -0
  36. package/types/hooks/useEvent.d.ts +16 -0
  37. package/types/hooks/useForkRef.d.ts +8 -0
  38. package/types/hooks/useSafeState.d.ts +9 -0
  39. package/types/hooks/useSyncedRef.d.ts +11 -0
  40. package/types/hooks/useUnmountEffect.d.ts +8 -0
  41. package/types/hooks/useWindowSize.d.ts +30 -0
  42. package/types/index.d.ts +2 -0
  43. package/types/utils/contextBuilder.d.ts +18 -0
  44. package/types/utils/index.d.ts +1 -0
@@ -0,0 +1,30 @@
1
+ import * as React from 'react';
2
+ interface UseAutoControlledStateOptions<S> {
3
+ /** The default value to set at the first time */
4
+ defaultValue?: S;
5
+ /**
6
+ * The value to set and update every time it changes.
7
+ * If it has been set, then the state will be controlled
8
+ * externally and the trySet function won't have any effect
9
+ */
10
+ value?: S;
11
+ }
12
+ type UseAutoControlledStateResult<S> = Readonly<[
13
+ /** Current value */
14
+ S,
15
+ /** Function to try to change the value */
16
+ React.Dispatch<React.SetStateAction<S>>,
17
+ /** A function that will force the state change */
18
+ React.Dispatch<React.SetStateAction<S>>
19
+ ]>;
20
+ /**
21
+ * Augmented useState with AutoControlled option.
22
+ * Use this hook if the controlling of this state
23
+ * will be optionally demanded externally and
24
+ * potentially controlled by a prop or an arbitrary value
25
+ *
26
+ * @param initialState The initial state of the AutoControlled State
27
+ * @param options Options to pass controlled value and default value
28
+ */
29
+ export declare function useAutoControlledState<S>(initialState: S | (() => S), options?: UseAutoControlledStateOptions<S>): UseAutoControlledStateResult<S>;
30
+ export {};
@@ -0,0 +1,9 @@
1
+ import type * as React from 'react';
2
+ /**
3
+ * Use this hook to fire an event every time a click occurs outside
4
+ * the provided target element
5
+ * @param target
6
+ * @param callback
7
+ * @param events
8
+ */
9
+ export declare function useClickOutside<T extends HTMLElement>(target: React.RefObject<T> | React.MutableRefObject<T>, callback: EventListener, events?: ReadonlyArray<keyof HTMLElementEventMap>): void;
@@ -0,0 +1,16 @@
1
+ import * as React from 'react';
2
+ interface DebouncedFunction<Fn extends (...args: any[]) => any> {
3
+ (this: null, ...args: Parameters<Fn>): void;
4
+ }
5
+ /**
6
+ * Return a wrapped Debounced variant function of the
7
+ * provided function.
8
+ * Interval param will delay the returned function call
9
+ * if a next invoking will be requested
10
+ *
11
+ * @param callback The function to call
12
+ * @param deps Array dependencies list
13
+ * @param interval The interval time
14
+ */
15
+ export declare function useDebouncedCallback<Fn extends (...args: any[]) => any>(callback: Fn, deps: React.DependencyList, interval: number): DebouncedFunction<Fn>;
16
+ export {};
@@ -0,0 +1,25 @@
1
+ import type * as React from 'react';
2
+ import type { AnyObject, KeysMatching } from '@proedis/types';
3
+ export type GetElementType<P extends {}> = ((props: P) => React.ElementType | undefined | false) | ElementTypeMapper<P>;
4
+ interface ElementReliantProps extends AnyObject {
5
+ /** Received props could have the as ElementType set */
6
+ as?: React.ElementType;
7
+ /** Href could be used to define if an Element must be an anchor or not */
8
+ href?: string;
9
+ }
10
+ type ForwardedComponent<P> = React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<any>>;
11
+ type ElementTypeMapper<P extends AnyObject, K extends keyof KeysMatching<P, string> = keyof KeysMatching<P, string>> = {
12
+ map: K;
13
+ using: Partial<Record<P[K], React.ElementType>>;
14
+ default: React.ElementType;
15
+ };
16
+ /**
17
+ * Get the ElementType that must be used to render a Component
18
+ *
19
+ * @param Component The Component to render
20
+ * @param props Props received by Component
21
+ * @param defaultElementType A default element type to use
22
+ * @param getDefault A defined function used to compute the ElementType by props
23
+ */
24
+ export declare function useElementType<Props extends ElementReliantProps = ElementReliantProps>(Component: (ForwardedComponent<Props>) | React.ComponentType<Props>, props: Props, defaultElementType?: React.ElementType, getDefault?: GetElementType<Props>): React.ElementType;
25
+ export {};
@@ -0,0 +1,8 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Return the best way to use the native
4
+ * useEffect React hook, getting the
5
+ * useLayoutEffect if current code is executing
6
+ * on a real browser client
7
+ */
8
+ export declare const useEnhancedEffect: typeof React.useLayoutEffect;
@@ -0,0 +1,16 @@
1
+ interface UseEventOptions {
2
+ /** Set the event fire as disabled without unloading the hook */
3
+ disabled?: boolean;
4
+ /** Change the event target, default to the main document element */
5
+ target?: HTMLElement;
6
+ }
7
+ type EventCallback<K extends keyof HTMLElementEventMap> = (this: HTMLElement, event: HTMLElementEventMap[K]) => void;
8
+ /**
9
+ * Attach a HTMLElement lister to a target DOM Element.
10
+ *
11
+ * @param name The event to listen to
12
+ * @param callback The callback function to call
13
+ * @param options Some options to customize the hook
14
+ */
15
+ export declare function useEvent<K extends keyof HTMLElementEventMap>(name: K, callback: EventCallback<K>, options?: UseEventOptions): void;
16
+ export {};
@@ -0,0 +1,8 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Group multiple ref object into a single ref that will
4
+ * assign the referenced element to each single ref.
5
+ *
6
+ * @param refs
7
+ */
8
+ export declare function useForkRef<T>(...refs: ReturnType<typeof React.useRef<T>>[]): React.Ref<T>;
@@ -0,0 +1,9 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Hook that could be used to wrap the original
4
+ * setState hook function to be called and to change
5
+ * state only if the using component is mounted
6
+ *
7
+ * @param initialState Initial state to use
8
+ */
9
+ export declare function useSafeState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>];
@@ -0,0 +1,11 @@
1
+ /**
2
+ * An augmented version of the useRef function.
3
+ * It will return a 'ref' style object with the
4
+ * current key memoized, returning each time
5
+ * the most updated version of the value
6
+ *
7
+ * @param value The value to store into ref object
8
+ */
9
+ export declare function useSyncedRef<T>(value: T): {
10
+ readonly current: T;
11
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Use this hook to call the provided
3
+ * callable function to be called once the
4
+ * component will unmount
5
+ *
6
+ * @param callback Function to be called
7
+ */
8
+ export declare function useUnmountEffect(callback: CallableFunction): void;
@@ -0,0 +1,30 @@
1
+ interface UseWindowSizeOptions {
2
+ /** Disable the monitoring of the Window Size */
3
+ disabled?: boolean;
4
+ }
5
+ interface WindowSize {
6
+ /** The inner height of the window element */
7
+ height: number;
8
+ /** The inner width of the window element */
9
+ width: number;
10
+ }
11
+ /**
12
+ * Monitor the change of the Window Size.
13
+ * Take careful that the object returned by the hook will never change:
14
+ * to use value within hook dependencies must deconstruct internal properties
15
+ *
16
+ * @param options Set hook options
17
+ *
18
+ * @example
19
+ * // Do this
20
+ * const { height, width } = useWindowSize();
21
+ *
22
+ * React.useEffect(
23
+ * () => {
24
+ * // Wonderful code...
25
+ * },
26
+ * [ height, width ]
27
+ * );
28
+ */
29
+ export declare function useWindowSize(options?: UseWindowSizeOptions): WindowSize;
30
+ export {};
@@ -0,0 +1,2 @@
1
+ export * from './hooks';
2
+ export * from './utils';
@@ -0,0 +1,18 @@
1
+ import * as React from 'react';
2
+ import type { AnyObject } from '@proedis/types';
3
+ type KeyValue<Key extends string, Value> = {
4
+ [key in Key]: Value;
5
+ };
6
+ type ContextHook<N extends string, C extends AnyObject> = KeyValue<`use${Capitalize<N>}`, () => C>;
7
+ type ContextProvider<N extends string, C extends AnyObject> = KeyValue<`${Capitalize<N>}Provider`, React.Provider<C>>;
8
+ type ContextConsumer<N extends string, C extends AnyObject> = KeyValue<`${Capitalize<N>}Consumer`, React.Consumer<C>>;
9
+ type BuiltContext<Name extends string, Context extends AnyObject> = ContextHook<Name, Context> & ContextProvider<Name, Context> & ContextConsumer<Name, Context>;
10
+ /**
11
+ * Easy creation of a React Context providing the name to use
12
+ * and optionally an initial context.
13
+ * Object returned will reformat string and return well-named function and components
14
+ * @param name
15
+ * @param initialContext
16
+ */
17
+ export declare function contextBuilder<Context extends AnyObject, Name extends string = string>(name: Name, initialContext?: Context): BuiltContext<Name, Context>;
18
+ export {};
@@ -0,0 +1 @@
1
+ export * from './contextBuilder';