Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Drawer support z-index manager #45417

Merged
merged 3 commits into from Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions components/drawer/__tests__/Drawer.test.tsx
Expand Up @@ -7,6 +7,7 @@ import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render } from '../../../tests/utils';
import { resetWarned } from '../../_util/warning';
import ConfigProvider from '../../config-provider';
import { Select } from 'antd';

const DrawerTest: React.FC<DrawerProps> = ({ getContainer }) => (
<div>
Expand Down Expand Up @@ -309,5 +310,37 @@ describe('Drawer', () => {
);
expect(baseElement.querySelector('.anticon-close')).not.toBeNull();
});
it('z-index should be accumulated in nested Drawer', () => {
const options = [
{
label: 'Option 1',
value: '1',
},
{
label: 'Option 2',
value: '2',
},
];
render(
<>
<Select open options={options} popupClassName="select0" />
<Drawer open>
<Select open options={options} popupClassName="select1" />
<Drawer open>
<Select open options={options} popupClassName="select2" />
</Drawer>
</Drawer>
</>,
);
expect(
(document.querySelectorAll('.ant-drawer')[0] as HTMLDivElement)!.style.zIndex,
).toBeFalsy();
expect((document.querySelectorAll('.ant-drawer')[1] as HTMLDivElement)!.style.zIndex).toBe(
'2000',
);
expect((document.querySelector('.select0') as HTMLDivElement)!.style.zIndex).toBeFalsy();
expect((document.querySelector('.select1') as HTMLDivElement)!.style.zIndex).toBe('1050');
expect((document.querySelector('.select2') as HTMLDivElement)!.style.zIndex).toBe('2050');
});
});
});
Expand Up @@ -1049,7 +1049,7 @@ Array [
</div>
<div
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-select-dropdown-placement-bottomLeft"
style="--arrow-x: 0px; --arrow-y: 0px; left: -1000vw; top: -1000vh; box-sizing: border-box;"
style="--arrow-x: 0px; --arrow-y: 0px; left: -1000vw; top: -1000vh; box-sizing: border-box; z-index: 1050;"
>
<div>
<div
Expand Down Expand Up @@ -1226,7 +1226,7 @@ Array [
</div>
<div
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-select-dropdown-placement-bottomLeft"
style="--arrow-x: 0px; --arrow-y: 0px; left: -1000vw; top: -1000vh; box-sizing: border-box;"
style="--arrow-x: 0px; --arrow-y: 0px; left: -1000vw; top: -1000vh; box-sizing: border-box; z-index: 1050;"
>
<div>
<div
Expand Down Expand Up @@ -1408,7 +1408,7 @@ Array [
</div>
<div
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-select-dropdown-placement-bottomLeft"
style="--arrow-x: 0px; --arrow-y: 0px; left: -1000vw; top: -1000vh; box-sizing: border-box;"
style="--arrow-x: 0px; --arrow-y: 0px; left: -1000vw; top: -1000vh; box-sizing: border-box; z-index: 1050;"
>
<div>
<div
Expand Down Expand Up @@ -2867,6 +2867,7 @@ Array [
</button>
<div
class="ant-drawer ant-drawer-right ant-drawer-open ant-drawer-inline"
style="z-index: 2000;"
tabindex="-1"
>
<div
Expand Down Expand Up @@ -3536,6 +3537,7 @@ exports[`renders components/drawer/demo/scroll-debug.tsx extend context correctl
Some contents...
<div
class="ant-drawer ant-drawer-right ant-drawer-open ant-drawer-inline"
style="z-index: 2000;"
tabindex="-1"
>
<div
Expand Down
76 changes: 42 additions & 34 deletions components/drawer/index.tsx
Expand Up @@ -5,6 +5,7 @@ import RcDrawer from 'rc-drawer';
import type { Placement } from 'rc-drawer/lib/Drawer';
import type { CSSMotionProps } from 'rc-motion';

import { useZIndex } from '../_util/hooks/useZIndex';
import { getTransitionName } from '../_util/motion';
import { devUseWarning } from '../_util/warning';
import { ConfigContext } from '../config-provider';
Expand All @@ -15,6 +16,7 @@ import { usePanelRef } from '../watermark/context';
import type { DrawerClassNames, DrawerPanelProps, DrawerStyles } from './DrawerPanel';
import DrawerPanel from './DrawerPanel';
import useStyle from './style';
import zIndexContext from '../_util/zindexContext';

const SizeTypes = ['default', 'large'] as const;
type sizeType = typeof SizeTypes[number];
Expand Down Expand Up @@ -143,44 +145,50 @@ const Drawer: React.FC<DrawerProps> & {
// Select `ant-modal-content` by `panelRef`
const panelRef = usePanelRef();

// ============================ zIndex ============================
const [zIndex, contextZIndex] = useZIndex('Drawer', rest.zIndex);

// =========================== Render ===========================
return wrapSSR(
<NoCompactStyle>
<NoFormStyle status override>
<RcDrawer
prefixCls={prefixCls}
onClose={onClose}
maskMotion={maskMotion}
motion={panelMotion}
{...rest}
classNames={{
mask: classNames(rest.classNames?.mask, drawer?.classNames?.mask),
content: classNames(rest.classNames?.content, drawer?.classNames?.content),
}}
styles={{
mask: {
...rest.styles?.mask,
...drawer?.styles?.mask,
},
content: {
...rest.styles?.content,
...drawer?.styles?.content,
},
}}
open={open ?? visible}
mask={mask}
push={push}
width={mergedWidth}
height={mergedHeight}
style={{ ...drawer?.style, ...style }}
className={classNames(drawer?.className, className)}
rootClassName={drawerClassName}
getContainer={getContainer}
afterOpenChange={afterOpenChange ?? afterVisibleChange}
panelRef={panelRef}
>
<DrawerPanel prefixCls={prefixCls} {...rest} onClose={onClose} />
</RcDrawer>
<zIndexContext.Provider value={contextZIndex}>
<RcDrawer
prefixCls={prefixCls}
onClose={onClose}
maskMotion={maskMotion}
motion={panelMotion}
{...rest}
classNames={{
mask: classNames(rest.classNames?.mask, drawer?.classNames?.mask),
content: classNames(rest.classNames?.content, drawer?.classNames?.content),
}}
styles={{
mask: {
...rest.styles?.mask,
...drawer?.styles?.mask,
},
content: {
...rest.styles?.content,
...drawer?.styles?.content,
},
}}
open={open ?? visible}
mask={mask}
push={push}
width={mergedWidth}
height={mergedHeight}
style={{ ...drawer?.style, ...style }}
className={classNames(drawer?.className, className)}
rootClassName={drawerClassName}
getContainer={getContainer}
afterOpenChange={afterOpenChange ?? afterVisibleChange}
panelRef={panelRef}
zIndex={zIndex}
>
<DrawerPanel prefixCls={prefixCls} {...rest} onClose={onClose} />
</RcDrawer>
</zIndexContext.Provider>
</NoFormStyle>
</NoCompactStyle>,
);
Expand Down