diff --git a/src/packages/overlay/overlay.scss b/src/packages/overlay/overlay.scss index 1c71dd1e06..099ed71cdf 100644 --- a/src/packages/overlay/overlay.scss +++ b/src/packages/overlay/overlay.scss @@ -9,6 +9,11 @@ background-color: $overlay-bg-color; } +.nut-overlay-skyline { + width: 100vw; + height: 100vh; +} + .nut-overflow-hidden { overflow: hidden !important; } diff --git a/src/packages/overlay/overlay.taro.tsx b/src/packages/overlay/overlay.taro.tsx index e5e729bb80..1ca3efddc9 100644 --- a/src/packages/overlay/overlay.taro.tsx +++ b/src/packages/overlay/overlay.taro.tsx @@ -4,6 +4,7 @@ import { ITouchEvent, View } from '@tarojs/components' import { ComponentDefaults } from '@/utils/typings' import { useLockScrollTaro } from '@/hooks/taro/use-lock-scoll' import { TaroOverlayProps } from '@/types' +import { isSkyline } from '@/utils/taro/is-skyline' import ConfigurableCSSTransition from '@/utils/taro/ConfigurableCSSTransition' export const defaultOverlayProps: TaroOverlayProps = { @@ -44,7 +45,13 @@ export const Overlay: FunctionComponent< setInnerVisible(visible) }, [visible]) - const classes = classNames(classPrefix, className) + const classes = classNames( + classPrefix, + { + [`${classPrefix}-skyline`]: isSkyline() && !style?.position, + }, + className + ) const styles = { zIndex, ...style, diff --git a/src/packages/popup/__tests__/popup.taro.spec.tsx b/src/packages/popup/__tests__/popup.taro.spec.tsx new file mode 100644 index 0000000000..d9cc51a563 --- /dev/null +++ b/src/packages/popup/__tests__/popup.taro.spec.tsx @@ -0,0 +1,110 @@ +/* eslint-disable global-require */ +import * as React from 'react' +import { render } from '@testing-library/react' +import '@testing-library/jest-dom' +import { isSkyline } from '@/utils/taro/is-skyline' +import { Popup } from '../popup.taro' + +const { MockView } = vi.hoisted(() => { + const React = require('react') + const MockView = React.forwardRef( + ({ children, catchMove, ariaRole, ariaLabel, ...props }: any, ref: any) => + React.createElement('div', { ...props, ref }, children) + ) + MockView.displayName = 'MockView' + return { MockView } +}) + +vi.mock('@tarojs/components', () => ({ + View: MockView, +})) + +vi.mock('@tarojs/taro', () => ({ + default: { getEnv: () => 'WEAPP' }, + getEnv: () => 'WEAPP', + nextTick: (cb: () => void) => cb(), +})) + +vi.mock('@/utils/taro/is-skyline', () => ({ isSkyline: vi.fn() })) + +const mockedIsSkyline = vi.mocked(isSkyline) + +describe('Popup (Taro)', () => { + beforeEach(() => { + mockedIsSkyline.mockReset() + }) + + test('非 Skyline 环境不渲染 wrapper 且不追加 skyline 类,保持原有结构', () => { + mockedIsSkyline.mockReturnValue(false) + const { container } = render( + + Content + + ) + + expect(container.querySelector('.nut-popup-wrapper')).toBeNull() + const popup = container.querySelector('.nut-popup') as HTMLElement + expect(popup).toBeInTheDocument() + const overlay = container.querySelector('.nut-overlay') as HTMLElement + expect(overlay).toBeInTheDocument() + expect(overlay).not.toHaveClass('nut-overlay-skyline') + }) + + test('Skyline 环境使用固定定位 wrapper 包裹弹层,overlay 追加 skyline 类', () => { + mockedIsSkyline.mockReturnValue(true) + const { container } = render( + + Content + + ) + + const wrapper = container.querySelector( + '.nut-popup-wrapper.nut-popup-wrapper-bottom' + ) as HTMLElement + expect(wrapper).toBeInTheDocument() + expect(wrapper.firstElementChild).toHaveClass('nut-popup') + const overlay = container.querySelector('.nut-overlay') as HTMLElement + expect(overlay).toHaveClass('nut-overlay-skyline') + }) + + test('Skyline 环境将调用方 style.zIndex 透传到 wrapper', () => { + mockedIsSkyline.mockReturnValue(true) + const { container, rerender } = render( + + Content + + ) + + const wrapper = container.querySelector('.nut-popup-wrapper') as HTMLElement + expect(wrapper).toHaveStyle({ zIndex: '5000' }) + + rerender( + + Content + + ) + expect(wrapper).toHaveStyle({ zIndex: '0' }) + }) + + test('Skyline 环境设置自定义 style.position 时不渲染 wrapper,保留原定位参照容器', () => { + mockedIsSkyline.mockReturnValue(true) + const { container } = render( +
+ + Content + +
+ ) + + expect(container.querySelector('.nut-popup-wrapper')).toBeNull() + const popup = container.querySelector('.nut-popup') as HTMLElement + expect(popup).toHaveStyle({ position: 'absolute', top: '0px', left: '0px' }) + const overlay = container.querySelector('.nut-overlay') as HTMLElement + expect(overlay).not.toHaveClass('nut-overlay-skyline') + }) +}) diff --git a/src/packages/popup/popup.scss b/src/packages/popup/popup.scss index cfa9ddd001..cc96907eb0 100644 --- a/src/packages/popup/popup.scss +++ b/src/packages/popup/popup.scss @@ -362,6 +362,108 @@ } } +.nut-popup-wrapper { + position: fixed; + top: 0; + left: 0; + bottom: 0; + right: 0; + width: 100%; + height: 100%; + width: 100vw; + height: 100vh; + pointer-events: none; + display: flex; + box-sizing: border-box; + + &-bottom { + flex-direction: column; + justify-content: flex-end; + align-items: center; + + > .nut-popup { + position: relative; + top: auto; + bottom: auto; + left: auto; + right: auto; + width: 100%; + width: 100vw; + box-sizing: border-box; + pointer-events: auto; + } + } + + &-top { + flex-direction: column; + justify-content: flex-start; + align-items: center; + + > .nut-popup { + position: relative; + top: auto; + bottom: auto; + left: auto; + right: auto; + width: 100%; + width: 100vw; + box-sizing: border-box; + pointer-events: auto; + } + } + + &-center { + flex-direction: column; + justify-content: center; + align-items: center; + + > .nut-popup { + position: relative; + top: auto; + bottom: auto; + left: auto; + right: auto; + transform: none; + box-sizing: border-box; + pointer-events: auto; + } + } + + &-left { + flex-direction: row; + justify-content: flex-start; + align-items: stretch; + + > .nut-popup { + position: relative; + top: auto; + bottom: auto; + left: auto; + right: auto; + height: 100%; + box-sizing: border-box; + pointer-events: auto; + } + } + + &-right { + flex-direction: row; + justify-content: flex-end; + align-items: stretch; + + > .nut-popup { + position: relative; + top: auto; + bottom: auto; + left: auto; + right: auto; + height: 100%; + box-sizing: border-box; + pointer-events: auto; + } + } +} + [dir='rtl'] .nut-popup, .nut-rtl .nut-popup { &-title { @@ -411,3 +513,26 @@ right: 0; } } + +[dir='rtl'] .nut-popup-wrapper, +.nut-rtl .nut-popup-wrapper { + &-center > .nut-popup { + left: auto; + right: auto; + transform: none; + } + + &-bottom > .nut-popup, + &-top > .nut-popup { + left: auto; + right: auto; + } + + &-left { + justify-content: flex-end; + } + + &-right { + justify-content: flex-start; + } +} diff --git a/src/packages/popup/popup.taro.tsx b/src/packages/popup/popup.taro.tsx index 7dfff32cee..24e5e6c3a0 100644 --- a/src/packages/popup/popup.taro.tsx +++ b/src/packages/popup/popup.taro.tsx @@ -19,6 +19,7 @@ import Overlay from '@/packages/overlay/index.taro' import { useLockScrollTaro } from '@/hooks/taro/use-lock-scoll' import { TaroPopupProps } from '@/types' import { harmony } from '@/utils/taro/platform' +import { isSkyline } from '@/utils/taro/is-skyline' import { pxTransform } from '@/utils/taro/px-transform' import { useConfig } from '@/packages/configprovider/configprovider.taro' @@ -117,6 +118,8 @@ export const Popup: FunctionComponent< } const contentZIndex = harmony() ? index + 1 : index // 解决harmony层级问题 const popStyles = { zIndex: contentZIndex, minHeight, ...style } + const wrapperZIndex = style?.zIndex ?? contentZIndex + const useSkylineWrapper = isSkyline() && !style?.position const popClassName = classNames( classPrefix, { @@ -393,7 +396,22 @@ export const Popup: FunctionComponent< onClick={handleOverlayClick} /> ) : null} - {renderPop()} + {useSkylineWrapper ? ( + + {renderPop()} + + ) : ( + renderPop() + )} ) }