Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/packages/overlay/overlay.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
background-color: $overlay-bg-color;
}

.nut-overlay-skyline {
width: 100vw;
height: 100vh;
}

.nut-overflow-hidden {
overflow: hidden !important;
}
Expand Down
9 changes: 8 additions & 1 deletion src/packages/overlay/overlay.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
110 changes: 110 additions & 0 deletions src/packages/popup/__tests__/popup.taro.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(
<Popup visible position="bottom">
Content
</Popup>
)

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(
<Popup visible position="bottom">
Content
</Popup>
)

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(
<Popup visible position="center" style={{ zIndex: 5000 }}>
Content
</Popup>
)

const wrapper = container.querySelector('.nut-popup-wrapper') as HTMLElement
expect(wrapper).toHaveStyle({ zIndex: '5000' })

rerender(
<Popup visible position="center" style={{ zIndex: 0 }}>
Content
</Popup>
)
expect(wrapper).toHaveStyle({ zIndex: '0' })
})

test('Skyline 环境设置自定义 style.position 时不渲染 wrapper,保留原定位参照容器', () => {
mockedIsSkyline.mockReturnValue(true)
const { container } = render(
<div style={{ position: 'relative' }}>
<Popup
visible
portal={false as any}
style={{ position: 'absolute', top: 0, left: 0 }}
overlayStyle={{ position: 'absolute' }}
>
Content
</Popup>
</div>
)

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')
})
})
125 changes: 125 additions & 0 deletions src/packages/popup/popup.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
box-sizing: border-box;
pointer-events: auto;
}
}

&-left {
flex-direction: row;
justify-content: flex-start;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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 {
Expand Down Expand Up @@ -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;
}
}
20 changes: 19 additions & 1 deletion src/packages/popup/popup.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
{
Expand Down Expand Up @@ -393,7 +396,22 @@ export const Popup: FunctionComponent<
onClick={handleOverlayClick}
/>
) : null}
{renderPop()}
{useSkylineWrapper ? (
<View
className={classNames(
`${classPrefix}-wrapper`,
`${classPrefix}-wrapper-${position}`
)}
style={{
zIndex: wrapperZIndex,
display: innerVisible ? 'flex' : 'none',
}}
>
{renderPop()}
</View>
) : (
renderPop()
)}
</>
)
}
Expand Down
Loading