UNPKG

@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

55 lines 2.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useDataTableStickyLeftColumns = void 0; const react_1 = __importDefault(require("react")); const resizeObserver_1 = require("../../../utils/resizeObserver/resizeObserver"); const hasScroll_1 = require("../../../utils/scroll/hasScroll"); const useDataTableStickyLeftColumns = ({ ref, }) => { react_1.default.useEffect(() => { const scrollableContainer = ref.current?.querySelector('[data-datatable-scrollable-container]'); const leftBoxShadowContainer = ref.current?.querySelector('[data-datatable-left-shadow]'); let resizeObserver; if (scrollableContainer instanceof HTMLElement) { 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 => { if (row instanceof HTMLElement) { // Retrieve all the cell with sticky attribute const stickyCells = Array.from(row.querySelectorAll('[data-sticky="left"]')); let left = 0; stickyCells.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(); }; }, []); return {}; }; exports.useDataTableStickyLeftColumns = useDataTableStickyLeftColumns; //# sourceMappingURL=useDataTableStickyLeftColumns.js.map