You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.5 KiB
53 lines
1.5 KiB
import * as React from 'react';
|
|
|
|
/**
|
|
* Takes an argument and if it's an array, returns the first item in the array,
|
|
* otherwise returns the argument. Used for Preact compatibility.
|
|
*/
|
|
export var unwrapArray = function unwrapArray(arg) {
|
|
return Array.isArray(arg) ? arg[0] : arg;
|
|
};
|
|
/**
|
|
* Takes a maybe-undefined function and arbitrary args and invokes the function
|
|
* only if it is defined.
|
|
*/
|
|
|
|
export var safeInvoke = function safeInvoke(fn) {
|
|
if (typeof fn === 'function') {
|
|
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
args[_key - 1] = arguments[_key];
|
|
}
|
|
|
|
return fn.apply(void 0, args);
|
|
}
|
|
};
|
|
/**
|
|
* Sets a ref using either a ref callback or a ref object
|
|
*/
|
|
|
|
export var setRef = function setRef(ref, node) {
|
|
// if its a function call it
|
|
if (typeof ref === 'function') {
|
|
return safeInvoke(ref, node);
|
|
} // otherwise we should treat it as a ref object
|
|
else if (ref != null) {
|
|
ref.current = node;
|
|
}
|
|
};
|
|
/**
|
|
* Simple ponyfill for Object.fromEntries
|
|
*/
|
|
|
|
export var fromEntries = function fromEntries(entries) {
|
|
return entries.reduce(function (acc, _ref) {
|
|
var key = _ref[0],
|
|
value = _ref[1];
|
|
acc[key] = value;
|
|
return acc;
|
|
}, {});
|
|
};
|
|
/**
|
|
* Small wrapper around `useLayoutEffect` to get rid of the warning on SSR envs
|
|
*/
|
|
|
|
export var useIsomorphicLayoutEffect = typeof window !== 'undefined' && window.document && window.document.createElement ? React.useLayoutEffect : React.useEffect; |