mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-30 10:19:15 +08:00
refactor(Popup): create overlay for every popup (#6357)
This commit is contained in:
parent
6a2a0cc5c6
commit
c5c2e9690d
@ -343,30 +343,3 @@ exports[`title slot 1`] = `
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`toggle method 1`] = `
|
|
||||||
<div class="van-dropdown-menu">
|
|
||||||
<div class="van-dropdown-menu__bar van-dropdown-menu__bar--opened">
|
|
||||||
<div role="button" tabindex="0" class="van-dropdown-menu__item"><span class="van-dropdown-menu__title van-dropdown-menu__title--active van-dropdown-menu__title--down"><div class="van-ellipsis"></div></span></div>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<div class="van-dropdown-item van-dropdown-item--down" style="top: 0px;">
|
|
||||||
<div class="van-overlay" style="z-index: 2017; animation-duration: 0s; position: absolute;" name="van-fade"></div>
|
|
||||||
<div class="van-popup van-popup--top van-dropdown-item__content" style="transition-duration: 0s; z-index: 2018;" name="van-popup-slide-top"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`toggle method 2`] = `
|
|
||||||
<div class="van-dropdown-menu">
|
|
||||||
<div class="van-dropdown-menu__bar van-dropdown-menu__bar--opened">
|
|
||||||
<div role="button" tabindex="0" class="van-dropdown-menu__item"><span class="van-dropdown-menu__title"><div class="van-ellipsis"></div></span></div>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<div class="van-dropdown-item van-dropdown-item--down" style="top: 0px;">
|
|
||||||
<div class="van-popup van-popup--top van-dropdown-item__content" style="transition-duration: 0s; z-index: 2018; display: none;" name="van-popup-slide-top"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
|
@ -224,12 +224,14 @@ test('toggle method', async (done) => {
|
|||||||
// show
|
// show
|
||||||
this.$refs.item.toggle(true, { immediate: true });
|
this.$refs.item.toggle(true, { immediate: true });
|
||||||
await later();
|
await later();
|
||||||
expect(wrapper).toMatchSnapshot();
|
|
||||||
|
const content = wrapper.find('.van-dropdown-item__content');
|
||||||
|
expect(content.element.style.display).toEqual('');
|
||||||
|
|
||||||
// hide
|
// hide
|
||||||
this.$refs.item.toggle(false, { immediate: true });
|
this.$refs.item.toggle(false, { immediate: true });
|
||||||
await later();
|
await later();
|
||||||
expect(wrapper).toMatchSnapshot();
|
expect(content.element.style.display).toEqual('none');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
},
|
},
|
||||||
|
@ -2,6 +2,7 @@ import { OverlayConfig } from './overlay';
|
|||||||
|
|
||||||
export type StackItem = {
|
export type StackItem = {
|
||||||
vm: any;
|
vm: any;
|
||||||
|
overlay: any;
|
||||||
config: OverlayConfig;
|
config: OverlayConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -9,8 +10,7 @@ export const context = {
|
|||||||
zIndex: 2000,
|
zIndex: 2000,
|
||||||
lockCount: 0,
|
lockCount: 0,
|
||||||
stack: [] as StackItem[],
|
stack: [] as StackItem[],
|
||||||
|
find(vm: any): StackItem | undefined {
|
||||||
get top(): StackItem {
|
return this.stack.filter((item) => item.vm === vm)[0];
|
||||||
return this.stack[this.stack.length - 1];
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
// Context
|
// Context
|
||||||
import { context } from './context';
|
import { context } from './context';
|
||||||
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
import {
|
||||||
|
openOverlay,
|
||||||
|
closeOverlay,
|
||||||
|
updateOverlay,
|
||||||
|
removeOverlay,
|
||||||
|
} from './overlay';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { on, off, preventDefault } from '../../utils/dom/event';
|
import { on, off, preventDefault } from '../../utils/dom/event';
|
||||||
@ -94,7 +99,8 @@ export function PopupMixin(options = {}) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
this.close();
|
this.removeLock();
|
||||||
|
removeOverlay(this);
|
||||||
|
|
||||||
if (this.getContainer) {
|
if (this.getContainer) {
|
||||||
removeNode(this.$el);
|
removeNode(this.$el);
|
||||||
@ -123,7 +129,10 @@ export function PopupMixin(options = {}) {
|
|||||||
|
|
||||||
this.opened = true;
|
this.opened = true;
|
||||||
this.renderOverlay();
|
this.renderOverlay();
|
||||||
|
this.addLock();
|
||||||
|
},
|
||||||
|
|
||||||
|
addLock() {
|
||||||
if (this.lockScroll) {
|
if (this.lockScroll) {
|
||||||
on(document, 'touchstart', this.touchStart);
|
on(document, 'touchstart', this.touchStart);
|
||||||
on(document, 'touchmove', this.onTouchMove);
|
on(document, 'touchmove', this.onTouchMove);
|
||||||
@ -135,11 +144,7 @@ export function PopupMixin(options = {}) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
close() {
|
removeLock() {
|
||||||
if (!this.opened) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.lockScroll) {
|
if (this.lockScroll) {
|
||||||
context.lockCount--;
|
context.lockCount--;
|
||||||
off(document, 'touchstart', this.touchStart);
|
off(document, 'touchstart', this.touchStart);
|
||||||
@ -149,9 +154,16 @@ export function PopupMixin(options = {}) {
|
|||||||
document.body.classList.remove('van-overflow-hidden');
|
document.body.classList.remove('van-overflow-hidden');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
close() {
|
||||||
|
if (!this.opened) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.opened = false;
|
|
||||||
closeOverlay(this);
|
closeOverlay(this);
|
||||||
|
this.opened = false;
|
||||||
|
this.removeLock();
|
||||||
this.$emit('input', false);
|
this.$emit('input', false);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import Overlay from '../../overlay';
|
import Overlay from '../../overlay';
|
||||||
import { context } from './context';
|
import { context } from './context';
|
||||||
import { mount } from '../../utils/functional';
|
import { mount } from '../../utils/functional';
|
||||||
|
import { removeNode } from '../../utils/dom/node';
|
||||||
|
|
||||||
export type OverlayConfig = {
|
export type OverlayConfig = {
|
||||||
zIndex?: number;
|
zIndex?: number;
|
||||||
@ -13,12 +14,11 @@ const defaultConfig: OverlayConfig = {
|
|||||||
customStyle: {},
|
customStyle: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
let overlay: any;
|
function mountOverlay(vm: any) {
|
||||||
|
return mount(Overlay, {
|
||||||
// close popup when click overlay && closeOnClickOverlay is true
|
on: {
|
||||||
function onClickOverlay(): void {
|
// close popup when overlay clicked & closeOnClickOverlay is true
|
||||||
if (context.top) {
|
click() {
|
||||||
const { vm } = context.top;
|
|
||||||
vm.$emit('click-overlay');
|
vm.$emit('click-overlay');
|
||||||
|
|
||||||
if (vm.closeOnClickOverlay) {
|
if (vm.closeOnClickOverlay) {
|
||||||
@ -28,56 +28,47 @@ function onClickOverlay(): void {
|
|||||||
vm.close();
|
vm.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
|
|
||||||
function mountOverlay() {
|
|
||||||
overlay = mount(Overlay, {
|
|
||||||
on: {
|
|
||||||
click: onClickOverlay,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateOverlay(): void {
|
export function updateOverlay(vm: any): void {
|
||||||
if (!overlay) {
|
const item = context.find(vm);
|
||||||
mountOverlay();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context.top) {
|
|
||||||
const { vm, config } = context.top;
|
|
||||||
|
|
||||||
|
if (item) {
|
||||||
const el = vm.$el;
|
const el = vm.$el;
|
||||||
|
const { config, overlay } = item;
|
||||||
|
|
||||||
if (el && el.parentNode) {
|
if (el && el.parentNode) {
|
||||||
el.parentNode.insertBefore(overlay.$el, el);
|
el.parentNode.insertBefore(overlay.$el, el);
|
||||||
} else {
|
|
||||||
document.body.appendChild(overlay.$el);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(overlay, defaultConfig, config, {
|
Object.assign(overlay, defaultConfig, config, {
|
||||||
show: true,
|
show: true,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
overlay.show = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function openOverlay(vm: any, config: OverlayConfig): void {
|
export function openOverlay(vm: any, config: OverlayConfig): void {
|
||||||
if (!context.stack.some((item) => item.vm === vm)) {
|
if (!context.find(vm)) {
|
||||||
context.stack.push({ vm, config });
|
const overlay = mountOverlay(vm);
|
||||||
updateOverlay();
|
context.stack.push({ vm, config, overlay });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateOverlay(vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function closeOverlay(vm: any): void {
|
export function closeOverlay(vm: any): void {
|
||||||
const { stack } = context;
|
const item = context.find(vm);
|
||||||
|
if (item) {
|
||||||
if (stack.length) {
|
item.overlay.show = false;
|
||||||
if (context.top.vm === vm) {
|
}
|
||||||
stack.pop();
|
}
|
||||||
updateOverlay();
|
|
||||||
} else {
|
export function removeOverlay(vm: any) {
|
||||||
context.stack = stack.filter((item) => item.vm !== vm);
|
const item = context.find(vm);
|
||||||
}
|
if (item) {
|
||||||
|
removeNode(item.overlay.$el);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user