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';
|
2024-07-04 15:44:32 +08:00
|
|
|
import { devUseWarning } from '../warning';
|
2023-10-19 15:03:20 +08:00
|
|
|
import zIndexContext from '../zindexContext';
|
|
|
|
|
|
|
|
export type ZIndexContainer = 'Modal' | 'Drawer' | 'Popover' | 'Popconfirm' | 'Tooltip' | 'Tour';
|
|
|
|
|
2023-11-21 15:53:39 +08:00
|
|
|
export type ZIndexConsumer = 'SelectLike' | 'Dropdown' | 'DatePicker' | 'Menu' | 'ImagePreview';
|
2023-10-19 15:03:20 +08:00
|
|
|
|
2023-11-17 10:54:19 +08:00
|
|
|
// Z-Index control range
|
|
|
|
// Container: 1000 + offset 100 (max base + 10 * offset = 2000)
|
|
|
|
// Popover: offset 50
|
|
|
|
// Notification: Container Max zIndex + componentOffset
|
|
|
|
|
|
|
|
const CONTAINER_OFFSET = 100;
|
|
|
|
const CONTAINER_OFFSET_MAX_COUNT = 10;
|
|
|
|
|
|
|
|
export const CONTAINER_MAX_OFFSET = CONTAINER_OFFSET * CONTAINER_OFFSET_MAX_COUNT;
|
|
|
|
|
2023-10-19 15:03:20 +08:00
|
|
|
export const containerBaseZIndexOffset: Record<ZIndexContainer, number> = {
|
2023-11-17 10:54:19 +08:00
|
|
|
Modal: CONTAINER_OFFSET,
|
|
|
|
Drawer: CONTAINER_OFFSET,
|
|
|
|
Popover: CONTAINER_OFFSET,
|
|
|
|
Popconfirm: CONTAINER_OFFSET,
|
|
|
|
Tooltip: CONTAINER_OFFSET,
|
|
|
|
Tour: CONTAINER_OFFSET,
|
2023-10-19 15:03:20 +08:00
|
|
|
};
|
|
|
|
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,
|
2023-11-21 15:53:39 +08:00
|
|
|
ImagePreview: 1,
|
2023-10-19 15:03:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function isContainerType(type: ZIndexContainer | ZIndexConsumer): type is ZIndexContainer {
|
|
|
|
return type in containerBaseZIndexOffset;
|
|
|
|
}
|
|
|
|
|
2024-07-04 15:44:32 +08:00
|
|
|
type ReturnResult = [zIndex: number | undefined, contextZIndex: number];
|
|
|
|
|
2023-10-19 15:03:20 +08:00
|
|
|
export function useZIndex(
|
|
|
|
componentType: ZIndexContainer | ZIndexConsumer,
|
|
|
|
customZIndex?: number,
|
2024-07-04 15:44:32 +08:00
|
|
|
): ReturnResult {
|
2023-10-19 15:03:20 +08:00
|
|
|
const [, token] = useToken();
|
|
|
|
const parentZIndex = React.useContext(zIndexContext);
|
|
|
|
const isContainer = isContainerType(componentType);
|
2023-11-22 18:02:32 +08:00
|
|
|
|
2024-07-04 15:44:32 +08:00
|
|
|
let result: ReturnResult;
|
|
|
|
|
2023-11-22 18:02:32 +08:00
|
|
|
if (customZIndex !== undefined) {
|
2024-07-04 15:44:32 +08:00
|
|
|
result = [customZIndex, customZIndex];
|
|
|
|
} else {
|
|
|
|
let zIndex = parentZIndex ?? 0;
|
|
|
|
|
|
|
|
if (isContainer) {
|
|
|
|
zIndex +=
|
|
|
|
// Use preset token zIndex by default but not stack when has parent container
|
|
|
|
(parentZIndex ? 0 : token.zIndexPopupBase) +
|
|
|
|
// Container offset
|
|
|
|
containerBaseZIndexOffset[componentType];
|
|
|
|
} else {
|
|
|
|
zIndex += consumerBaseZIndexOffset[componentType];
|
|
|
|
}
|
|
|
|
result = [parentZIndex === undefined ? customZIndex : zIndex, zIndex];
|
2023-11-22 18:02:32 +08:00
|
|
|
}
|
|
|
|
|
2024-07-04 15:44:32 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const warning = devUseWarning(componentType);
|
2023-11-22 18:02:32 +08:00
|
|
|
|
2024-07-04 15:44:32 +08:00
|
|
|
const maxZIndex = token.zIndexPopupBase + CONTAINER_MAX_OFFSET;
|
|
|
|
const currentZIndex = result[0] || 0;
|
2023-11-17 10:54:19 +08:00
|
|
|
|
2024-07-04 15:44:32 +08:00
|
|
|
warning(
|
|
|
|
customZIndex !== undefined || currentZIndex <= maxZIndex,
|
|
|
|
'usage',
|
|
|
|
'`zIndex` is over design token `zIndexPopupBase` too much. It may cause unexpected override.',
|
|
|
|
);
|
2023-10-19 15:03:20 +08:00
|
|
|
}
|
2024-07-04 15:44:32 +08:00
|
|
|
|
|
|
|
return result;
|
2023-10-19 15:03:20 +08:00
|
|
|
}
|