UNPKG

@carbon/ibm-products

Version:
47 lines (45 loc) 2.5 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { pkg } from "../../../settings.js"; import { px } from "@carbon/layout"; //#region src/components/DataSpreadsheet/utils/moveColumnIndicatorLine.js /** * Copyright IBM Corp. 2022, 2022 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const moveColumnIndicatorLine = ({ blockClass = `${pkg.prefix}--data-spreadsheet`, clonedSelectionElement, ref, spreadsheetCoords, leftScrollAmount }) => { const closestCell = event.target.closest(`.${blockClass}--interactive-cell-element`); const newColumnIndex = closestCell?.getAttribute("data-column-index"); const originalColumnIndex = clonedSelectionElement?.getAttribute("data-column-index-original"); const closestCellCoords = closestCell.getBoundingClientRect(); const indicatorLineElement = ref.current.querySelector(`.${blockClass}__reorder-indicator-line`); const matcherId = clonedSelectionElement?.getAttribute("data-matcher-id"); const selectionAreaOrigin = ref.current.querySelector(`[data-matcher-id="${matcherId}"]`); const listContainer = ref.current.querySelector(`.${blockClass}__list--container`); const scrollSpeed = 10; const leftEdgeThreshold = 120; const rightEdgeThreshold = 100; const { clientX } = event; const { left, right } = listContainer.getBoundingClientRect(); if (clientX < leftEdgeThreshold) window.scrollBy(-scrollSpeed, 0); if (clientX > window.innerWidth - rightEdgeThreshold) window.scrollBy(scrollSpeed, 0); if (clientX > left && clientX < left + leftEdgeThreshold) listContainer.scrollBy(-scrollSpeed, 0); if (clientX < right && clientX > right - rightEdgeThreshold) listContainer.scrollBy(scrollSpeed, 0); if (Number(newColumnIndex) > Number(originalColumnIndex)) { const leftPosition = closestCellCoords.left - spreadsheetCoords.left + closestCell.offsetWidth - 2 + leftScrollAmount; indicatorLineElement.style.left = px(leftPosition); } if (Number(newColumnIndex) < Number(originalColumnIndex)) { const leftPosition = closestCellCoords.left - spreadsheetCoords.left + leftScrollAmount; indicatorLineElement.style.left = px(leftPosition); } if (Number(newColumnIndex) === Number(originalColumnIndex)) indicatorLineElement.style.left = selectionAreaOrigin.style.left; }; //#endregion export { moveColumnIndicatorLine };