2023-10-19 15:03:20 +08:00
|
|
|
import React from 'react';
|
2023-11-09 19:07:22 +08:00
|
|
|
|
2023-10-19 15:03:20 +08:00
|
|
|
import useToken from '../../theme/useToken';
|
|
|
|
import zIndexContext from '../zindexContext';
|
|
|
|
|
|
|
|
export type ZIndexContainer = 'Modal' | 'Drawer' | 'Popover' | 'Popconfirm' | 'Tooltip' | 'Tour';
|
|
|
|
|
2023-11-14 18:01:23 +08:00
|
|
|
export type ZIndexConsumer = 'SelectLike' | 'Dropdown' | 'DatePicker' | 'Menu';
|
2023-10-19 15:03:20 +08:00
|
|
|
|
|
|
|
export const containerBaseZIndexOffset: Record<ZIndexContainer, number> = {
|
|
|
|
Modal: 0,
|
|
|
|
Drawer: 0,
|
2023-10-23 17:16:41 +08:00
|
|
|
Popover: 70,
|
|
|
|
Popconfirm: 70,
|
2023-10-19 15:03:20 +08:00
|
|
|
Tooltip: 70,
|
|
|
|
Tour: 70,
|
|
|
|
};
|
|
|
|
export const consumerBaseZIndexOffset: Record<ZIndexConsumer, number> = {
|
2023-10-23 17:16:41 +08:00
|
|
|
SelectLike: 50,
|
2023-10-19 15:03:20 +08:00
|
|
|
Dropdown: 50,
|
|
|
|
DatePicker: 50,
|
|
|
|
Menu: 50,
|
|
|
|
};
|
|
|
|
|
|
|
|
function isContainerType(type: ZIndexContainer | ZIndexConsumer): type is ZIndexContainer {
|
|
|
|
return type in containerBaseZIndexOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useZIndex(
|
|
|
|
componentType: ZIndexContainer | ZIndexConsumer,
|
|
|
|
customZIndex?: number,
|
|
|
|
): [zIndex: number | undefined, contextZIndex: number] {
|
|
|
|
const [, token] = useToken();
|
|
|
|
const parentZIndex = React.useContext(zIndexContext);
|
|
|
|
const isContainer = isContainerType(componentType);
|
|
|
|
let zIndex = parentZIndex ?? 0;
|
|
|
|
if (isContainer) {
|
|
|
|
zIndex += token.zIndexPopupBase + containerBaseZIndexOffset[componentType];
|
|
|
|
} else {
|
|
|
|
zIndex += consumerBaseZIndexOffset[componentType];
|
|
|
|
}
|
|
|
|
return [parentZIndex === undefined ? customZIndex : zIndex, zIndex];
|
|
|
|
}
|