UNPKG

@elastic/eui

Version:

Elastic UI Component Library

146 lines (140 loc) 5.73 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; import { useEuiTableColumnDataStore } from '../store/provider'; import { EuiTableHeader } from '../table_header'; import { EuiTableWithinStickyHeaderProvider } from './context'; import { useEuiMemoizedStyles } from '../../../services'; import { euiTableStyles } from '../table.styles'; import { euiTableStickyHeaderStyles } from './sticky_header.styles'; import { euiContainerCSS } from '../../../global_styling'; import { EUI_TABLE_CSS_CONTAINER_NAME } from '../const'; /** * @internal */ import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiTableStickyHeader = function EuiTableStickyHeader(_ref) { var tableRef = _ref.tableRef, tableWrapperRef = _ref.tableWrapperRef, compressed = _ref.compressed, scrollableInline = _ref.scrollableInline, isResponsive = _ref.isResponsive; var store = useEuiTableColumnDataStore(); var columnElements = useRef(new Map()); var stickyTableWrapperRef = useRef(null); var stickyTableRef = useRef(null); var _useState = useState(function () { return Array.from(store.getColumns().entries()); }), _useState2 = _slicedToArray(_useState, 2), columns = _useState2[0], setColumns = _useState2[1]; var originalStyles = useEuiMemoizedStyles(euiTableStyles); var styles = useEuiMemoizedStyles(euiTableStickyHeaderStyles); /** * Get a callback ref to handle the column element ref */ var getColumnRef = useCallback(function (id) { return function (element) { if (element) { columnElements.current.set(id, element); } else { columnElements.current.delete(id); } }; }, []); useEffect(function () { var unsubscribe = store.subscribe(function (columns) { setColumns(Array.from(columns.entries())); }); var unsubscribeColumnWidths = store.subscribeToColumnWidths(function (columns) { columns.forEach(function (width, name) { var element = columnElements.current.get(name); if (element) { element.style.width = "".concat(width, "px"); } }); }); return function () { unsubscribe(); unsubscribeColumnWidths(); }; }, [store]); // When columns change, apply column widths after render useLayoutEffect(function () { store.getColumnWidths().forEach(function (width, name) { var element = columnElements.current.get(name); if (element) { element.style.width = "".concat(width, "px"); } }); }, [store, columns]); useEffect(function () { if (!scrollableInline || !tableWrapperRef.current || !stickyTableRef.current || !tableRef.current) { return; } var tableWrapper = tableWrapperRef.current; var handleScroll = function handleScroll() { if (stickyTableWrapperRef.current) { stickyTableWrapperRef.current.scrollLeft = tableWrapper.scrollLeft; } }; var handleResize = function handleResize(entries) { var element = entries[0].target; if (!element) { return; } if (stickyTableRef.current) { stickyTableRef.current.style.minWidth = "".concat(element.clientWidth, "px"); } }; // Initial width sync stickyTableRef.current.style.minWidth = "".concat(tableRef.current.getBoundingClientRect().width, "px"); // Use ResizeObserver to keep table width in sync var resizeObserver = new ResizeObserver(handleResize); resizeObserver.observe(tableRef.current); tableWrapper.addEventListener('scroll', handleScroll, { passive: true }); return function () { tableWrapper.removeEventListener('scroll', handleScroll); resizeObserver.disconnect(); }; }, [scrollableInline, tableRef, tableWrapperRef]); var tableStyles = [originalStyles.euiTable, scrollableInline && originalStyles.euiTableScrollableInline, (!compressed || isResponsive) && originalStyles.uncompressed, compressed && !isResponsive && originalStyles.compressed, // Forced fixed layout since all column widths come synced from the main table originalStyles.layout.fixed, originalStyles.hasBackground, styles.table]; if (isResponsive) { return null; } return ___EmotionJSX(EuiTableWithinStickyHeaderProvider, null, ___EmotionJSX("div", { css: styles.wrapper, "aria-hidden": "true" }, ___EmotionJSX("div", { css: [ // This CSS container is needed to feed `<EuiTableHeaderCell>` // with `sticky` prop set the necessary scroll state euiContainerCSS('normal', EUI_TABLE_CSS_CONTAINER_NAME, true), styles.innerWrapper, ";label:EuiTableStickyHeader;"], ref: stickyTableWrapperRef }, ___EmotionJSX("table", { css: tableStyles, ref: stickyTableRef }, ___EmotionJSX(EuiTableHeader, { css: styles.header }, columns.map(function (_ref2, index) { var _data$renderHeaderCel, _data$renderHeaderCel2; var _ref3 = _slicedToArray(_ref2, 2), name = _ref3[0], data = _ref3[1]; return (_data$renderHeaderCel = (_data$renderHeaderCel2 = data.renderHeaderCellRef).current) === null || _data$renderHeaderCel === void 0 ? void 0 : _data$renderHeaderCel.call(_data$renderHeaderCel2, { ref: getColumnRef(name), key: "".concat(name, "-").concat(index) }); })))))); };