@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.
- package/LICENSE +9 -0
- package/cjs/hooks/useAutoControlledState.js +97 -0
- package/cjs/hooks/useClickOutside.js +62 -0
- package/cjs/hooks/useDebouncedCallback.js +62 -0
- package/cjs/hooks/useElementType.js +40 -0
- package/cjs/hooks/useEnhancedEffect.js +35 -0
- package/cjs/hooks/useEvent.js +45 -0
- package/cjs/hooks/useForkRef.js +42 -0
- package/cjs/hooks/useSafeState.js +54 -0
- package/cjs/hooks/useSyncedRef.js +42 -0
- package/cjs/hooks/useUnmountEffect.js +22 -0
- package/cjs/hooks/useWindowSize.js +81 -0
- package/cjs/index.js +29 -0
- package/cjs/utils/contextBuilder.js +56 -0
- package/esm/hooks/useAutoControlledState.js +76 -0
- package/esm/hooks/useClickOutside.js +60 -0
- package/esm/hooks/useDebouncedCallback.js +41 -0
- package/esm/hooks/useElementType.js +38 -0
- package/esm/hooks/useEnhancedEffect.js +14 -0
- package/esm/hooks/useEvent.js +43 -0
- package/esm/hooks/useForkRef.js +21 -0
- package/esm/hooks/useSafeState.js +33 -0
- package/esm/hooks/useSyncedRef.js +21 -0
- package/esm/hooks/useUnmountEffect.js +20 -0
- package/esm/hooks/useWindowSize.js +60 -0
- package/esm/index.js +12 -0
- package/esm/utils/contextBuilder.js +35 -0
- package/package.json +39 -0
- package/tsconfig.declaration.tsbuildinfo +1 -0
- package/types/hooks/index.d.ts +11 -0
- package/types/hooks/useAutoControlledState.d.ts +30 -0
- package/types/hooks/useClickOutside.d.ts +9 -0
- package/types/hooks/useDebouncedCallback.d.ts +16 -0
- package/types/hooks/useElementType.d.ts +25 -0
- package/types/hooks/useEnhancedEffect.d.ts +8 -0
- package/types/hooks/useEvent.d.ts +16 -0
- package/types/hooks/useForkRef.d.ts +8 -0
- package/types/hooks/useSafeState.d.ts +9 -0
- package/types/hooks/useSyncedRef.d.ts +11 -0
- package/types/hooks/useUnmountEffect.d.ts +8 -0
- package/types/hooks/useWindowSize.d.ts +30 -0
- package/types/index.d.ts +2 -0
- package/types/utils/contextBuilder.d.ts +18 -0
- package/types/utils/index.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Proedis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
5
|
+
var useSafeState = require('./useSafeState.js');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
var n = Object.create(null);
|
|
9
|
+
if (e) {
|
|
10
|
+
Object.keys(e).forEach(function (k) {
|
|
11
|
+
if (k !== 'default') {
|
|
12
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return e[k]; }
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
|
+
|
|
26
|
+
/* --------
|
|
27
|
+
* Hook Function
|
|
28
|
+
* -------- */
|
|
29
|
+
/**
|
|
30
|
+
* Augmented useState with AutoControlled option.
|
|
31
|
+
* Use this hook if the controlling of this state
|
|
32
|
+
* will be optionally demanded externally and
|
|
33
|
+
* potentially controlled by a prop or an arbitrary value
|
|
34
|
+
*
|
|
35
|
+
* @param initialState The initial state of the AutoControlled State
|
|
36
|
+
* @param options Options to pass controlled value and default value
|
|
37
|
+
*/
|
|
38
|
+
function useAutoControlledState(initialState, options) {
|
|
39
|
+
/**
|
|
40
|
+
* Get the user defined options to check if
|
|
41
|
+
* the state will be auto-controlled or
|
|
42
|
+
* controlled externally by user
|
|
43
|
+
*/
|
|
44
|
+
const { value, defaultValue } = options || {};
|
|
45
|
+
/**
|
|
46
|
+
* To avoid the useCallback change every time
|
|
47
|
+
* the value change, and let invalidate the
|
|
48
|
+
* other hooks, depending on it, save the
|
|
49
|
+
* AutoControlled state into an internal ref
|
|
50
|
+
*/
|
|
51
|
+
const isAutoControlled = React__namespace.useRef(value === undefined);
|
|
52
|
+
/**
|
|
53
|
+
* Update the isAutoControlled value any time the
|
|
54
|
+
* value will change
|
|
55
|
+
*/
|
|
56
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
57
|
+
isAutoControlled.current = value === undefined;
|
|
58
|
+
}, [value]);
|
|
59
|
+
/**
|
|
60
|
+
* As this hook will be principally used on a component
|
|
61
|
+
* with value (like input, checkbox) or in component
|
|
62
|
+
* with an external controllable state (like modal),
|
|
63
|
+
* use the SafeState hook to avoid state change when the
|
|
64
|
+
* component is unmounted
|
|
65
|
+
*/
|
|
66
|
+
const [state, setState] = useSafeState.useSafeState(() => {
|
|
67
|
+
/** Get initial value from user props */
|
|
68
|
+
const initialProp = value === undefined ? defaultValue : undefined;
|
|
69
|
+
/** If initialProps is undefined, could return it */
|
|
70
|
+
if (initialProp !== undefined) {
|
|
71
|
+
return initialProp;
|
|
72
|
+
}
|
|
73
|
+
/** Else, return the initialState provided within the function */
|
|
74
|
+
return typeof initialState === 'function'
|
|
75
|
+
? initialState()
|
|
76
|
+
: initialState;
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* Create a new function that could be used to attempt to modify
|
|
80
|
+
* the state object, only if the state is not being controlled
|
|
81
|
+
* by the external property
|
|
82
|
+
*/
|
|
83
|
+
const trySetState = React__namespace.useCallback((newState) => {
|
|
84
|
+
/** Update the state only if current prop is not */
|
|
85
|
+
if (isAutoControlled.current) {
|
|
86
|
+
setState(newState);
|
|
87
|
+
}
|
|
88
|
+
}, [setState]);
|
|
89
|
+
/** Return the Hook Result with state and utilities */
|
|
90
|
+
return [
|
|
91
|
+
value === undefined ? state : value,
|
|
92
|
+
trySetState,
|
|
93
|
+
setState
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
exports.useAutoControlledState = useAutoControlledState;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
4
|
+
var useSyncedRef = require('./useSyncedRef.js');
|
|
5
|
+
|
|
6
|
+
/** The default events name to listen to */
|
|
7
|
+
const DEFAULT_EVENTS = [
|
|
8
|
+
'mousedown',
|
|
9
|
+
'touchstart'
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* Use this hook to fire an event every time a click occurs outside
|
|
13
|
+
* the provided target element
|
|
14
|
+
* @param target
|
|
15
|
+
* @param callback
|
|
16
|
+
* @param events
|
|
17
|
+
*/
|
|
18
|
+
function useClickOutside(target, callback, events = DEFAULT_EVENTS) {
|
|
19
|
+
/** Wrap dependent data into synced ref to maintain immutable */
|
|
20
|
+
const targetElement = useSyncedRef.useSyncedRef(target);
|
|
21
|
+
const callbackFunction = useSyncedRef.useSyncedRef(callback);
|
|
22
|
+
/** Use the Enhanced Effect to attach event listeners */
|
|
23
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
24
|
+
/**
|
|
25
|
+
* Internal function used to check if the event
|
|
26
|
+
* target received from the listener it is contained within the
|
|
27
|
+
* requested target element provided within the hook
|
|
28
|
+
*
|
|
29
|
+
* @param event HTML Event received
|
|
30
|
+
*/
|
|
31
|
+
function handler(event) {
|
|
32
|
+
/** Assert the target element exists */
|
|
33
|
+
if (!targetElement.current.current) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
/** Extract the event target */
|
|
37
|
+
const { target: eventTarget } = event;
|
|
38
|
+
const cb = callbackFunction.current;
|
|
39
|
+
if (!eventTarget
|
|
40
|
+
|| (!!eventTarget && !targetElement.current.current.contains(eventTarget))) {
|
|
41
|
+
/** Call the callback */
|
|
42
|
+
cb.call(this, event);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Attach the events on all requested keys */
|
|
46
|
+
events.forEach((eventName) => {
|
|
47
|
+
/** List for event directly on document element */
|
|
48
|
+
document.addEventListener(eventName, handler, { passive: true });
|
|
49
|
+
});
|
|
50
|
+
/** On effect clear, remove all events */
|
|
51
|
+
return () => {
|
|
52
|
+
events.forEach((eventName) => {
|
|
53
|
+
/** List for event directly on document element */
|
|
54
|
+
document.removeEventListener(eventName, handler);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
59
|
+
[callbackFunction, targetElement, ...events]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
exports.useClickOutside = useClickOutside;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var useSyncedRef = require('./useSyncedRef.js');
|
|
5
|
+
var useUnmountEffect = require('./useUnmountEffect.js');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
var n = Object.create(null);
|
|
9
|
+
if (e) {
|
|
10
|
+
Object.keys(e).forEach(function (k) {
|
|
11
|
+
if (k !== 'default') {
|
|
12
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return e[k]; }
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Return a wrapped Debounced variant function of the
|
|
28
|
+
* provided function.
|
|
29
|
+
* Interval param will delay the returned function call
|
|
30
|
+
* if a next invoking will be requested
|
|
31
|
+
*
|
|
32
|
+
* @param callback The function to call
|
|
33
|
+
* @param deps Array dependencies list
|
|
34
|
+
* @param interval The interval time
|
|
35
|
+
*/
|
|
36
|
+
function useDebouncedCallback(callback, deps, interval) {
|
|
37
|
+
/** Prepare the timeout ref */
|
|
38
|
+
const timeout = React__namespace.useRef();
|
|
39
|
+
const debounceCallback = useSyncedRef.useSyncedRef(callback);
|
|
40
|
+
/** Set up a function that could be used to clear timeout */
|
|
41
|
+
const clearTimeoutRef = React__namespace.useCallback(() => {
|
|
42
|
+
if (timeout.current) {
|
|
43
|
+
clearTimeout(timeout.current);
|
|
44
|
+
timeout.current = undefined;
|
|
45
|
+
}
|
|
46
|
+
}, []);
|
|
47
|
+
/** Cleanup the timeout on unmount */
|
|
48
|
+
useUnmountEffect.useUnmountEffect(() => {
|
|
49
|
+
clearTimeoutRef();
|
|
50
|
+
});
|
|
51
|
+
/** Return a new wrapped function that will set up the timeout on call */
|
|
52
|
+
return React__namespace.useCallback((...args) => {
|
|
53
|
+
/** If the timeout already exists, clear it */
|
|
54
|
+
clearTimeoutRef();
|
|
55
|
+
/** Set a new timeout to wait for function call */
|
|
56
|
+
timeout.current = setTimeout(() => {
|
|
57
|
+
debounceCallback.current(...args);
|
|
58
|
+
}, interval);
|
|
59
|
+
}, [clearTimeoutRef, interval, debounceCallback]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
exports.useDebouncedCallback = useDebouncedCallback;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/* --------
|
|
4
|
+
* Hook Definition
|
|
5
|
+
* -------- */
|
|
6
|
+
/**
|
|
7
|
+
* Get the ElementType that must be used to render a Component
|
|
8
|
+
*
|
|
9
|
+
* @param Component The Component to render
|
|
10
|
+
* @param props Props received by Component
|
|
11
|
+
* @param defaultElementType A default element type to use
|
|
12
|
+
* @param getDefault A defined function used to compute the ElementType by props
|
|
13
|
+
*/
|
|
14
|
+
function useElementType(Component, props, defaultElementType, getDefault) {
|
|
15
|
+
/** Get default props directly from Component */
|
|
16
|
+
const { defaultProps: componentDefaultProps } = Component;
|
|
17
|
+
/** If received props contain the element type, use it to render the component */
|
|
18
|
+
if (props.as && props.as !== (componentDefaultProps === null || componentDefaultProps === void 0 ? void 0 : componentDefaultProps.as)) {
|
|
19
|
+
return props.as;
|
|
20
|
+
}
|
|
21
|
+
/** If a function has been provided to compute the element type, call it and return the element if exists */
|
|
22
|
+
if (typeof getDefault === 'function') {
|
|
23
|
+
const computedElementType = getDefault(props);
|
|
24
|
+
if (computedElementType) {
|
|
25
|
+
return computedElementType;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** If getDefault is an object than is a ElementType mapper using props, compute it and return */
|
|
29
|
+
if (typeof getDefault === 'object') {
|
|
30
|
+
return getDefault.using[props[getDefault.map]] || getDefault.default;
|
|
31
|
+
}
|
|
32
|
+
/** If props include a href key, return anchor element as the element type */
|
|
33
|
+
if (props.href) {
|
|
34
|
+
return 'a';
|
|
35
|
+
}
|
|
36
|
+
/** Return element type, falling back to a 'div' element */
|
|
37
|
+
return (componentDefaultProps === null || componentDefaultProps === void 0 ? void 0 : componentDefaultProps.as) || defaultElementType || 'div';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.useElementType = useElementType;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var utils = require('@proedis/utils');
|
|
5
|
+
|
|
6
|
+
function _interopNamespaceDefault(e) {
|
|
7
|
+
var n = Object.create(null);
|
|
8
|
+
if (e) {
|
|
9
|
+
Object.keys(e).forEach(function (k) {
|
|
10
|
+
if (k !== 'default') {
|
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return e[k]; }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Return the best way to use the native
|
|
27
|
+
* useEffect React hook, getting the
|
|
28
|
+
* useLayoutEffect if current code is executing
|
|
29
|
+
* on a real browser client
|
|
30
|
+
*/
|
|
31
|
+
const useEnhancedEffect = utils.isBrowser
|
|
32
|
+
? React__namespace.useLayoutEffect
|
|
33
|
+
: React__namespace.useEffect;
|
|
34
|
+
|
|
35
|
+
exports.useEnhancedEffect = useEnhancedEffect;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
4
|
+
var useSyncedRef = require('./useSyncedRef.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Attach a HTMLElement lister to a target DOM Element.
|
|
8
|
+
*
|
|
9
|
+
* @param name The event to listen to
|
|
10
|
+
* @param callback The callback function to call
|
|
11
|
+
* @param options Some options to customize the hook
|
|
12
|
+
*/
|
|
13
|
+
function useEvent(name, callback, options) {
|
|
14
|
+
/** Get options and set defaults */
|
|
15
|
+
const { disabled = false, target = document.documentElement } = options || {};
|
|
16
|
+
/**
|
|
17
|
+
* Every time the hook will be called update the handler
|
|
18
|
+
* function in the ref object.
|
|
19
|
+
* This logic will help the event listener to get the last
|
|
20
|
+
* version update of the handler, without having the need to save it
|
|
21
|
+
* into react use effect dependencies
|
|
22
|
+
*/
|
|
23
|
+
const handler = useSyncedRef.useSyncedRef(callback);
|
|
24
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
25
|
+
/** If hook has been disabled, avoid the event listen */
|
|
26
|
+
if (disabled) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Wrap the handler function into a local scoped function to be removed
|
|
31
|
+
* once the effect will be cleared.
|
|
32
|
+
* HEADS UP, using the useRef method to save the handler; the function
|
|
33
|
+
* could not be the same reference as the component mount
|
|
34
|
+
*/
|
|
35
|
+
const eventCallback = (event) => (handler.current.apply(target, [event]));
|
|
36
|
+
/** Attach the event listener to the DOM target */
|
|
37
|
+
target.addEventListener(name, eventCallback);
|
|
38
|
+
/** On effect clear, remove the event attached to the target */
|
|
39
|
+
return () => {
|
|
40
|
+
target.removeEventListener(name, eventCallback);
|
|
41
|
+
};
|
|
42
|
+
}, [disabled, name, target, handler]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.useEvent = useEvent;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
var n = Object.create(null);
|
|
7
|
+
if (e) {
|
|
8
|
+
Object.keys(e).forEach(function (k) {
|
|
9
|
+
if (k !== 'default') {
|
|
10
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return e[k]; }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Group multiple ref object into a single ref that will
|
|
26
|
+
* assign the referenced element to each single ref.
|
|
27
|
+
*
|
|
28
|
+
* @param refs
|
|
29
|
+
*/
|
|
30
|
+
function useForkRef(...refs) {
|
|
31
|
+
return React__namespace.useCallback((refValue) => {
|
|
32
|
+
refs.forEach((ref) => ref.current = refValue !== null && refValue !== void 0 ? refValue : undefined);
|
|
33
|
+
},
|
|
34
|
+
// react-hooks/exhaustive-deps will warn the next line because
|
|
35
|
+
// the array will always change at each call of the hook.
|
|
36
|
+
// In this case, could disable the warning because the ref hook used
|
|
37
|
+
// as arguments won't change, because ref objects are immutable
|
|
38
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
39
|
+
[...refs]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.useForkRef = useForkRef;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
5
|
+
|
|
6
|
+
function _interopNamespaceDefault(e) {
|
|
7
|
+
var n = Object.create(null);
|
|
8
|
+
if (e) {
|
|
9
|
+
Object.keys(e).forEach(function (k) {
|
|
10
|
+
if (k !== 'default') {
|
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return e[k]; }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Hook that could be used to wrap the original
|
|
27
|
+
* setState hook function to be called and to change
|
|
28
|
+
* state only if the using component is mounted
|
|
29
|
+
*
|
|
30
|
+
* @param initialState Initial state to use
|
|
31
|
+
*/
|
|
32
|
+
function useSafeState(initialState) {
|
|
33
|
+
/** Init internal state using default React hook */
|
|
34
|
+
const [state, setState] = React__namespace.useState(initialState);
|
|
35
|
+
/** Use a ref hook to check component mount */
|
|
36
|
+
const isMounted = React__namespace.useRef(false);
|
|
37
|
+
/** Update the ref on component mount/unmount */
|
|
38
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
39
|
+
isMounted.current = true;
|
|
40
|
+
return () => {
|
|
41
|
+
isMounted.current = false;
|
|
42
|
+
};
|
|
43
|
+
}, []);
|
|
44
|
+
/** Wrap original setState action to consider mounted state */
|
|
45
|
+
const wrappedSeState = React__namespace.useCallback((newState) => {
|
|
46
|
+
if (isMounted.current) {
|
|
47
|
+
setState(newState);
|
|
48
|
+
}
|
|
49
|
+
}, []);
|
|
50
|
+
/** Return state and wrapped set state */
|
|
51
|
+
return [state, wrappedSeState];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.useSafeState = useSafeState;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
var n = Object.create(null);
|
|
7
|
+
if (e) {
|
|
8
|
+
Object.keys(e).forEach(function (k) {
|
|
9
|
+
if (k !== 'default') {
|
|
10
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return e[k]; }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An augmented version of the useRef function.
|
|
26
|
+
* It will return a 'ref' style object with the
|
|
27
|
+
* current key memoized, returning each time
|
|
28
|
+
* the most updated version of the value
|
|
29
|
+
*
|
|
30
|
+
* @param value The value to store into ref object
|
|
31
|
+
*/
|
|
32
|
+
function useSyncedRef(value) {
|
|
33
|
+
const ref = React__namespace.useRef(value);
|
|
34
|
+
ref.current = value;
|
|
35
|
+
return React__namespace.useMemo(() => Object.freeze({
|
|
36
|
+
get current() {
|
|
37
|
+
return ref.current;
|
|
38
|
+
}
|
|
39
|
+
}), []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.useSyncedRef = useSyncedRef;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
4
|
+
var useSyncedRef = require('./useSyncedRef.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Use this hook to call the provided
|
|
8
|
+
* callable function to be called once the
|
|
9
|
+
* component will unmount
|
|
10
|
+
*
|
|
11
|
+
* @param callback Function to be called
|
|
12
|
+
*/
|
|
13
|
+
function useUnmountEffect(callback) {
|
|
14
|
+
/** Save the callback function into an immutable object */
|
|
15
|
+
const unmountCallback = useSyncedRef.useSyncedRef(callback);
|
|
16
|
+
/** Attach an event to call function on unmount */
|
|
17
|
+
useEnhancedEffect.useEnhancedEffect(() => () => {
|
|
18
|
+
unmountCallback.current();
|
|
19
|
+
}, [unmountCallback]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.useUnmountEffect = useUnmountEffect;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
5
|
+
|
|
6
|
+
function _interopNamespaceDefault(e) {
|
|
7
|
+
var n = Object.create(null);
|
|
8
|
+
if (e) {
|
|
9
|
+
Object.keys(e).forEach(function (k) {
|
|
10
|
+
if (k !== 'default') {
|
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return e[k]; }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Monitor the change of the Window Size.
|
|
27
|
+
* Take careful that the object returned by the hook will never change:
|
|
28
|
+
* to use value within hook dependencies must deconstruct internal properties
|
|
29
|
+
*
|
|
30
|
+
* @param options Set hook options
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Do this
|
|
34
|
+
* const { height, width } = useWindowSize();
|
|
35
|
+
*
|
|
36
|
+
* React.useEffect(
|
|
37
|
+
* () => {
|
|
38
|
+
* // Wonderful code...
|
|
39
|
+
* },
|
|
40
|
+
* [ height, width ]
|
|
41
|
+
* );
|
|
42
|
+
*/
|
|
43
|
+
function useWindowSize(options) {
|
|
44
|
+
const { disabled } = options || {};
|
|
45
|
+
// ----
|
|
46
|
+
// Internal State
|
|
47
|
+
// ----
|
|
48
|
+
const [currentSize, setCurrentSize] = React__namespace.useState(() => ({
|
|
49
|
+
height: typeof window !== 'undefined' ? window.innerHeight : 0,
|
|
50
|
+
width: typeof window !== 'undefined' ? window.innerWidth : 0
|
|
51
|
+
}));
|
|
52
|
+
// ----
|
|
53
|
+
// Handler
|
|
54
|
+
// ----
|
|
55
|
+
const handleWindowSizeChange = React__namespace.useCallback(() => setCurrentSize((curr) => {
|
|
56
|
+
curr.height = window.innerHeight;
|
|
57
|
+
curr.width = window.innerWidth;
|
|
58
|
+
return curr;
|
|
59
|
+
}), []);
|
|
60
|
+
// ----
|
|
61
|
+
// Internal Hooks
|
|
62
|
+
// ----
|
|
63
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
64
|
+
/** If hook has been disabled, avoid the event listen */
|
|
65
|
+
if (disabled) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
/** Attach event to the window element to listen for resize change */
|
|
69
|
+
window.addEventListener('resize', handleWindowSizeChange);
|
|
70
|
+
/** On effect clear, remove the event attached to the window */
|
|
71
|
+
return () => {
|
|
72
|
+
window.removeEventListener('resize', handleWindowSizeChange);
|
|
73
|
+
};
|
|
74
|
+
}, [disabled, handleWindowSizeChange]);
|
|
75
|
+
// ----
|
|
76
|
+
// Hook returns
|
|
77
|
+
// ----
|
|
78
|
+
return currentSize;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
exports.useWindowSize = useWindowSize;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var useAutoControlledState = require('./hooks/useAutoControlledState.js');
|
|
4
|
+
var useClickOutside = require('./hooks/useClickOutside.js');
|
|
5
|
+
var useDebouncedCallback = require('./hooks/useDebouncedCallback.js');
|
|
6
|
+
var useElementType = require('./hooks/useElementType.js');
|
|
7
|
+
var useEnhancedEffect = require('./hooks/useEnhancedEffect.js');
|
|
8
|
+
var useEvent = require('./hooks/useEvent.js');
|
|
9
|
+
var useForkRef = require('./hooks/useForkRef.js');
|
|
10
|
+
var useSafeState = require('./hooks/useSafeState.js');
|
|
11
|
+
var useSyncedRef = require('./hooks/useSyncedRef.js');
|
|
12
|
+
var useUnmountEffect = require('./hooks/useUnmountEffect.js');
|
|
13
|
+
var useWindowSize = require('./hooks/useWindowSize.js');
|
|
14
|
+
var contextBuilder = require('./utils/contextBuilder.js');
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
exports.useAutoControlledState = useAutoControlledState.useAutoControlledState;
|
|
19
|
+
exports.useClickOutside = useClickOutside.useClickOutside;
|
|
20
|
+
exports.useDebouncedCallback = useDebouncedCallback.useDebouncedCallback;
|
|
21
|
+
exports.useElementType = useElementType.useElementType;
|
|
22
|
+
exports.useEnhancedEffect = useEnhancedEffect.useEnhancedEffect;
|
|
23
|
+
exports.useEvent = useEvent.useEvent;
|
|
24
|
+
exports.useForkRef = useForkRef.useForkRef;
|
|
25
|
+
exports.useSafeState = useSafeState.useSafeState;
|
|
26
|
+
exports.useSyncedRef = useSyncedRef.useSyncedRef;
|
|
27
|
+
exports.useUnmountEffect = useUnmountEffect.useUnmountEffect;
|
|
28
|
+
exports.useWindowSize = useWindowSize.useWindowSize;
|
|
29
|
+
exports.contextBuilder = contextBuilder.contextBuilder;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
var n = Object.create(null);
|
|
7
|
+
if (e) {
|
|
8
|
+
Object.keys(e).forEach(function (k) {
|
|
9
|
+
if (k !== 'default') {
|
|
10
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return e[k]; }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Easy creation of a React Context providing the name to use
|
|
26
|
+
* and optionally an initial context.
|
|
27
|
+
* Object returned will reformat string and return well-named function and components
|
|
28
|
+
* @param name
|
|
29
|
+
* @param initialContext
|
|
30
|
+
*/
|
|
31
|
+
function contextBuilder(name, initialContext) {
|
|
32
|
+
/** Create the base context using provided data */
|
|
33
|
+
const BaseContext = React__namespace.createContext(initialContext);
|
|
34
|
+
/** Create the capitalized name */
|
|
35
|
+
const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
36
|
+
/** Create the hook function to return */
|
|
37
|
+
const useContextHook = () => {
|
|
38
|
+
/** Get the context value using built in hook */
|
|
39
|
+
const contextValue = React__namespace.useContext(BaseContext);
|
|
40
|
+
/** Assert the context value exists */
|
|
41
|
+
if (!contextValue) {
|
|
42
|
+
throw new Error(`use${capitalizedName}() hook must be invoked inside its right Context`);
|
|
43
|
+
}
|
|
44
|
+
/** Return extracted value */
|
|
45
|
+
return contextValue;
|
|
46
|
+
};
|
|
47
|
+
/** Assign name to Context objects */
|
|
48
|
+
BaseContext.displayName = capitalizedName;
|
|
49
|
+
/** Return context tools */
|
|
50
|
+
const contextHook = { [`use${capitalizedName}`]: useContextHook };
|
|
51
|
+
const contextProvider = { [`${capitalizedName}Provider`]: BaseContext.Provider };
|
|
52
|
+
const contextConsumer = { [`${capitalizedName}Consumer`]: BaseContext.Consumer };
|
|
53
|
+
return Object.assign(Object.assign(Object.assign({}, contextHook), contextProvider), contextConsumer);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.contextBuilder = contextBuilder;
|