mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] add portal mixin (#3583)
This commit is contained in:
parent
1f3c2fa9db
commit
f86ab3a4d5
@ -146,6 +146,7 @@ ActionSheet.props = {
|
|||||||
title: String,
|
title: String,
|
||||||
actions: Array,
|
actions: Array,
|
||||||
cancelText: String,
|
cancelText: String,
|
||||||
|
getContainer: [String, Function],
|
||||||
closeOnClickAction: Boolean,
|
closeOnClickAction: Boolean,
|
||||||
safeAreaInsetBottom: Boolean,
|
safeAreaInsetBottom: Boolean,
|
||||||
overlay: {
|
overlay: {
|
||||||
|
@ -1,11 +1,21 @@
|
|||||||
import { context } from './context';
|
import { context } from './context';
|
||||||
import { TouchMixin } from '../touch';
|
import { TouchMixin } from '../touch';
|
||||||
|
import { PortalMixin } from '../portal';
|
||||||
import { on, off, preventDefault } from '../../utils/dom/event';
|
import { on, off, preventDefault } from '../../utils/dom/event';
|
||||||
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
||||||
import { getScrollEventTarget } from '../../utils/dom/scroll';
|
import { getScrollEventTarget } from '../../utils/dom/scroll';
|
||||||
|
|
||||||
export const PopupMixin = {
|
export const PopupMixin = {
|
||||||
mixins: [TouchMixin],
|
mixins: [
|
||||||
|
TouchMixin,
|
||||||
|
PortalMixin({
|
||||||
|
afterPortal() {
|
||||||
|
if (this.overlay) {
|
||||||
|
updateOverlay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
// whether to show popup
|
// whether to show popup
|
||||||
@ -20,8 +30,6 @@ export const PopupMixin = {
|
|||||||
closeOnClickOverlay: Boolean,
|
closeOnClickOverlay: Boolean,
|
||||||
// z-index
|
// z-index
|
||||||
zIndex: [String, Number],
|
zIndex: [String, Number],
|
||||||
// return the mount node for popup
|
|
||||||
getContainer: [String, Function],
|
|
||||||
// prevent body scroll
|
// prevent body scroll
|
||||||
lockScroll: {
|
lockScroll: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
@ -54,19 +62,12 @@ export const PopupMixin = {
|
|||||||
this.$emit(type);
|
this.$emit(type);
|
||||||
},
|
},
|
||||||
|
|
||||||
getContainer() {
|
|
||||||
this.move();
|
|
||||||
},
|
|
||||||
|
|
||||||
overlay() {
|
overlay() {
|
||||||
this.renderOverlay();
|
this.renderOverlay();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
if (this.getContainer) {
|
|
||||||
this.move();
|
|
||||||
}
|
|
||||||
if (this.value) {
|
if (this.value) {
|
||||||
this.open();
|
this.open();
|
||||||
}
|
}
|
||||||
@ -138,28 +139,6 @@ export const PopupMixin = {
|
|||||||
this.$emit('input', false);
|
this.$emit('input', false);
|
||||||
},
|
},
|
||||||
|
|
||||||
move() {
|
|
||||||
let container;
|
|
||||||
const { getContainer } = this;
|
|
||||||
|
|
||||||
if (getContainer) {
|
|
||||||
container =
|
|
||||||
typeof getContainer === 'string'
|
|
||||||
? document.querySelector(getContainer)
|
|
||||||
: getContainer();
|
|
||||||
} else if (this.$parent) {
|
|
||||||
container = this.$parent.$el;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (container && container !== this.$el.parentNode) {
|
|
||||||
container.appendChild(this.$el);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.overlay) {
|
|
||||||
updateOverlay();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onTouchMove(event) {
|
onTouchMove(event) {
|
||||||
this.touchMove(event);
|
this.touchMove(event);
|
||||||
const direction = this.deltaY > 0 ? '10' : '01';
|
const direction = this.deltaY > 0 ? '10' : '01';
|
||||||
|
48
packages/mixins/portal.js
Normal file
48
packages/mixins/portal.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
function getElement(selector) {
|
||||||
|
if (typeof selector === 'string') {
|
||||||
|
return document.querySelector(selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
return selector();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PortalMixin({ afterPortal }) {
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
getContainer: [String, Function]
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
getContainer() {
|
||||||
|
this.portal();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
if (this.getContainer) {
|
||||||
|
this.portal();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
portal() {
|
||||||
|
const { getContainer } = this;
|
||||||
|
|
||||||
|
let container;
|
||||||
|
if (getContainer) {
|
||||||
|
container = getElement(getContainer);
|
||||||
|
} else if (this.$parent) {
|
||||||
|
container = this.$parent.$el;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (container && container !== this.$el.parentNode) {
|
||||||
|
container.appendChild(this.$el);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (afterPortal) {
|
||||||
|
afterPortal.call(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -58,6 +58,7 @@ Notify.props = {
|
|||||||
...PopupMixin.props,
|
...PopupMixin.props,
|
||||||
className: null as any,
|
className: null as any,
|
||||||
message: [String, Number],
|
message: [String, Number],
|
||||||
|
getContainer: [String, Function],
|
||||||
color: {
|
color: {
|
||||||
type: String,
|
type: String,
|
||||||
default: WHITE
|
default: WHITE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user