Skip to content

Commit

Permalink
feat: Tooltip support z-index manager (ant-design#45422)
Browse files Browse the repository at this point in the history
* feat: Tooltip support z-index manager

* feat: optimize code
  • Loading branch information
kiner-tang authored Oct 20, 2023
1 parent 8c49b38 commit bf1468a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ exports[`renders components/tooltip/demo/disabled.tsx extend context correctly 1
</div>
<div
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-select-dropdown-empty 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: 1120;"
>
<div>
<div
Expand Down
29 changes: 29 additions & 0 deletions components/tooltip/__tests__/tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Group from '../../input/Group';
import Radio from '../../radio';
import Switch from '../../switch';
import { isTooltipOpen } from './util';
import { Select } from 'antd';

describe('Tooltip', () => {
mountTest(Tooltip);
Expand Down Expand Up @@ -603,4 +604,32 @@ describe('Tooltip', () => {
expect(error).toHaveBeenCalled();
error.mockRestore();
});
it('z-index should be accumulated in nested Tooltip', () => {
const options = [
{
label: 'Option 1',
value: '1',
},
{
label: 'Option 2',
value: '2',
},
];
render(
<>
<Select open options={options} popupClassName="select0" />
<Tooltip open title="test1" rootClassName="test1">
<Select open options={options} popupClassName="select1" />
<Tooltip open title="test2" rootClassName="test2">
<Select open options={options} popupClassName="select2" />
</Tooltip>
</Tooltip>
</>,
);
expect((document.querySelector('.test1') as HTMLDivElement)!.style.zIndex).toBeFalsy();
expect((document.querySelector('.test2') as HTMLDivElement)!.style.zIndex).toBe('2140');
expect((document.querySelector('.select0') as HTMLDivElement)!.style.zIndex).toBeFalsy();
expect((document.querySelector('.select1') as HTMLDivElement)!.style.zIndex).toBe('1120');
expect((document.querySelector('.select2') as HTMLDivElement)!.style.zIndex).toBe('2190');
});
});
16 changes: 14 additions & 2 deletions components/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';

import type { PresetColorType } from '../_util/colors';
import type { RenderFunction } from '../_util/getRenderPropValue';
import { useZIndex } from '../_util/hooks/useZIndex';
import { getTransitionName } from '../_util/motion';
import type { AdjustOverflow, PlacementsConfig } from '../_util/placements';
import getPlacements from '../_util/placements';
import { cloneElement, isFragment, isValidElement } from '../_util/reactNode';
import type { LiteralUnion } from '../_util/type';
import { devUseWarning } from '../_util/warning';
import zIndexContext from '../_util/zindexContext';
import { ConfigContext } from '../config-provider';
import { NoCompactStyle } from '../space/Compact';
import { useToken } from '../theme/internal';
Expand Down Expand Up @@ -292,9 +294,13 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
hashId,
);

return wrapSSR(
// ============================ zIndex ============================
const [zIndex, contextZIndex] = useZIndex('Tooltip', otherProps.zIndex);

const content = (
<RcTooltip
{...otherProps}
zIndex={zIndex}
showArrow={mergedShowArrow}
placement={placement}
mouseEnterDelay={mouseEnterDelay}
Expand All @@ -318,8 +324,14 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
destroyTooltipOnHide={!!destroyTooltipOnHide}
>
{tempOpen ? cloneElement(child, { className: childCls }) : child}
</RcTooltip>,
</RcTooltip>
);

if (injectFromPopover) {
return wrapSSR(content);
}

return wrapSSR(<zIndexContext.Provider value={contextZIndex}>{content}</zIndexContext.Provider>);
}) as React.ForwardRefExoticComponent<
React.PropsWithoutRef<TooltipProps> & React.RefAttributes<unknown>
> & {
Expand Down

0 comments on commit bf1468a

Please sign in to comment.