@activecollab/components
Version:
ActiveCollab Components
139 lines (138 loc) • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Breadcrumbs = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Styles = require("./Styles");
var _utils = require("../../utils");
var _IconButton = require("../IconButton");
var _Icons = require("../Icons");
var _List = require("../List");
var _Menu = require("../Menu");
var _excluded = ["children", "className", "separator"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var Breadcrumbs = exports.Breadcrumbs = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
var _dimensions$width, _childDimensions$widt;
var children = _ref.children,
className = _ref.className,
_ref$separator = _ref.separator,
separator = _ref$separator === void 0 ? "/" : _ref$separator,
rest = _objectWithoutProperties(_ref, _excluded);
var internalRef = (0, _react.useRef)(null);
var listWrapperRef = (0, _react.useRef)(null);
var olRef = (0, _react.useRef)(null);
var widthCollectionRef = (0, _react.useRef)([]);
var handleRef = (0, _utils.useForkRef)(ref, internalRef);
var _useState = (0, _react.useState)(0),
_useState2 = _slicedToArray(_useState, 2),
menuBreadcrumbs = _useState2[0],
setMenuBreadcrumbs = _useState2[1];
var dimensions = (0, _utils.useResizeObserver)(internalRef);
var childDimensions = (0, _utils.useResizeObserver)(listWrapperRef);
var parentWidth = (_dimensions$width = dimensions === null || dimensions === void 0 ? void 0 : dimensions.width) !== null && _dimensions$width !== void 0 ? _dimensions$width : 0;
var childWidth = (_childDimensions$widt = childDimensions === null || childDimensions === void 0 ? void 0 : childDimensions.width) !== null && _childDimensions$widt !== void 0 ? _childDimensions$widt : 0;
var childrenCollection = (0, _react.useMemo)(function () {
return _react.default.Children.toArray(children);
}, [children]);
var _useMemo = (0, _react.useMemo)(function () {
return [childrenCollection.slice(0, menuBreadcrumbs), childrenCollection.slice(menuBreadcrumbs, childrenCollection.length)];
}, [childrenCollection, menuBreadcrumbs]),
_useMemo2 = _slicedToArray(_useMemo, 2),
menuCollection = _useMemo2[0],
listCollection = _useMemo2[1];
(0, _react.useEffect)(function () {
widthCollectionRef.current = [];
setMenuBreadcrumbs(0);
}, [childrenCollection.length]);
(0, _react.useEffect)(function () {
var diffWidth = childWidth - parentWidth;
if (diffWidth > 0) {
var _offsetWidth, _olRef$current, _olRef$current$queryS, _olRef$current2, _breadcrumbItems$leng;
var separatorWidth = (_offsetWidth = (_olRef$current = olRef.current) === null || _olRef$current === void 0 || (_olRef$current = _olRef$current.querySelectorAll("[data-role=\"separator\"]")[0]) === null || _olRef$current === void 0 ? void 0 : _olRef$current.offsetWidth) !== null && _offsetWidth !== void 0 ? _offsetWidth : 0;
var breadcrumbItems = (_olRef$current$queryS = (_olRef$current2 = olRef.current) === null || _olRef$current2 === void 0 ? void 0 : _olRef$current2.querySelectorAll("[data-role=\"item\"]")) !== null && _olRef$current$queryS !== void 0 ? _olRef$current$queryS : [];
var childWidthCollection = [];
var maxIndex = ((_breadcrumbItems$leng = breadcrumbItems === null || breadcrumbItems === void 0 ? void 0 : breadcrumbItems.length) !== null && _breadcrumbItems$leng !== void 0 ? _breadcrumbItems$leng : 1) - 1;
var currentIndex = 0;
var childSumWidth = 0;
while (diffWidth > childSumWidth && maxIndex >= currentIndex) {
var _offsetWidth2, _breadcrumbItems$curr;
var _childWidth = (_offsetWidth2 = (_breadcrumbItems$curr = breadcrumbItems[currentIndex]) === null || _breadcrumbItems$curr === void 0 ? void 0 : _breadcrumbItems$curr.offsetWidth) !== null && _offsetWidth2 !== void 0 ? _offsetWidth2 : 0;
var result = separatorWidth + _childWidth;
childWidthCollection.push(result);
childSumWidth += result;
currentIndex += 1;
}
if (childWidthCollection.length === listCollection.length) {
childWidthCollection.pop();
}
widthCollectionRef.current = [...widthCollectionRef.current, ...childWidthCollection];
setMenuBreadcrumbs(function (oldVal) {
return oldVal + childWidthCollection.length;
});
}
}, [listCollection.length, parentWidth, childWidth]);
(0, _react.useEffect)(function () {
var diffWidth = parentWidth - childWidth;
if (diffWidth > 0 && widthCollectionRef.current.length > 0) {
var total = widthCollectionRef.current.length;
var collection = [...widthCollectionRef.current];
var sumWidth = 0;
while (diffWidth > sumWidth && collection.length > 0) {
var _collection;
var lastWidth = (_collection = collection[collection.length - 1]) !== null && _collection !== void 0 ? _collection : 0;
if (diffWidth < lastWidth + sumWidth) {
sumWidth = diffWidth;
} else {
var _collection$pop;
sumWidth += (_collection$pop = collection.pop()) !== null && _collection$pop !== void 0 ? _collection$pop : 0;
}
}
var totalToDecrease = total - collection.length;
setMenuBreadcrumbs(function (oldVal) {
var newValue = oldVal - totalToDecrease;
return newValue > 0 ? newValue : 0;
});
var end = total - totalToDecrease;
widthCollectionRef.current = end > 0 ? widthCollectionRef.current.slice(0, end) : [];
}
}, [parentWidth, childWidth]);
return /*#__PURE__*/_react.default.createElement(_Styles.StyledNavElement, _extends({
className: className,
ref: handleRef
}, rest), /*#__PURE__*/_react.default.createElement(_Styles.StyledListWrapper, {
ref: listWrapperRef
}, /*#__PURE__*/_react.default.createElement(_Styles.StyledOList, {
ref: olRef
}, menuCollection.length > 0 ? /*#__PURE__*/_react.default.createElement(_Styles.StyledBreadcrumbListItem, {
"data-role": "menu"
}, /*#__PURE__*/_react.default.createElement(_Menu.Menu, {
target: /*#__PURE__*/_react.default.createElement(_IconButton.IconButton, {
variant: "text gray"
}, /*#__PURE__*/_react.default.createElement(_Icons.TreeDotsIcon, null))
}, /*#__PURE__*/_react.default.createElement(_Styles.StyledMenuList, null, menuCollection.map(function (child, index) {
return /*#__PURE__*/_react.default.createElement(_List.ListItem, {
key: index
}, child);
})))) : null, listCollection.map(function (child, index) {
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
key: index
}, index > 0 || menuCollection.length > 0 ? /*#__PURE__*/_react.default.createElement(_Styles.StyledBreadcrumbListItem, {
"data-role": "separator"
}, separator) : null, /*#__PURE__*/_react.default.createElement(_Styles.StyledBreadcrumbListItem, {
"data-role": "item"
}, child));
}))));
});
Breadcrumbs.displayName = "Breadcrumbs";
//# sourceMappingURL=Breadcrumbs.js.map