UNPKG

@renderlesskit/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

162 lines (126 loc) 6.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createHook = createHook; var _reakitSystem = require("reakit-system"); var _reakitUtils = require("reakit-utils"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } /** * Creates a React custom hook that will return component props. * * @example * import { createHook } from "../system"; * * const useA = createHook({ * name: "A", * keys: ["url"], // custom props/options keys * useProps(options, htmlProps) { * return { * ...htmlProps, * href: options.url, * }; * }, * }); * * function A({ url, ...htmlProps }) { * const props = useA({ url }, htmlProps); * return <a {...props} />; * } * * @param options */ function createHook(options) { var _options$useState, _composedHooks$; var composedHooks = (0, _reakitUtils.toArray)(options.compose); var __useOptions = function __useOptions(hookOptions, htmlProps) { // Call the current hook's useOptions first if (options.useOptions) { hookOptions = options.useOptions(hookOptions, htmlProps); } // If there's name, call useOptions from the system context if (options.name) { // eslint-disable-next-line react-hooks/rules-of-hooks hookOptions = (0, _reakitSystem.useOptions)(options.name, hookOptions, htmlProps); } // Run composed hooks useOptions if (options.compose) { var _iterator = _createForOfIteratorHelper(composedHooks), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var hook = _step.value; hookOptions = hook.__useOptions(hookOptions, htmlProps); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } return hookOptions; }; var useHook = function useHook() { var hookOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var htmlProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var unstable_ignoreUseOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; // This won't execute when useHook was called from within another useHook if (!unstable_ignoreUseOptions) { hookOptions = __useOptions(hookOptions, htmlProps); } // Call the current hook's useProps if (options.useProps) { htmlProps = options.useProps(hookOptions, htmlProps); } // If there's name, call useProps from the system context if (options.name) { // eslint-disable-next-line react-hooks/rules-of-hooks htmlProps = (0, _reakitSystem.useProps)(options.name, hookOptions, htmlProps); } if (options.compose) { if (options.useComposeOptions) { hookOptions = options.useComposeOptions(hookOptions, htmlProps); } if (options.useComposeProps) { htmlProps = options.useComposeProps(hookOptions, htmlProps); } else { var _iterator2 = _createForOfIteratorHelper(composedHooks), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var hook = _step2.value; htmlProps = hook(hookOptions, htmlProps, true); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } } // Remove undefined values from htmlProps var finalHTMLProps = {}; var definedHTMLProps = htmlProps || {}; for (var prop in definedHTMLProps) { if (definedHTMLProps[prop] !== undefined) { finalHTMLProps[prop] = definedHTMLProps[prop]; } } return finalHTMLProps; }; useHook.__useOptions = __useOptions; var composedKeys = composedHooks.reduce(function (keys, hook) { keys.push.apply(keys, _toConsumableArray(hook.__keys || [])); return keys; }, []); // It's used by createComponent to split option props (keys) and html props useHook.__keys = [].concat(_toConsumableArray(composedKeys), _toConsumableArray(((_options$useState = options.useState) === null || _options$useState === void 0 ? void 0 : _options$useState.__keys) || []), _toConsumableArray(options.keys || [])); useHook.unstable_propsAreEqual = options.propsAreEqual || ((_composedHooks$ = composedHooks[0]) === null || _composedHooks$ === void 0 ? void 0 : _composedHooks$.unstable_propsAreEqual) || _reakitUtils.shallowEqual; if (options.name) { Object.defineProperty(useHook, "name", { value: "use".concat(options.name) }); } return useHook; } //# sourceMappingURL=createHook.js.map