@rc-component/tabs
Version:
tabs ui component for react
55 lines (54 loc) • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useVisibleRange;
var _react = require("react");
const DEFAULT_SIZE = {
width: 0,
height: 0,
left: 0,
top: 0,
right: 0
};
function useVisibleRange(tabOffsets, visibleTabContentValue, transform, tabContentSizeValue, addNodeSizeValue, operationNodeSizeValue, {
tabs,
tabPosition,
rtl
}) {
let charUnit;
let position;
let transformSize;
if (['top', 'bottom'].includes(tabPosition)) {
charUnit = 'width';
position = rtl ? 'right' : 'left';
transformSize = Math.abs(transform);
} else {
charUnit = 'height';
position = 'top';
transformSize = -transform;
}
return (0, _react.useMemo)(() => {
if (!tabs.length) {
return [0, 0];
}
const len = tabs.length;
let endIndex = len;
for (let i = 0; i < len; i += 1) {
const offset = tabOffsets.get(tabs[i].key) || DEFAULT_SIZE;
if (Math.floor(offset[position] + offset[charUnit]) > Math.floor(transformSize + visibleTabContentValue)) {
endIndex = i - 1;
break;
}
}
let startIndex = 0;
for (let i = len - 1; i >= 0; i -= 1) {
const offset = tabOffsets.get(tabs[i].key) || DEFAULT_SIZE;
if (offset[position] < transformSize) {
startIndex = i + 1;
break;
}
}
return startIndex > endIndex ? [0, -1] : [startIndex, endIndex];
}, [tabOffsets, visibleTabContentValue, tabContentSizeValue, addNodeSizeValue, operationNodeSizeValue, transformSize, tabPosition, tabs.map(tab => tab.key).join('_'), rtl]);
}