<template>
<view
v-if="visibleSync"
:style="[customStyle, {
zIndex: uZindex - 1
}]"
:class="{ 'u-drawer-visible': showDrawer }"
class="u-drawer"
>
<u-mask :maskClickAble="maskCloseAble" :show="showDrawer && mask" :z-index="zIndex-1" @click="maskClick"></u-mask>
<view
class="u-drawer-content"
@tap="modeCenterClose(mode)"
:class="[
safeAreaInsetBottom ? 'safe-area-inset-bottom' : '',
'u-drawer-' + mode,
showDrawer ? 'u-drawer-content-visible' : '',
zoom && mode == 'center' ? 'u-animation-zoom' : ''
]"
:style="[style]"
>
<view
class="u-mode-center-box"
@tap.stop.prevent
v-if="mode == 'center'"
:style="[centerStyle]"
>
<u-icon
@click="close"
v-if="closeable"
class="u-close"
:class="['u-close--' + closeIconPos]"
:name="closeIcon"
:color="closeIconColor"
:size="closeIconSize"
></u-icon>
<scroll-view class="u-drawer__scroll-view" scroll-y="true">
<slot />
</scroll-view>
</view>
<scroll-view class="u-drawer__scroll-view" scroll-y="true" v-else>
<slot />
</scroll-view>
<view class="u-close" :class="['u-close--' + closeIconPos]">
<u-icon
v-if="mode != 'center' && closeable"
@click="close"
:name="closeIcon"
:color="closeIconColor"
:size="closeIconSize"
></u-icon>
</view>
</view>
</view>
</template>
<script>
export default {
name: 'u-popup',
props: {
show: {
type: Boolean,
default: false
},
mode: {
type: String,
default: 'left'
},
mask: {
type: Boolean,
default: true
},
length: {
type: [Number, String],
default: 'auto'
},
zoom: {
type: Boolean,
default: true
},
safeAreaInsetBottom: {
type: Boolean,
default: false
},
maskCloseAble: {
type: Boolean,
default: true
},
customStyle: {
type: Object,
default() {
return {}
}
},
value: {
type: Boolean,
default: false
},
popup: {
type: Boolean,
default: true
},
borderRadius: {
type: [Number, String],
default: 0
},
zIndex: {
type: [Number, String],
default: ''
},
closeable: {
type: Boolean,
default: false
},
closeIcon: {
type: String,
default: 'close'
},
closeIconPos: {
type: String,
default: 'top-right'
},
closeIconColor: {
type: String,
default: '#909399'
},
closeIconSize: {
type: [String, Number],
default: '15'
},
width: {
type: String,
default: ''
},
height: {
type: String,
default: ''
},
negativeTop: {
type: [String, Number],
default: 0
}
},
data() {
return {
visibleSync: false,
showDrawer: false,
timer: null
}
},
computed: {
style() {
let style = {}
if (this.mode == 'left' || this.mode == 'right') {
if(navigator.userAgent.indexOf("Ultra")>-1 || navigator.userAgent.indexOf("NRD90M")>-1){
style = {
width: this.width
? this.getUnitValue(this.width)
: this.getUnitValue(this.length),
height: '100%',
transform: `translate3D(${
this.mode == 'left' ? '-100%' : '100%'
},0px,0px)`
}
}else{
style = {
width: this.width
? this.getUnitValue(this.width)
: this.getUnitValue(this.length),
height: '100%'
}
}
} else if (this.mode == 'top' || this.mode == 'bottom') {
if(navigator.userAgent.indexOf("Ultra")>-1 || navigator.userAgent.indexOf("NRD90M")>-1){
style = {
width: '100%',
height: this.height
? this.getUnitValue(this.height)
: this.getUnitValue(this.length),
transform: `translate3D(0px,${
this.mode == 'top' ? '-100%' : '100%'
},0px)`
}
}else{
style = {
width: '100%',
height: this.height
? this.getUnitValue(this.height)
: this.getUnitValue(this.length),
}
}
}
style.zIndex = this.uZindex
if (this.borderRadius) {
switch (this.mode) {
case 'left':
style.borderRadius = `0 ${this.borderRadius}rpx ${this.borderRadius}rpx 0`
break
case 'top':
style.borderRadius = `0 0 ${this.borderRadius}rpx ${this.borderRadius}rpx`
break
case 'right':
style.borderRadius = `${this.borderRadius}rpx 0 0 ${this.borderRadius}rpx`
break
case 'bottom':
style.borderRadius = `${this.borderRadius}rpx ${this.borderRadius}rpx 0 0`
break
default:
}
style.overflow = 'hidden'
}
return style
},
centerStyle() {
let style = {}
style.width = this.width
? this.getUnitValue(this.width)
: this.getUnitValue(this.length)
style.height = this.height ? this.getUnitValue(this.height) : 'auto'
style.zIndex = this.uZindex
style.marginTop = `-${this.$u.addUnit(this.negativeTop)}`
if (this.borderRadius) {
style.borderRadius = `${this.borderRadius}rpx`
style.overflow = 'hidden'
}
return style
},
uZindex() {
return this.zIndex ? this.zIndex : this.$u.zIndex.popup
}
},
watch: {
value(val) {
if (val) {
this.open()
} else {
this.close()
}
}
},
mounted() {
this.value && this.open()
},
methods: {
getUnitValue(val) {
if (/(%|px|rpx|auto)$/.test(val)) return val
else return val + 'rpx'
},
maskClick() {
this.close()
},
close() {
this.change('showDrawer', 'visibleSync', false)
},
modeCenterClose(mode) {
if (mode != 'center' || !this.maskCloseAble) return
this.close()
},
open() {
this.change('visibleSync', 'showDrawer', true)
},
change(param1, param2, status) {
if (this.popup == true) this.$emit('input', status)
this[param1] = status
this.$nextTick(() => {
this[param2] = status
this.$emit(status ? 'open' : 'close')
})
}
}
}
</script>
<style scoped lang="scss">
@import '../../libs/css/style.components.scss';
.u-drawer {
display: block;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.u-drawer-content {
display: block;
position: absolute;
z-index: 1003;
transition: all 0.3s linear;
}
.u-drawer__scroll-view {
width: 100%;
height: 100%;
}
.u-drawer-left {
top: 0;
bottom: 0;
left: 0;
background-color: #ffffff;
}
.u-drawer-right {
right: 0;
top: 0;
bottom: 0;
background-color: #ffffff;
}
.u-drawer-top {
top: 0;
left: 0;
right: 0;
background-color: #ffffff;
}
.u-drawer-bottom {
bottom: 0;
left: 0;
right: 0;
background-color: #ffffff;
}
.u-drawer-center {
display: flex;
flex-direction: column;
bottom: 0;
left: 0;
right: 0;
top: 0;
justify-content: center;
align-items: center;
opacity: 0;
z-index: 99999;
}
.u-mode-center-box {
min-width: 50rpx;
min-height: 50rpx;
display: block;
position: relative;
background-color: #ffffff;
}
.u-drawer-content-visible.u-drawer-center {
transform: scale(1);
opacity: 1;
}
.u-animation-zoom {
transform: scale(1.15);
}
.u-drawer-content-visible {
transform: translate3D(0px, 0px, 0px) !important;
}
.u-close {
position: absolute;
z-index: 3;
}
.u-close--top-left {
top: 15rpx;
left: 15rpx;
}
.u-close--top-right {
top: 15rpx;
right: 15rpx;
}
.u-close--bottom-left {
bottom: 15rpx;
left: 15rpx;
}
.u-close--bottom-right {
right: 15rpx;
bottom: 15rpx;
}
</style>