forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Modal & Select support z-index context to manage z-index (ant-d…
…esign#45346) * feat: z-index manager * feat: z-index manager * feat: update snap * chore: update site-limit * feat: optimize code * feat: optimize code * feat: add test case * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code
- Loading branch information
1 parent
98a8d30
commit dde3651
Showing
13 changed files
with
347 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
import React, { useEffect } from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import zIndexContext from '../zindexContext'; | ||
|
||
import type { ZIndexConsumer, ZIndexContainer } from '../hooks/useZIndex'; | ||
import { consumerBaseZIndexOffset, containerBaseZIndexOffset, useZIndex } from '../hooks/useZIndex'; | ||
|
||
const WrapWithProvider: React.FC<PropsWithChildren<{ containerType: ZIndexContainer }>> = ({ | ||
children, | ||
containerType, | ||
}) => { | ||
const [, contextZIndex] = useZIndex(containerType); | ||
return <zIndexContext.Provider value={contextZIndex}>{children}</zIndexContext.Provider>; | ||
}; | ||
|
||
describe('Test useZIndex hooks', () => { | ||
Object.keys(containerBaseZIndexOffset).forEach((containerKey) => { | ||
Object.keys(consumerBaseZIndexOffset).forEach((key) => { | ||
describe(`Test ${key} zIndex in ${containerKey}`, () => { | ||
it('parentZIndex should be parent zIndex', () => { | ||
const fn = jest.fn(); | ||
const Child = () => { | ||
const [zIndex] = useZIndex(key as ZIndexConsumer); | ||
useEffect(() => { | ||
fn(zIndex); | ||
}, [zIndex]); | ||
return <div>Child</div>; | ||
}; | ||
|
||
const App = () => ( | ||
<WrapWithProvider containerType={containerKey as ZIndexContainer}> | ||
<WrapWithProvider containerType={containerKey as ZIndexContainer}> | ||
<WrapWithProvider containerType={containerKey as ZIndexContainer}> | ||
<Child /> | ||
</WrapWithProvider> | ||
</WrapWithProvider> | ||
</WrapWithProvider> | ||
); | ||
render(<App />); | ||
expect(fn).toHaveBeenLastCalledWith( | ||
(1000 + containerBaseZIndexOffset[containerKey as ZIndexContainer]) * 3 + | ||
consumerBaseZIndexOffset[key as ZIndexConsumer], | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
import useToken from '../../theme/useToken'; | ||
import zIndexContext from '../zindexContext'; | ||
|
||
export type ZIndexContainer = 'Modal' | 'Drawer' | 'Popover' | 'Popconfirm' | 'Tooltip' | 'Tour'; | ||
|
||
export type ZIndexConsumer = | ||
| 'Select' | ||
| 'Dropdown' | ||
| 'Cascader' | ||
| 'TreeSelect' | ||
| 'AutoComplete' | ||
| 'ColorPicker' | ||
| 'DatePicker' | ||
| 'TimePicker' | ||
| 'Menu'; | ||
|
||
export const containerBaseZIndexOffset: Record<ZIndexContainer, number> = { | ||
Modal: 0, | ||
Drawer: 0, | ||
Popover: 30, | ||
Popconfirm: 60, | ||
Tooltip: 70, | ||
Tour: 70, | ||
}; | ||
export const consumerBaseZIndexOffset: Record<ZIndexConsumer, number> = { | ||
Select: 50, | ||
Dropdown: 50, | ||
Cascader: 50, | ||
TreeSelect: 50, | ||
AutoComplete: 50, | ||
ColorPicker: 30, | ||
DatePicker: 50, | ||
TimePicker: 50, | ||
Menu: 50, | ||
}; | ||
|
||
function isContainerType(type: ZIndexContainer | ZIndexConsumer): type is ZIndexContainer { | ||
return type in containerBaseZIndexOffset; | ||
} | ||
|
||
export function useZIndex( | ||
componentType: ZIndexContainer | ZIndexConsumer, | ||
customZIndex?: number, | ||
): [zIndex: number | undefined, contextZIndex: number] { | ||
const [, token] = useToken(); | ||
const parentZIndex = React.useContext(zIndexContext); | ||
const isContainer = isContainerType(componentType); | ||
let zIndex = parentZIndex ?? 0; | ||
if (isContainer) { | ||
zIndex += token.zIndexPopupBase + containerBaseZIndexOffset[componentType]; | ||
} else { | ||
zIndex += consumerBaseZIndexOffset[componentType]; | ||
} | ||
return [parentZIndex === undefined ? customZIndex : zIndex, zIndex]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
const zIndexContext = React.createContext<number | undefined>(undefined); | ||
|
||
export default zIndexContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## zh-CN | ||
|
||
嵌套弹框 | ||
|
||
## en-US | ||
|
||
Nested modal. |
Oops, something went wrong.