@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
44 lines (43 loc) • 1.33 kB
JavaScript
import * as React from 'react';
import { createUuid } from '../utils/uuid';
const setZIndexMap = {};
const zIndexMap = [];
const BASE_Z_INDEX = 3000;
const Z_INDEX_GAP = 5;
let GLOBAL_COUNTER = 0;
const getNewOffset = () => {
const counter = GLOBAL_COUNTER++;
return counter * Z_INDEX_GAP;
};
const getNewZIndex = () => {
return BASE_Z_INDEX + getNewOffset();
};
export const useStacking = () => {
const modalZIndexId = React.useMemo(() => createUuid(), []);
const [zIndex, setZIndex] = React.useState(getNewZIndex);
React.useEffect(() => {
setZIndexMap[modalZIndexId] = setZIndex;
return () => {
delete setZIndexMap[modalZIndexId];
const indexInMap = zIndexMap.indexOf(modalZIndexId);
zIndexMap.splice(indexInMap, 1);
};
}, [modalZIndexId]);
const bringInFront = React.useCallback(() => {
if (zIndexMap.includes(modalZIndexId)) {
const indexInMap = zIndexMap.indexOf(modalZIndexId);
zIndexMap.splice(indexInMap, 1);
zIndexMap.push(modalZIndexId);
}
else {
zIndexMap.push(modalZIndexId);
}
zIndexMap.forEach((id) => {
setZIndexMap?.[id]?.(getNewZIndex());
});
}, []);
return {
zIndex,
bringInFront,
};
};