@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
61 lines • 3.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTableStickyRightColumns = void 0;
const react_1 = __importDefault(require("react"));
const resizeObserver_1 = require("../../../utils/resizeObserver/resizeObserver");
const hasScroll_1 = require("../../../utils/scroll/hasScroll");
const useTableStickyRightColumns = ({ ref, disabled = false, }) => {
react_1.default.useEffect(() => {
const scrollableContainer = ref.current?.querySelector('[data-table-scrollable-container]');
const rightBoxShadowContainer = ref.current?.querySelector('[data-table-right-shadow]');
let resizeObserver;
if (scrollableContainer instanceof HTMLElement && !disabled) {
const handleElementResize = (element) => {
const _hasHorizontalScroll = (0, hasScroll_1.hasHorizontalScroll)(element);
// For each row, set the right position of its sticky cells
const rows = element.querySelectorAll('[data-table-row]');
// used to calc rightBoxShadowContainer position
let maxStickyRightWidth = 0;
rows.forEach(row => {
// Retrieve all the cell with sticky attribute
const rightStickyCells = Array.from(
// deprecated - delete [data-sticky="true"] after boolean is deleted from the sticky type
row.querySelectorAll('[data-sticky="true"], [data-sticky="right"]')).reverse();
let right = 0;
rightStickyCells.forEach(stickyCell => {
if (stickyCell instanceof HTMLElement) {
stickyCell.style.right = _hasHorizontalScroll ? `${right}px` : 'auto';
right += stickyCell.offsetWidth;
}
});
maxStickyRightWidth = Math.max(maxStickyRightWidth, right);
});
// Once the sticky position have been added, set the position of the rightBoxShadowContainer
// This position should be the same as the max width of the sticky cells
if (rightBoxShadowContainer instanceof HTMLElement) {
// If vertical scroll, add scroll bar size to maxStickyRightWidth
const _hasVerticalScroll = (0, hasScroll_1.hasVerticalScroll)(element);
if (_hasVerticalScroll) {
const scrollBarSize = element.offsetWidth - element.clientWidth;
maxStickyRightWidth += scrollBarSize;
}
rightBoxShadowContainer.style.right = `${maxStickyRightWidth}px`;
}
};
handleElementResize(scrollableContainer);
resizeObserver = new resizeObserver_1.ResizeObserver(() => {
handleElementResize(scrollableContainer);
});
resizeObserver.observe(scrollableContainer);
}
return () => {
resizeObserver?.disconnect();
};
}, [disabled]);
return {};
};
exports.useTableStickyRightColumns = useTableStickyRightColumns;
//# sourceMappingURL=useTableStickyRightColumns.js.map