Skip to content

Commit 25459b9

Browse files
committed
perf: 优化 popover 显示/隐藏过渡效果
1 parent a415d0b commit 25459b9

2 files changed

Lines changed: 48 additions & 72 deletions

File tree

src/packages/__VUE/popover/index.scss

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,21 @@
103103

104104
&-group {
105105
display: block;
106-
// position: relative;
107106
height: 100%;
108107
width: 100%;
109108
}
110109

110+
&-enter-from,
111+
&-leave-to {
112+
transform: scale(0.8);
113+
opacity: 0;
114+
}
115+
116+
&-enter-active,
117+
&-leave-active {
118+
transition-timing-function: ease;
119+
}
120+
111121
.nut-popover-menu-item {
112122
display: flex;
113123
align-items: center;
@@ -226,20 +236,6 @@
226236
}
227237
}
228238

229-
.nut-popover-enter-from,
230-
.nut-popover-leave-active {
231-
transform: scale(0.8);
232-
opacity: 0;
233-
}
234-
235-
.nut-popover-enter-active {
236-
transition-timing-function: ease-out;
237-
}
238-
239-
.nut-popover-leave-active {
240-
transition-timing-function: ease-in;
241-
}
242-
243239
.nut-popover-content-bg {
244240
position: fixed;
245241
height: 100%;

src/packages/__VUE/popover/index.taro.vue

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
class="nut-popover-wrapper"
77
@click="openPopover"
88
>
9-
<slot name="reference"></slot
10-
>
9+
<slot name="reference"></slot>
1110
</view>
1211
<view :class="['nut-popover', `nut-popover--${theme}`, `${customClass}`]" :style="getRootPosition">
1312
<nut-popup
1413
v-model:visible="showPopup"
1514
:pop-class="`nut-popover-content nut-popover-content--${location}`"
1615
:style="{ background: bgColor }"
1716
position=""
18-
transition="nut-popover"
17+
transition="nut-popover-content"
1918
:overlay="overlay"
2019
:duration="duration"
2120
:overlay-style="overlayStyle"
@@ -41,9 +40,9 @@
4140
</view>
4241
</template>
4342
<script lang="ts">
44-
import { onMounted, computed, watch, ref, PropType, CSSProperties } from 'vue'
43+
import { onMounted, computed, watch, ref, PropType, CSSProperties, nextTick } from 'vue'
4544
import { createComponent, renderIcon } from '@/packages/utils/create'
46-
import { useTaroRect, useTaroRectById } from '@/packages/utils/useTaroRect'
45+
import { rectTaro, useTaroRect, useTaroRectById } from '@/packages/utils/useTaroRect'
4746
import { PopoverList, PopoverTheme, PopoverLocation, PopoverRootPosition } from './type'
4847
import NutPopup from '../popup/index.taro.vue'
4948
import Taro from '@tarojs/taro'
@@ -131,9 +130,7 @@ export default create({
131130
})
132131
133132
const upperCaseFirst = (str: string) => {
134-
str = str.toLowerCase()
135-
str = str.replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1))
136-
return str
133+
return str.toLowerCase().replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1))
137134
}
138135
139136
const getRootPosition = computed(() => {
@@ -194,73 +191,56 @@ export default create({
194191
})
195192
196193
const getPopoverContentW = async () => {
197-
useTaroRect(popoverContentRef).then(
198-
(rect: { width: number, height: number }) => {
199-
elRect.value = {
200-
height: rect.height,
201-
width: rect.width
202-
}
203-
},
204-
() => {}
205-
)
194+
try {
195+
const rect = await useTaroRect(popoverContentRef)
196+
elRect.value = {
197+
height: rect.height,
198+
width: rect.width
199+
}
200+
} catch (error) {
201+
console.warn('[NutUI] Failed to get rect:', error)
202+
}
206203
}
207204
208205
// 获取宽度
209-
const getContentWidth = () => {
206+
const getContentWidth = async () => {
207+
getPopoverContentW()
210208
Taro.createSelectorQuery()
211209
.selectViewport()
212-
.scrollOffset((res) => {
213-
const distance = res.scrollTop
214-
210+
.scrollOffset(async (result) => {
211+
const distance = result.scrollTop
215212
if (props.targetId) {
216-
useTaroRectById(props.targetId).then(
217-
(rect: any) => {
218-
rootPosition.value = {
219-
width: rect?.width,
220-
height: rect?.height,
221-
left: rect?.left,
222-
top: rect?.top + distance,
223-
right: rect?.right
224-
}
225-
},
226-
() => {}
227-
)
213+
const rect = (await useTaroRectById(props.targetId)) as rectTaro
214+
rootPosition.value = {
215+
width: rect?.width,
216+
height: rect?.height,
217+
left: rect?.left,
218+
top: rect?.top + distance,
219+
right: rect?.right
220+
}
228221
} else {
229-
useTaroRect(popoverRef).then(
230-
(rect: any) => {
231-
rootPosition.value = {
232-
width: rect?.width,
233-
height: rect?.height,
234-
left: rect?.left,
235-
top: rect?.top + distance,
236-
right: rect?.right
237-
}
238-
},
239-
() => {}
240-
)
222+
const rect = await useTaroRect(popoverRef)
223+
rootPosition.value = {
224+
width: rect?.width,
225+
height: rect?.height,
226+
left: rect?.left,
227+
top: rect?.top + distance,
228+
right: rect?.right
229+
}
241230
}
242231
})
243232
.exec()
244-
setTimeout(() => {
245-
getPopoverContentW()
246-
}, 300)
247233
}
248234
249235
onMounted(() => {
250-
setTimeout(() => {
251-
getContentWidth()
252-
}, 300)
236+
getContentWidth()
253237
})
254238
255239
watch(
256240
() => props.visible,
257-
(value) => {
241+
async (value) => {
258242
showPopup.value = value
259-
if (value) {
260-
Taro.nextTick(() => {
261-
getContentWidth()
262-
})
263-
}
243+
if (value) nextTick(() => getContentWidth())
264244
}
265245
)
266246

0 commit comments

Comments
 (0)