@carbon/ibm-products
Version:
Carbon for IBM Products
46 lines (44 loc) • 1.69 kB
JavaScript
/**
* 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 { removeCellSelections } from "../utils/removeCellSelections.js";
import { useEffect } from "react";
//#region src/components/DataSpreadsheet/hooks/useSpreadsheetOutsideClick.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 useSpreadsheetOutsideClick = ({ isBlurSpreadsheet, spreadsheetRef, blockClass = `${pkg.prefix}--data-spreadsheet`, setActiveCellCoordinates, setSelectionAreas, removeActiveCell, setContainerHasFocus, removeCellEditor }) => {
useEffect(() => {
const handleOutsideClick = (event) => {
if (!spreadsheetRef.current || spreadsheetRef.current.contains(event.target) || event.target.classList.contains(`${blockClass}__active-cell--highlight`) || event.target.classList.contains(`${blockClass}--interactive-cell-element`)) return;
isBlurSpreadsheet.current = true;
setActiveCellCoordinates(null);
setSelectionAreas([]);
removeActiveCell();
removeCellSelections({ spreadsheetRef });
setContainerHasFocus(false);
};
document.addEventListener("click", handleOutsideClick);
return () => {
document.removeEventListener("click", handleOutsideClick);
};
}, [
isBlurSpreadsheet,
spreadsheetRef,
removeActiveCell,
blockClass,
setActiveCellCoordinates,
setContainerHasFocus,
setSelectionAreas,
removeCellEditor
]);
};
//#endregion
export { useSpreadsheetOutsideClick };