uni-popup.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" :class="[popupstyle, isDesktop ? 'fixforpc-z-index' : '']" @touchmove.stop.prevent="clear">
  3. <view @touchstart="touchstart" >
  4. <uni-transition key="1" v-if="maskShow" name="mask" mode-class="fade" :styles="maskClass" :duration="duration" :show="showTrans" @click="onTap" />
  5. <uni-transition key="2" :mode-class="ani" name="content" :styles="transClass" :duration="duration" :show="showTrans" @click="onTap">
  6. <view class="uni-popup__wrapper" :style="{ backgroundColor: bg }" :class="[popupstyle]" @click="clear"><slot /></view>
  7. </uni-transition>
  8. </view>
  9. <!-- #ifdef H5 -->
  10. <keypress v-if="maskShow" @esc="onTap" />
  11. <!-- #endif -->
  12. </view>
  13. </template>
  14. <script>
  15. // #ifdef H5
  16. import keypress from './keypress.js'
  17. // #endif
  18. /**
  19. * PopUp 弹出层
  20. * @description 弹出层组件,为了解决遮罩弹层的问题
  21. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  22. * @property {String} type = [top|center|bottom|left|right|message|dialog|share] 弹出方式
  23. * @value top 顶部弹出
  24. * @value center 中间弹出
  25. * @value bottom 底部弹出
  26. * @value left 左侧弹出
  27. * @value right 右侧弹出
  28. * @value message 消息提示
  29. * @value dialog 对话框
  30. * @value share 底部分享示例
  31. * @property {Boolean} animation = [true|false] 是否开启动画
  32. * @property {Boolean} maskClick = [true|false] 蒙版点击是否关闭弹窗
  33. * @property {String} backgroundColor 主窗口背景色
  34. * @property {Boolean} safeArea 是否适配底部安全区
  35. * @event {Function} change 打开关闭弹窗触发,e={show: false}
  36. * @event {Function} maskClick 点击遮罩触发
  37. */
  38. export default {
  39. name: 'uniPopup',
  40. components: {
  41. // #ifdef H5
  42. keypress
  43. // #endif
  44. },
  45. emits:['change','maskClick'],
  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. backgroundColor: {
  64. type: String,
  65. default: 'none'
  66. },
  67. safeArea:{
  68. type: Boolean,
  69. default: true
  70. }
  71. },
  72. watch: {
  73. /**
  74. * 监听type类型
  75. */
  76. type: {
  77. handler: function(type) {
  78. if (!this.config[type]) return
  79. this[this.config[type]](true)
  80. },
  81. immediate: true
  82. },
  83. isDesktop: {
  84. handler: function(newVal) {
  85. if (!this.config[newVal]) return
  86. this[this.config[this.type]](true)
  87. },
  88. immediate: true
  89. },
  90. /**
  91. * 监听遮罩是否可点击
  92. * @param {Object} val
  93. */
  94. maskClick: {
  95. handler: function(val) {
  96. this.mkclick = val
  97. },
  98. immediate: true
  99. }
  100. },
  101. data() {
  102. return {
  103. duration: 300,
  104. ani: [],
  105. showPopup: false,
  106. showTrans: false,
  107. popupWidth: 0,
  108. popupHeight: 0,
  109. config: {
  110. top: 'top',
  111. bottom: 'bottom',
  112. center: 'center',
  113. left: 'left',
  114. right: 'right',
  115. message: 'top',
  116. dialog: 'center',
  117. share: 'bottom'
  118. },
  119. maskClass: {
  120. position: 'fixed',
  121. bottom: 0,
  122. top: 0,
  123. left: 0,
  124. right: 0,
  125. backgroundColor: 'rgba(0, 0, 0, 0.4)'
  126. },
  127. transClass: {
  128. position: 'fixed',
  129. left: 0,
  130. right: 0
  131. },
  132. maskShow: true,
  133. mkclick: true,
  134. popupstyle: this.isDesktop ? 'fixforpc-top' : 'top'
  135. }
  136. },
  137. computed: {
  138. isDesktop() {
  139. return this.popupWidth >= 500 && this.popupHeight >= 500
  140. },
  141. bg() {
  142. if (this.backgroundColor === '' || this.backgroundColor === 'none') {
  143. return 'transparent'
  144. }
  145. return this.backgroundColor
  146. }
  147. },
  148. mounted() {
  149. const fixSize = () => {
  150. const { windowWidth, windowHeight, windowTop, safeAreaInsets } = uni.getSystemInfoSync()
  151. this.popupWidth = windowWidth
  152. this.popupHeight = windowHeight + windowTop
  153. // 是否适配底部安全区
  154. if(this.safeArea){
  155. this.safeAreaInsets = safeAreaInsets
  156. }else{
  157. this.safeAreaInsets = 0
  158. }
  159. }
  160. fixSize()
  161. // #ifdef H5
  162. // window.addEventListener('resize', fixSize)
  163. // this.$once('hook:beforeDestroy', () => {
  164. // window.removeEventListener('resize', fixSize)
  165. // })
  166. // #endif
  167. },
  168. created() {
  169. this.mkclick = this.maskClick
  170. if (this.animation) {
  171. this.duration = 300
  172. } else {
  173. this.duration = 0
  174. }
  175. // TODO 处理 message 组件生命周期异常的问题
  176. this.messageChild = null
  177. // TODO 解决头条冒泡的问题
  178. this.clearPropagation = false
  179. },
  180. methods: {
  181. /**
  182. * 公用方法,不显示遮罩层
  183. */
  184. closeMask() {
  185. this.maskShow = false
  186. },
  187. /**
  188. * 公用方法,遮罩层禁止点击
  189. */
  190. disableMask() {
  191. this.mkclick = false
  192. },
  193. // TODO nvue 取消冒泡
  194. clear(e) {
  195. // #ifndef APP-NVUE
  196. e.stopPropagation()
  197. // #endif
  198. this.clearPropagation = true
  199. },
  200. open(direction) {
  201. let innerType = ['top', 'center', 'bottom', 'left', 'right', 'message', 'dialog', 'share']
  202. if (!(direction && innerType.indexOf(direction) !== -1)) {
  203. direction = this.type
  204. }
  205. if (!this.config[direction]) {
  206. return
  207. }
  208. this[this.config[direction]]()
  209. this.$emit('change', {
  210. show: true,
  211. type: direction
  212. })
  213. },
  214. close(type) {
  215. this.showTrans = false
  216. this.$emit('change', {
  217. show: false,
  218. type: this.type
  219. })
  220. clearTimeout(this.timer)
  221. // // 自定义关闭事件
  222. // this.customOpen && this.customClose()
  223. this.timer = setTimeout(() => {
  224. this.showPopup = false
  225. }, 300)
  226. },
  227. // TODO 处理冒泡事件,头条的冒泡事件有问题 ,先这样兼容
  228. touchstart(){
  229. this.clearPropagation = false
  230. },
  231. onTap() {
  232. if (this.clearPropagation) {
  233. // fix by mehaotian 兼容 nvue
  234. this.clearPropagation = false
  235. return
  236. }
  237. this.$emit('maskClick')
  238. if (!this.mkclick) return
  239. this.close()
  240. },
  241. /**
  242. * 顶部弹出样式处理
  243. */
  244. top(type) {
  245. this.popupstyle = this.isDesktop ? 'fixforpc-top' : 'top'
  246. this.ani = ['slide-top']
  247. this.transClass = {
  248. position: 'fixed',
  249. left: 0,
  250. right: 0,
  251. backgroundColor: this.bg
  252. }
  253. // TODO 兼容 type 属性 ,后续会废弃
  254. if (type) return
  255. this.showPopup = true
  256. this.showTrans = true
  257. this.$nextTick(() => {
  258. if (this.messageChild && this.type === 'message') {
  259. this.messageChild.timerClose()
  260. }
  261. })
  262. },
  263. /**
  264. * 底部弹出样式处理
  265. */
  266. bottom(type) {
  267. this.popupstyle = 'bottom'
  268. this.ani = ['slide-bottom']
  269. this.transClass = {
  270. position: 'fixed',
  271. left: 0,
  272. right: 0,
  273. bottom: 0,
  274. paddingBottom: (this.safeAreaInsets && this.safeAreaInsets.bottom) || 0,
  275. backgroundColor: this.bg
  276. }
  277. // TODO 兼容 type 属性 ,后续会废弃
  278. if (type) return
  279. this.showPopup = true
  280. this.showTrans = true
  281. },
  282. /**
  283. * 中间弹出样式处理
  284. */
  285. center(type) {
  286. this.popupstyle = 'center'
  287. this.ani = ['zoom-out', 'fade']
  288. this.transClass = {
  289. position: 'fixed',
  290. /* #ifndef APP-NVUE */
  291. display: 'flex',
  292. flexDirection: 'column',
  293. /* #endif */
  294. bottom: 0,
  295. left: 0,
  296. right: 0,
  297. top: 0,
  298. justifyContent: 'center',
  299. alignItems: 'center'
  300. }
  301. // TODO 兼容 type 属性 ,后续会废弃
  302. if (type) return
  303. this.showPopup = true
  304. this.showTrans = true
  305. },
  306. left(type) {
  307. this.popupstyle = 'left'
  308. this.ani = ['slide-left']
  309. this.transClass = {
  310. position: 'fixed',
  311. left: 0,
  312. bottom: 0,
  313. top: 0,
  314. backgroundColor: this.bg,
  315. /* #ifndef APP-NVUE */
  316. display: 'flex',
  317. flexDirection: 'column'
  318. /* #endif */
  319. }
  320. // TODO 兼容 type 属性 ,后续会废弃
  321. if (type) return
  322. this.showPopup = true
  323. this.showTrans = true
  324. },
  325. right(type) {
  326. this.popupstyle = 'right'
  327. this.ani = ['slide-right']
  328. this.transClass = {
  329. position: 'fixed',
  330. bottom: 0,
  331. right: 0,
  332. top: 0,
  333. backgroundColor: this.bg,
  334. /* #ifndef APP-NVUE */
  335. display: 'flex',
  336. flexDirection: 'column'
  337. /* #endif */
  338. }
  339. // TODO 兼容 type 属性 ,后续会废弃
  340. if (type) return
  341. this.showPopup = true
  342. this.showTrans = true
  343. }
  344. }
  345. }
  346. </script>
  347. <style lang="scss" scoped>
  348. .uni-popup {
  349. position: fixed;
  350. /* #ifndef APP-NVUE */
  351. z-index: 99;
  352. /* #endif */
  353. &.top,
  354. &.left,
  355. &.right {
  356. /* #ifdef H5 */
  357. top: var(--window-top);
  358. /* #endif */
  359. /* #ifndef H5 */
  360. top: 0;
  361. /* #endif */
  362. }
  363. .uni-popup__wrapper {
  364. /* #ifndef APP-NVUE */
  365. display: block;
  366. /* #endif */
  367. position: relative;
  368. /* iphonex 等安全区设置,底部安全区适配 */
  369. /* #ifndef APP-NVUE */
  370. // padding-bottom: constant(safe-area-inset-bottom);
  371. // padding-bottom: env(safe-area-inset-bottom);
  372. /* #endif */
  373. &.left,
  374. &.right {
  375. /* #ifdef H5 */
  376. padding-top: var(--window-top);
  377. /* #endif */
  378. /* #ifndef H5 */
  379. padding-top: 0;
  380. /* #endif */
  381. flex: 1;
  382. }
  383. }
  384. }
  385. .fixforpc-z-index {
  386. /* #ifndef APP-NVUE */
  387. z-index: 999;
  388. /* #endif */
  389. }
  390. .fixforpc-top {
  391. top: 0;
  392. }
  393. </style>