uni-popup.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" :class="[popupstyle]" @touchmove.stop.prevent="clear">
  3. <uni-transition v-if="maskShow" class="uni-mask--hook" :mode-class="['fade']" :styles="maskClass" :duration="duration" :show="showTrans" @click="onTap" />
  4. <uni-transition :mode-class="ani" :styles="transClass" :duration="duration" :show="showTrans" @click="onTap">
  5. <view
  6. :style="{
  7. borderRadius: radiusStyle.borderRadius
  8. }"
  9. class="uni-popup__wrapper-box"
  10. @click.stop="clear">
  11. <slot />
  12. </view>
  13. </uni-transition>
  14. </view>
  15. </template>
  16. <script>
  17. import uniTransition from '../uni-transition/uni-transition.vue'
  18. import popup from './popup.js'
  19. /**
  20. * PopUp 弹出层
  21. * @description 弹出层组件,为了解决遮罩弹层的问题
  22. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  23. * @property {String} type = [top|center|bottom] 弹出方式
  24. * @value top 顶部弹出
  25. * @value center 中间弹出
  26. * @value bottom 底部弹出
  27. * @value message 消息提示
  28. * @value dialog 对话框
  29. * @value share 底部分享示例
  30. * @property {String} radius wrapper 圆角
  31. * @property {Boolean} animation = [ture|false] 是否开启动画
  32. * @property {Boolean} maskClick = [ture|false] 蒙版点击是否关闭弹窗
  33. * @event {Function} change 打开关闭弹窗触发,e={show: false}
  34. */
  35. export default {
  36. name: 'UniPopup',
  37. components: {
  38. uniTransition
  39. },
  40. mixins: [popup],
  41. provide() {
  42. return {
  43. popup: this
  44. }
  45. },
  46. props: {
  47. // 开启动画
  48. animation: {
  49. type: Boolean,
  50. default: true
  51. },
  52. // 弹出层类型,可选值,top: 顶部弹出层;bottom:底部弹出层;center:全屏弹出层
  53. // message: 消息提示 ; dialog : 对话框
  54. type: {
  55. type: String,
  56. default: 'center'
  57. },
  58. // maskClick
  59. maskClick: {
  60. type: Boolean,
  61. default: true
  62. },
  63. radius: {
  64. type: String,
  65. default: '0 0 0 0'
  66. }
  67. },
  68. data() {
  69. return {
  70. duration: 300,
  71. ani: [],
  72. showPopup: false,
  73. showTrans: false,
  74. maskClass: {
  75. 'position': 'fixed',
  76. 'bottom': 0,
  77. 'top': 0,
  78. 'left': 0,
  79. 'right': 0,
  80. 'backgroundColor': 'rgba(0, 0, 0, 0.4)'
  81. },
  82. transClass: {
  83. 'position': 'fixed',
  84. 'left': 0,
  85. 'right': 0,
  86. },
  87. maskShow: true,
  88. mkclick: true,
  89. popupstyle: 'top'
  90. }
  91. },
  92. computed: {
  93. radiusStyle() {
  94. if (this.$isEmpty(this.radius)) {
  95. return {}
  96. }
  97. const res = []
  98. const arr = this.radius.split(' ')
  99. arr.forEach(item => {
  100. let num = 0
  101. if (item.endsWith('rpx')) {
  102. num = Number(item.replace('rpx', ''))
  103. num = isNaN(num) ? 0 : uni.upx2px(num)
  104. } else if (item.endsWith('px')) {
  105. num = Number(item.replace('px', ''))
  106. num = isNaN(num) ? 0 : num
  107. }
  108. res.push(num + 'px')
  109. })
  110. return {
  111. borderRadius: res.join(' ')
  112. }
  113. }
  114. },
  115. watch: {
  116. /**
  117. * 监听type类型
  118. */
  119. type: {
  120. handler: function(newVal) {
  121. this[this.config[newVal]]()
  122. },
  123. immediate: true
  124. },
  125. /**
  126. * 监听遮罩是否可点击
  127. * @param {Object} val
  128. */
  129. maskClick(val) {
  130. this.mkclick = val
  131. }
  132. },
  133. created() {
  134. this.mkclick = this.maskClick
  135. if (this.animation) {
  136. this.duration = 300
  137. } else {
  138. this.duration = 0
  139. }
  140. },
  141. methods: {
  142. clear(e) {
  143. // TODO nvue 取消冒泡
  144. e.stopPropagation()
  145. },
  146. open() {
  147. this.showPopup = true
  148. this.$nextTick(() => {
  149. new Promise(resolve => {
  150. clearTimeout(this.timer)
  151. this.timer = setTimeout(() => {
  152. this.showTrans = true
  153. // fixed by mehaotian 兼容 app 端
  154. this.$nextTick(() => {
  155. resolve();
  156. })
  157. }, 50);
  158. }).then(res => {
  159. // 自定义打开事件
  160. clearTimeout(this.msgtimer)
  161. this.msgtimer = setTimeout(() => {
  162. this.customOpen && this.customOpen()
  163. }, 100)
  164. this.$emit('change', {
  165. show: true,
  166. type: this.type
  167. })
  168. })
  169. })
  170. },
  171. close(type) {
  172. this.showTrans = false
  173. this.$nextTick(() => {
  174. this.$emit('change', {
  175. show: false,
  176. type: this.type
  177. })
  178. clearTimeout(this.timer)
  179. // 自定义关闭事件
  180. this.customOpen && this.customClose()
  181. this.timer = setTimeout(() => {
  182. this.showPopup = false
  183. }, 300)
  184. })
  185. },
  186. onTap() {
  187. this.$emit('mask-close')
  188. this.mkclick = this.maskClick
  189. if (!this.mkclick) return
  190. this.close()
  191. },
  192. /**
  193. * 顶部弹出样式处理
  194. */
  195. top() {
  196. this.popupstyle = 'top'
  197. this.ani = ['slide-top']
  198. this.transClass = {
  199. 'position': 'fixed',
  200. 'left': 0,
  201. 'right': 0,
  202. }
  203. },
  204. /**
  205. * 底部弹出样式处理
  206. */
  207. bottom() {
  208. this.popupstyle = 'bottom'
  209. this.ani = ['slide-bottom']
  210. this.transClass = {
  211. 'position': 'fixed',
  212. 'left': 0,
  213. 'right': 0,
  214. 'bottom': 0
  215. }
  216. },
  217. /**
  218. * 中间弹出样式处理
  219. */
  220. center() {
  221. this.popupstyle = 'center'
  222. this.ani = ['zoom-out', 'fade']
  223. this.transClass = {
  224. 'position': 'fixed',
  225. /* #ifndef APP-NVUE */
  226. 'display': 'flex',
  227. 'flexDirection': 'column',
  228. /* #endif */
  229. 'bottom': 0,
  230. 'left': 0,
  231. 'right': 0,
  232. 'top': 0,
  233. 'justifyContent': 'center',
  234. 'alignItems': 'center'
  235. }
  236. }
  237. }
  238. }
  239. </script>
  240. <style scoped>
  241. .uni-popup {
  242. position: fixed;
  243. z-index: 500;
  244. }
  245. .uni-popup__mask {
  246. position: absolute;
  247. top: 0;
  248. bottom: 0;
  249. left: 0;
  250. right: 0;
  251. background-color: rgba(0, 0, 0, 0.4);
  252. opacity: 0;
  253. }
  254. .mask-ani {
  255. transition-property: opacity;
  256. transition-duration: 0.2s;
  257. }
  258. .uni-top-mask {
  259. opacity: 1;
  260. }
  261. .uni-bottom-mask {
  262. opacity: 1;
  263. }
  264. .uni-center-mask {
  265. opacity: 1;
  266. }
  267. .uni-popup__wrapper {
  268. /* #ifndef APP-NVUE */
  269. display: block;
  270. /* #endif */
  271. position: absolute;
  272. }
  273. .top {
  274. /* #ifdef H5 */
  275. top: var(--window-top);
  276. /* #endif */
  277. /* #ifndef H5 */
  278. top: 0;
  279. /* #endif */
  280. }
  281. .bottom {
  282. bottom: 0;
  283. }
  284. .uni-popup__wrapper-box {
  285. /* #ifndef APP-NVUE */
  286. display: block;
  287. /* #endif */
  288. position: relative;
  289. }
  290. .bottom .uni-popup__wrapper-box {
  291. /* iphonex 等安全区设置,底部安全区适配 */
  292. /* #ifndef APP-NVUE */
  293. padding: 0;
  294. padding-bottom: constant(safe-area-inset-bottom);
  295. padding-bottom: env(safe-area-inset-bottom);
  296. background-color: white;
  297. /* #endif */
  298. }
  299. .content-ani {
  300. /* transition: transform 0.3s; */
  301. transition-property: transform, opacity;
  302. transition-duration: 0.2s;
  303. }
  304. .uni-top-content {
  305. transform: translateY(0);
  306. }
  307. .uni-bottom-content {
  308. transform: translateY(0);
  309. }
  310. .uni-center-content {
  311. transform: scale(1);
  312. opacity: 1;
  313. }
  314. </style>