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

147 lines (112 loc) 8.73 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.useNestedDialogs = useNestedDialogs; var React = _interopRequireWildcard(require("react")); var _removeItemFromArray = require("reakit-utils/removeItemFromArray"); var _jsxRuntime = require("react/jsx-runtime"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 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; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var DialogContext = /*#__PURE__*/React.createContext({}); function useNestedDialogs(dialogRef, options) { var context = React.useContext(DialogContext); var _React$useState = React.useState([]), _React$useState2 = _slicedToArray(_React$useState, 2), dialogs = _React$useState2[0], setDialogs = _React$useState2[1]; var _React$useState3 = React.useState(dialogs), _React$useState4 = _slicedToArray(_React$useState3, 2), visibleModals = _React$useState4[0], setVisibleModals = _React$useState4[1]; var addDialog = React.useCallback(function (ref) { var _context$addDialog; (_context$addDialog = context.addDialog) === null || _context$addDialog === void 0 ? void 0 : _context$addDialog.call(context, ref); setDialogs(function (prevDialogs) { return [].concat(_toConsumableArray(prevDialogs), [ref]); }); }, // eslint-disable-next-line react-hooks/exhaustive-deps [context.addDialog]); var removeDialog = React.useCallback(function (ref) { var _context$removeDialog; (_context$removeDialog = context.removeDialog) === null || _context$removeDialog === void 0 ? void 0 : _context$removeDialog.call(context, ref); setDialogs(function (prevDialogs) { return (0, _removeItemFromArray.removeItemFromArray)(prevDialogs, ref); }); }, // eslint-disable-next-line react-hooks/exhaustive-deps [context.removeDialog]); var showDialog = React.useCallback(function (ref) { var _context$showDialog; (_context$showDialog = context.showDialog) === null || _context$showDialog === void 0 ? void 0 : _context$showDialog.call(context, ref); setVisibleModals(function (prevDialogs) { return [].concat(_toConsumableArray(prevDialogs), [ref]); }); }, // eslint-disable-next-line react-hooks/exhaustive-deps [context.showDialog]); var hideDialog = React.useCallback(function (ref) { var _context$hideDialog; (_context$hideDialog = context.hideDialog) === null || _context$hideDialog === void 0 ? void 0 : _context$hideDialog.call(context, ref); setVisibleModals(function (prevDialogs) { return (0, _removeItemFromArray.removeItemFromArray)(prevDialogs, ref); }); }, // eslint-disable-next-line react-hooks/exhaustive-deps [context.hideDialog]); // If it's a nested dialog, add it to context React.useEffect(function () { var _context$addDialog2; if (options.unstable_orphan) return undefined; (_context$addDialog2 = context.addDialog) === null || _context$addDialog2 === void 0 ? void 0 : _context$addDialog2.call(context, dialogRef); return function () { var _context$removeDialog2; (_context$removeDialog2 = context.removeDialog) === null || _context$removeDialog2 === void 0 ? void 0 : _context$removeDialog2.call(context, dialogRef); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [options.unstable_orphan, context.addDialog, dialogRef, context.removeDialog]); React.useEffect(function () { var _context$showDialog2; if (options.unstable_orphan) return undefined; if (!options.modal) return undefined; if (!options.visible) return undefined; (_context$showDialog2 = context.showDialog) === null || _context$showDialog2 === void 0 ? void 0 : _context$showDialog2.call(context, dialogRef); return function () { var _context$hideDialog2; (_context$hideDialog2 = context.hideDialog) === null || _context$hideDialog2 === void 0 ? void 0 : _context$hideDialog2.call(context, dialogRef); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [options.unstable_orphan, options.modal, options.visible, context.showDialog, dialogRef, context.hideDialog]); // Close all nested dialogs when parent dialog closes React.useEffect(function () { if (context.visible === false && options.visible && !options.unstable_orphan) { var _options$hide; (_options$hide = options.hide) === null || _options$hide === void 0 ? void 0 : _options$hide.call(options); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [context.visible, options.visible, options.hide, options.unstable_orphan]); // Provider var providerValue = React.useMemo(function () { return { visible: options.visible, addDialog: addDialog, removeDialog: removeDialog, showDialog: showDialog, hideDialog: hideDialog }; }, [options.visible, addDialog, removeDialog, showDialog, hideDialog]); var wrap = React.useCallback(function (element) { return /*#__PURE__*/(0, _jsxRuntime.jsx)(DialogContext.Provider, { value: providerValue, children: element }); }, [providerValue]); return { dialogs: dialogs, visibleModals: visibleModals, wrap: wrap }; } //# sourceMappingURL=useNestedDialogs.js.map