diff --git a/components/_util/__tests__/useZIndex.test.tsx b/components/_util/__tests__/useZIndex.test.tsx index eb5062efb373..52df2a225245 100644 --- a/components/_util/__tests__/useZIndex.test.tsx +++ b/components/_util/__tests__/useZIndex.test.tsx @@ -234,7 +234,8 @@ describe('Test useZIndex hooks', () => { ); render(); expect(fn).toHaveBeenLastCalledWith( - (1000 + containerBaseZIndexOffset[containerKey as ZIndexContainer]) * 3 + + 1000 + + containerBaseZIndexOffset[containerKey as ZIndexContainer] * 3 + consumerBaseZIndexOffset[key as ZIndexConsumer], ); }); @@ -272,7 +273,7 @@ describe('Test useZIndex hooks', () => { comps.forEach((comp) => { const isColorPicker = (comp as HTMLDivElement).className.includes('comp-ColorPicker'); const consumerOffset = isColorPicker - ? 1000 + containerBaseZIndexOffset.Popover + ? containerBaseZIndexOffset.Popover : consumerBaseZIndexOffset[key as ZIndexConsumer]; expect((comp as HTMLDivElement).style.zIndex).toBe( String( @@ -287,11 +288,12 @@ describe('Test useZIndex hooks', () => { comps.forEach((comp) => { const isColorPicker = (comp as HTMLDivElement).className.includes('comp-ColorPicker'); const consumerOffset = isColorPicker - ? 1000 + containerBaseZIndexOffset.Popover + ? containerBaseZIndexOffset.Popover : consumerBaseZIndexOffset[key as ZIndexConsumer]; expect((comp as HTMLDivElement).style.zIndex).toBe( String( - (1000 + containerBaseZIndexOffset[containerKey as ZIndexContainer]) * 2 + + 1000 + + containerBaseZIndexOffset[containerKey as ZIndexContainer] * 2 + consumerOffset, ), ); @@ -317,7 +319,8 @@ describe('Test useZIndex hooks', () => { expect((document.querySelector(selector3) as HTMLDivElement).style.zIndex).toBe( String( - (1000 + containerBaseZIndexOffset[containerKey as ZIndexContainer]) * 2 + + 1000 + + containerBaseZIndexOffset[containerKey as ZIndexContainer] * 2 + consumerBaseZIndexOffset[key as ZIndexConsumer], ), ); @@ -328,4 +331,28 @@ describe('Test useZIndex hooks', () => { }); }); }); + + it('Modal static func should always use max zIndex', async () => { + jest.useFakeTimers(); + + const instance = Modal.confirm({ + title: 'bamboo', + content: 'little', + }); + + await waitFakeTimer(); + + expect(document.querySelector('.ant-modal-wrap')).toHaveStyle({ + zIndex: '2000', + }); + + instance.destroy(); + + await waitFakeTimer(); + + // Clean up for static method + document.body.innerHTML = ''; + + jest.useRealTimers(); + }); }); diff --git a/components/_util/hooks/useZIndex.tsx b/components/_util/hooks/useZIndex.tsx index be90054cf4fc..33a4726e9193 100644 --- a/components/_util/hooks/useZIndex.tsx +++ b/components/_util/hooks/useZIndex.tsx @@ -7,13 +7,23 @@ export type ZIndexContainer = 'Modal' | 'Drawer' | 'Popover' | 'Popconfirm' | 'T export type ZIndexConsumer = 'SelectLike' | 'Dropdown' | 'DatePicker' | 'Menu'; +// 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; + export const containerBaseZIndexOffset: Record = { - Modal: 0, - Drawer: 0, - Popover: 70, - Popconfirm: 70, - Tooltip: 70, - Tour: 70, + Modal: CONTAINER_OFFSET, + Drawer: CONTAINER_OFFSET, + Popover: CONTAINER_OFFSET, + Popconfirm: CONTAINER_OFFSET, + Tooltip: CONTAINER_OFFSET, + Tour: CONTAINER_OFFSET, }; export const consumerBaseZIndexOffset: Record = { SelectLike: 50, @@ -35,7 +45,13 @@ export function useZIndex( const isContainer = isContainerType(componentType); let zIndex = parentZIndex ?? 0; if (isContainer) { - zIndex += token.zIndexPopupBase + containerBaseZIndexOffset[componentType]; + zIndex += + // Use preset token zIndex by default but not stack when has parent container + (parentZIndex ? 0 : token.zIndexPopupBase) + + // Container offset + containerBaseZIndexOffset[componentType]; + + zIndex = Math.min(zIndex, token.zIndexPopupBase + CONTAINER_MAX_OFFSET); } else { zIndex += consumerBaseZIndexOffset[componentType]; } diff --git a/components/avatar/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/avatar/__tests__/__snapshots__/demo-extend.test.ts.snap index 359e1128f34c..69105054c41c 100644 --- a/components/avatar/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/avatar/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -439,7 +439,7 @@ Array [
-
- -