@carbon/ibm-products
Version:
Carbon for IBM Products
48 lines (43 loc) • 1.65 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
;
var React = require('react');
var settings = require('../../../settings.js');
var removeCellSelections = require('../utils/removeCellSelections.js');
// Click outside useEffect for spreadsheet
const useSpreadsheetOutsideClick = _ref => {
let {
isBlurSpreadsheet,
spreadsheetRef,
blockClass = `${settings.pkg.prefix}--data-spreadsheet`,
setActiveCellCoordinates,
setSelectionAreas,
removeActiveCell,
setContainerHasFocus,
removeCellEditor
} = _ref;
React.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.removeCellSelections({
spreadsheetRef
});
setContainerHasFocus(false);
};
document.addEventListener('click', handleOutsideClick);
return () => {
document.removeEventListener('click', handleOutsideClick);
};
}, [isBlurSpreadsheet, spreadsheetRef, removeActiveCell, blockClass, setActiveCellCoordinates, setContainerHasFocus, setSelectionAreas, removeCellEditor]);
};
exports.useSpreadsheetOutsideClick = useSpreadsheetOutsideClick;