Skip to content

Commit

Permalink
fix: Fixed image preview in a nested modal where z-index Settings did…
Browse files Browse the repository at this point in the history
… not meet expectations (ant-design#45979)
  • Loading branch information
kiner-tang authored Nov 21, 2023
1 parent e778066 commit ef9971c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
14 changes: 13 additions & 1 deletion components/_util/__tests__/useZIndex.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { PropsWithChildren } from 'react';
import React, { useEffect } from 'react';
import { render } from '@testing-library/react';
import type { MenuProps } from 'antd';
import type { ImageProps, MenuProps } from 'antd';
import {
AutoComplete,
Cascader,
ColorPicker,
DatePicker,
Drawer,
Dropdown,
Image,
Menu,
Modal,
Popconfirm,
Expand Down Expand Up @@ -178,6 +179,15 @@ const consumerComponent: Record<ZIndexConsumer, React.FC<{ rootClassName: string
</>
),
Menu: (props) => <Menu {...props} items={items} defaultOpenKeys={['SubMenu']} />,
ImagePreview: ({ rootClassName }: ImageProps) => (
<Image
src="xxx"
preview={{
visible: true,
rootClassName: `${rootClassName} comp-item comp-ImagePreview`,
}}
/>
),
};

function getConsumerSelector(baseSelector: string, consumer: ZIndexConsumer): string {
Expand All @@ -196,6 +206,8 @@ function getConsumerSelector(baseSelector: string, consumer: ZIndexConsumer): st
.join(',');
} else if (['Menu'].includes(consumer)) {
selector = `${baseSelector}.ant-menu-submenu-placement-rightTop`;
} else if (consumer === 'ImagePreview') {
selector = `${baseSelector}.comp-ImagePreview`;
}
return selector;
}
Expand Down
3 changes: 2 additions & 1 deletion components/_util/hooks/useZIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zIndexContext from '../zindexContext';

export type ZIndexContainer = 'Modal' | 'Drawer' | 'Popover' | 'Popconfirm' | 'Tooltip' | 'Tour';

export type ZIndexConsumer = 'SelectLike' | 'Dropdown' | 'DatePicker' | 'Menu';
export type ZIndexConsumer = 'SelectLike' | 'Dropdown' | 'DatePicker' | 'Menu' | 'ImagePreview';

// Z-Index control range
// Container: 1000 + offset 100 (max base + 10 * offset = 2000)
Expand All @@ -30,6 +30,7 @@ export const consumerBaseZIndexOffset: Record<ZIndexConsumer, number> = {
Dropdown: 50,
DatePicker: 50,
Menu: 50,
ImagePreview: 1,
};

function isContainerType(type: ZIndexContainer | ZIndexConsumer): type is ZIndexContainer {
Expand Down
7 changes: 7 additions & 0 deletions components/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames';
import RcImage from 'rc-image';
import type { ImageProps } from 'rc-image';

import { useZIndex } from '../_util/hooks/useZIndex';
import { getTransitionName } from '../_util/motion';
import { ConfigContext } from '../config-provider';
import defaultLocale from '../locale/en_US';
Expand Down Expand Up @@ -42,6 +43,11 @@ const Image: CompositionImage<ImageProps> = (props) => {

const mergedClassName = classNames(className, hashId, image?.className);

const [zIndex] = useZIndex(
'ImagePreview',
typeof preview === 'object' ? preview.zIndex : undefined,
);

const mergedPreview = React.useMemo(() => {
if (preview === false) {
return preview;
Expand All @@ -60,6 +66,7 @@ const Image: CompositionImage<ImageProps> = (props) => {
getContainer: getContainer || getContextPopupContainer,
transitionName: getTransitionName(rootPrefixCls, 'zoom', _preview.transitionName),
maskTransitionName: getTransitionName(rootPrefixCls, 'fade', _preview.maskTransitionName),
zIndex,
};
}, [preview, imageLocale]);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"rc-drawer": "~6.5.2",
"rc-dropdown": "~4.1.0",
"rc-field-form": "~1.40.0",
"rc-image": "~7.3.2",
"rc-image": "~7.4.0",
"rc-input": "~1.3.6",
"rc-input-number": "~8.4.0",
"rc-mentions": "~2.9.1",
Expand Down

0 comments on commit ef9971c

Please sign in to comment.