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

perf: 优化 popover 显隐过渡效果 #3140

Open
wants to merge 3 commits into
base: v4
Choose a base branch
from
Open
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
26 changes: 11 additions & 15 deletions src/packages/__VUE/popover/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,21 @@

&-group {
display: block;
// position: relative;
height: 100%;
width: 100%;
}

&-enter-from,
&-leave-to {
transform: scale(var(--transform-scale));
opacity: 0;
}

&-enter-active,
&-leave-active {
transition-timing-function: ease;
}

.nut-popover-menu-item {
display: flex;
align-items: center;
Expand Down Expand Up @@ -226,20 +236,6 @@
}
}

.nut-popover-enter-from,
.nut-popover-leave-active {
transform: scale(0.8);
opacity: 0;
}

.nut-popover-enter-active {
transition-timing-function: ease-out;
}

.nut-popover-leave-active {
transition-timing-function: ease-in;
}

.nut-popover-content-bg {
position: fixed;
height: 100%;
Expand Down
99 changes: 41 additions & 58 deletions src/packages/__VUE/popover/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
class="nut-popover-wrapper"
@click="openPopover"
>
<slot name="reference"></slot
>
<slot name="reference"></slot>
</view>
<view :class="['nut-popover', `nut-popover--${theme}`, `${customClass}`]" :style="getRootPosition">
<nut-popup
v-model:visible="showPopup"
:pop-class="`nut-popover-content nut-popover-content--${location}`"
:style="{ background: bgColor }"
:style="{ background: bgColor, '--transform-scale': DEFAULT_SCALE_TRANSITION }"
position=""
transition="nut-popover"
transition="nut-popover-content"
:overlay="overlay"
:duration="duration"
:overlay-style="overlayStyle"
Expand All @@ -41,9 +40,9 @@
</view>
</template>
<script lang="ts">
import { onMounted, computed, watch, ref, PropType, CSSProperties } from 'vue'
import { onMounted, computed, watch, ref, PropType, CSSProperties, nextTick } from 'vue'
import { createComponent, renderIcon } from '@/packages/utils/create'
import { useTaroRect, useTaroRectById } from '@/packages/utils/useTaroRect'
import { rectTaro, useTaroRect, useTaroRectById } from '@/packages/utils/useTaroRect'
import { PopoverList, PopoverTheme, PopoverLocation, PopoverRootPosition } from './type'
import NutPopup from '../popup/index.taro.vue'
import Taro from '@tarojs/taro'
Expand Down Expand Up @@ -79,6 +78,8 @@ export default create({

const rootPosition = ref<PopoverRootPosition>()

const DEFAULT_SCALE_TRANSITION = 0.8

const elRect = ref({
width: 0,
height: 0
Expand Down Expand Up @@ -131,9 +132,7 @@ export default create({
})

const upperCaseFirst = (str: string) => {
str = str.toLowerCase()
str = str.replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1))
return str
return str.toLowerCase().replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1))
}

const getRootPosition = computed(() => {
Expand Down Expand Up @@ -194,73 +193,56 @@ export default create({
})

const getPopoverContentW = async () => {
useTaroRect(popoverContentRef).then(
(rect: { width: number, height: number }) => {
elRect.value = {
height: rect.height,
width: rect.width
}
},
() => {}
)
try {
const rect = await useTaroRect(popoverContentRef)
elRect.value = {
height: rect.height / DEFAULT_SCALE_TRANSITION,
width: rect.width / DEFAULT_SCALE_TRANSITION
}
} catch (error) {
console.warn('[NutUI] Failed to get rect:', error)
}
}

// 获取宽度
const getContentWidth = () => {
const getContentWidth = async () => {
getPopoverContentW()
Taro.createSelectorQuery()
.selectViewport()
.scrollOffset((res) => {
const distance = res.scrollTop

.scrollOffset(async (result) => {
const distance = result.scrollTop
if (props.targetId) {
useTaroRectById(props.targetId).then(
(rect: any) => {
rootPosition.value = {
width: rect?.width,
height: rect?.height,
left: rect?.left,
top: rect?.top + distance,
right: rect?.right
}
},
() => {}
)
const rect = (await useTaroRectById(props.targetId)) as rectTaro
rootPosition.value = {
width: rect?.width,
height: rect?.height,
left: rect?.left,
top: rect?.top + distance,
right: rect?.right
}
} else {
useTaroRect(popoverRef).then(
(rect: any) => {
rootPosition.value = {
width: rect?.width,
height: rect?.height,
left: rect?.left,
top: rect?.top + distance,
right: rect?.right
}
},
() => {}
)
const rect = await useTaroRect(popoverRef)
rootPosition.value = {
width: rect?.width,
height: rect?.height,
left: rect?.left,
top: rect?.top + distance,
right: rect?.right
}
}
})
.exec()
setTimeout(() => {
getPopoverContentW()
}, 300)
}

onMounted(() => {
setTimeout(() => {
getContentWidth()
}, 300)
getContentWidth()
})

watch(
() => props.visible,
(value) => {
showPopup.value = value
if (value) {
Taro.nextTick(() => {
getContentWidth()
})
}
if (value) nextTick(() => getContentWidth())
}
)

Expand Down Expand Up @@ -306,7 +288,8 @@ export default create({
popoverArrowStyle,
renderIcon,
refRandomId,
clickAway
clickAway,
DEFAULT_SCALE_TRANSITION
}
}
})
Expand Down