@kubit-ui-web/react-components
Version:
Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience
53 lines • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTableStickyLeftColumns = void 0;
const react_1 = __importDefault(require("react"));
const resizeObserver_1 = require("../../../utils/resizeObserver/resizeObserver");
const hasScroll_1 = require("../../../utils/scroll/hasScroll");
const useTableStickyLeftColumns = ({ ref, disabled = false, }) => {
react_1.default.useEffect(() => {
const scrollableContainer = ref.current?.querySelector('[data-table-scrollable-container]');
const leftBoxShadowContainer = ref.current?.querySelector('[data-table-left-shadow]');
let resizeObserver;
if (scrollableContainer instanceof HTMLElement && !disabled) {
const handleElementResize = (element) => {
const _hasHorizontalScroll = (0, hasScroll_1.hasHorizontalScroll)(element);
// For each row, set the left position of its sticky cells
const rows = element.querySelectorAll('[data-table-row]');
// used to calc leftBoxShadowContainer position
let maxStickyLeftWidth = 0;
rows.forEach(row => {
// Retrieve all the cell with sticky attribute
const leftStickyCells = Array.from(row.querySelectorAll('[data-sticky="left"]'));
let left = 0;
leftStickyCells.forEach(stickyCell => {
if (stickyCell instanceof HTMLElement) {
stickyCell.style.left = _hasHorizontalScroll ? `${left}px` : 'auto';
left += stickyCell.offsetWidth;
}
});
maxStickyLeftWidth = Math.max(maxStickyLeftWidth, left);
});
// Once the sticky position have been added, set the position of the leftBoxShadowContainer
// This position should be the same as the max width of the sticky cells
if (leftBoxShadowContainer instanceof HTMLElement) {
leftBoxShadowContainer.style.left = `${maxStickyLeftWidth}px`;
}
};
handleElementResize(scrollableContainer);
resizeObserver = new resizeObserver_1.ResizeObserver(() => {
handleElementResize(scrollableContainer);
});
resizeObserver.observe(scrollableContainer);
}
return () => {
resizeObserver?.disconnect();
};
}, [disabled]);
return {};
};
exports.useTableStickyLeftColumns = useTableStickyLeftColumns;
//# sourceMappingURL=useTableStickyLeftColumns.js.map