@fluido/react-components
Version:
Fluido webapp components
71 lines (70 loc) • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.animatedScrollTo = exports.getNearestChildIndex = exports.getChildrenVisible = exports.getChildrenRange = exports.parseHtmlCollectionToArray = void 0;
const utils_1 = require("../utils");
const parseHtmlCollectionToArray = (el) => {
const result = [];
for (let i = 0; i < el.length; i++) {
result.push(el[i]);
}
return result;
};
exports.parseHtmlCollectionToArray = parseHtmlCollectionToArray;
const getChildrenRange = (node) => {
const children = exports.parseHtmlCollectionToArray(node.children);
return children.map((el, i, l) => {
const isLast = i === l.length - 1;
const fix = isLast ? parseInt(el.style.paddingInlineEnd) || 32 : 0;
const start = el.offsetLeft - node.scrollLeft;
return [start, start + el.offsetWidth - fix];
});
};
exports.getChildrenRange = getChildrenRange;
const getChildrenVisible = (node, partial = false) => {
const children = exports.getChildrenRange(node);
return children.map((range) => {
if (!partial)
return range[0] >= 0 && range[1] <= node.offsetWidth;
return range[1] >= 0 && range[0] <= node.offsetWidth;
});
};
exports.getChildrenVisible = getChildrenVisible;
const getNearestChildIndex = (node, align = 'none') => {
const width = node.offsetWidth;
const position = align !== 'center' ? 0 : width / 2;
const children = exports.getChildrenRange(node).map((v) => {
return align !== 'center' ? v[0] : v[0] + (v[1] - v[0]) / 2;
});
const closest = children.length
? children.reduce((prev, pos) => {
return Math.abs(pos - position) < Math.abs(Math.abs(prev - position))
? pos
: prev;
}, Infinity)
: null;
return closest ? children.indexOf(closest) : -1;
};
exports.getNearestChildIndex = getNearestChildIndex;
const animatedScrollTo = (node, val, align = 'none') => {
if (utils_1.testIsSSR())
return false;
let distance = -1;
const testPositions = () => distance < 0;
const start = +window.getComputedStyle(node).paddingInlineStart || 16;
const scrollPos = node.scrollLeft;
const width = node.offsetWidth;
if (val instanceof HTMLElement && testPositions()) {
if (align !== 'center')
distance = val.offsetLeft - scrollPos;
else
distance = val.offsetLeft - scrollPos + val.offsetWidth / 2 - width / 2;
}
else {
}
if (!isNaN(parseFloat(val)) && testPositions()) {
distance = val || 0;
}
const to = scrollPos + distance - start;
node.scrollTo(to, 0);
};
exports.animatedScrollTo = animatedScrollTo;