@activecollab/components
Version:
ActiveCollab Components
138 lines (137 loc) • 9.93 kB
JavaScript
"use strict";
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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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