chore(Popup): useLazyRender

This commit is contained in:
chenjiahan 2020-09-14 20:05:45 +08:00
parent 9465653f42
commit 708c37c759
2 changed files with 15 additions and 23 deletions

View File

@ -1,7 +1,7 @@
import { ref, Ref, watch } from 'vue'; import { ref, isRef, watch, WatchSource } from 'vue';
export function useLazyRender(show: Ref<boolean>) { export function useLazyRender(show: WatchSource<boolean>) {
const inited = ref(show.value); const inited = ref(isRef(show) ? show.value : show());
watch(show, (value) => { watch(show, (value) => {
if (value) { if (value) {

View File

@ -1,8 +1,7 @@
// Utils // Utils
import { import {
ref,
watch, watch,
computed,
reactive,
Teleport, Teleport,
onMounted, onMounted,
Transition, Transition,
@ -12,7 +11,8 @@ import {
} from 'vue'; } from 'vue';
import { createNamespace, isDef } from '../utils'; import { createNamespace, isDef } from '../utils';
// Mixins // Composition
import { useLazyRender } from '../composition/use-lazy-render';
import { CloseOnPopstateMixin } from '../mixins/close-on-popstate'; import { CloseOnPopstateMixin } from '../mixins/close-on-popstate';
// Components // Components
@ -76,6 +76,10 @@ export default createComponent({
closeable: Boolean, closeable: Boolean,
transition: String, transition: String,
safeAreaInsetBottom: Boolean, safeAreaInsetBottom: Boolean,
position: {
type: String,
default: 'center',
},
closeIcon: { closeIcon: {
type: String, type: String,
default: 'cross', default: 'cross',
@ -84,10 +88,6 @@ export default createComponent({
type: String, type: String,
default: 'top-right', default: 'top-right',
}, },
position: {
type: String,
default: 'center',
},
}, },
emits: [ emits: [
@ -104,12 +104,9 @@ export default createComponent({
let opened; let opened;
let shouldReopen; let shouldReopen;
const state = reactive({ const zIndex = ref();
inited: props.show,
zIndex: null,
});
const shouldRender = computed(() => state.inited || !props.lazyRender); const shouldRender = useLazyRender(() => props.show || !props.lazyRender);
const lockScroll = () => { const lockScroll = () => {
if (props.lockScroll) { if (props.lockScroll) {
@ -130,10 +127,6 @@ export default createComponent({
} }
}; };
const updateZIndex = () => {
state.zIndex = ++context.zIndex;
};
const open = () => { const open = () => {
if (opened) { if (opened) {
return; return;
@ -144,8 +137,8 @@ export default createComponent({
} }
opened = true; opened = true;
updateZIndex();
lockScroll(); lockScroll();
zIndex.value = ++context.zIndex;
}; };
const close = () => { const close = () => {
@ -171,7 +164,7 @@ export default createComponent({
show={props.show} show={props.show}
class={props.overlayClass} class={props.overlayClass}
style={props.overlayStyle} style={props.overlayStyle}
zIndex={state.zIndex} zIndex={zIndex.value}
duration={props.duration} duration={props.duration}
onClick={onClickOverlay} onClick={onClickOverlay}
/> />
@ -211,7 +204,7 @@ export default createComponent({
transition || (isCenter ? 'van-fade' : `van-popup-slide-${position}`); transition || (isCenter ? 'van-fade' : `van-popup-slide-${position}`);
const style = { const style = {
zIndex: state.zIndex, zIndex: zIndex.value,
}; };
if (isDef(duration)) { if (isDef(duration)) {
@ -249,7 +242,6 @@ export default createComponent({
() => props.show, () => props.show,
(value) => { (value) => {
if (value) { if (value) {
state.inited = true;
open(); open();
emit('open'); emit('open');
} else { } else {