@adaptabletools/adaptable
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
41 lines (40 loc) • 1.35 kB
JavaScript
import * as React from 'react';
import { createUuid } from '../utils/uuid';
const setZIndexMap = {};
const zIndexMap = [];
const BASE_Z_INDEX = 1000;
/**
* Used to facilitate the stacking between multiple opened windows.
* When calling 'bringToFront' brings caller to top.
* This is done by changing the zIndex of all registered window-modals.
*/
export const useStacking = () => {
const modalZIndexId = React.useMemo(() => createUuid(), []);
const [zIndex, setZIndex] = React.useState(BASE_Z_INDEX);
React.useEffect(() => {
setZIndexMap[modalZIndexId] = setZIndex;
return () => {
// clean-up
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, index) => {
setZIndexMap?.[id]?.(BASE_Z_INDEX + index * 10);
});
}, []);
return {
zIndex,
bringInFront,
};
};