@atlaskit/navigation-system
Version:
The latest navigation system for Atlassian apps.
111 lines (110 loc) • 5.72 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useOccupiedLayoutWidth = useOccupiedLayoutWidth;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _bindEventListener = require("bind-event-listener");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
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; }
/**
* Measures the existing Root slots that are not managed by the chat-layout allocator.
* Only count separate inline columns: Aside stacks below Main on mobile, and the
* legacy Panel spans other areas while overlaying. Read the grid rather than duplicating
* its responsive breakpoints. Border boxes include slot borders and live splitter widths.
*/
function useOccupiedLayoutWidth(layoutRef) {
var _useState = (0, _react.useState)(0),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
width = _useState2[0],
setWidth = _useState2[1];
(0, _react.useEffect)(function () {
var root = layoutRef === null || layoutRef === void 0 ? void 0 : layoutRef.current;
if (!root) {
return;
}
var occupiedSlots = [];
var measure = function measure() {
var _getComputedStyle$gri, _rows$map$find;
if (occupiedSlots.length === 0) {
setWidth(0);
return;
}
var rows = (_getComputedStyle$gri = getComputedStyle(root).gridTemplateAreas.match(/"[^"]+"/g)) !== null && _getComputedStyle$gri !== void 0 ? _getComputedStyle$gri : [];
var mainRow = (_rows$map$find = rows.map(function (row) {
return row.replace(/"/g, '').split(/\s+/);
}).find(function (row) {
return row.includes('main');
})) !== null && _rows$map$find !== void 0 ? _rows$map$find : [];
var occupiedWidth = 0;
var _iterator = _createForOfIteratorHelper(occupiedSlots),
_step;
try {
var _loop = function _loop() {
var child = _step.value;
var style = getComputedStyle(child);
var area = style.gridArea.split('/').map(function (part) {
return part.trim();
});
var slot = area[0];
if (['ribbon', 'aside', 'panel'].includes(slot) && area.every(function (part) {
return part === slot;
}) && mainRow.includes(slot) && style.display !== 'none' && style.position !== 'absolute' && style.position !== 'fixed') {
occupiedWidth += child.getBoundingClientRect().width;
}
};
for (_iterator.s(); !(_step = _iterator.n()).done;) {
_loop();
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
setWidth(occupiedWidth);
};
var resizeObserver = new ResizeObserver(measure);
var observeSlots = function observeSlots() {
resizeObserver.disconnect();
resizeObserver.observe(root);
// Managed regions resize on every drag frame but cannot consume extra tracks.
// Keep them out of both the observer and the measurement loop. Slot identity,
// unlike gridArea, remains stable when a region switches to overlay mode.
// TopNav and Banner also span columns rather than consuming independent tracks.
occupiedSlots = Array.from(root.children).filter(function (child) {
return child instanceof HTMLElement && child.hasAttribute('data-layout-slot') && !child.matches('nav, header, [role="main"], [data-layout-chat-panel]') && getComputedStyle(child).gridArea.split('/')[0].trim() !== 'banner';
});
var _iterator2 = _createForOfIteratorHelper(occupiedSlots),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var child = _step2.value;
resizeObserver.observe(child);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
measure();
};
var mutationObserver = new MutationObserver(observeSlots);
mutationObserver.observe(root, {
childList: true
});
observeSlots();
var unbind = (0, _bindEventListener.bind)(window, {
type: 'resize',
listener: measure
});
return function () {
unbind();
resizeObserver.disconnect();
mutationObserver.disconnect();
};
}, [layoutRef]);
return width;
}