uni-popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" :class="[popupstyle, isDesktop ? 'fixforpc-z-index' : '']">
  3. <view @touchstart="touchstart">
  4. <uni-transition key="1" v-if="maskShow" name="mask" :styles="maskClass"
  5. :duration="duration" :show="showTrans" @click="onTap" />
  6. <uni-transition key="2" :mode-class="ani" name="content" :styles="transClass" :duration="duration"
  7. :show="showTrans" @click="onTap">
  8. <view class="uni-popup__wrapper" :style="{ backgroundColor: bg }" :class="[popupstyle]" @click="clear">
  9. <slot />
  10. </view>
  11. </uni-transition>
  12. </view>
  13. <!-- #ifdef H5 -->
  14. <keypress v-if="maskShow" @esc="onTap" />
  15. <!-- #endif -->
  16. </view>
  17. </template>
  18. <script>
  19. // #ifdef H5
  20. import keypress from './keypress.js'
  21. // #endif
  22. /**
  23. * PopUp 弹出层
  24. * @description 弹出层组件,为了解决遮罩弹层的问题
  25. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  26. * @property {String} type = [top|center|bottom|left|right|message|dialog|share] 弹出方式
  27. * @value top 顶部弹出
  28. * @value center 中间弹出
  29. * @value bottom 底部弹出
  30. * @value left 左侧弹出
  31. * @value right 右侧弹出
  32. * @value message 消息提示
  33. * @value dialog 对话框
  34. * @value share 底部分享示例
  35. * @property {Boolean} animation = [true|false] 是否开启动画
  36. * @property {Boolean} maskClick = [true|false] 蒙版点击是否关闭弹窗(废弃)
  37. * @property {Boolean} isMaskClick = [true|false] 蒙版点击是否关闭弹窗
  38. * @property {String} backgroundColor 主窗口背景色
  39. * @property {String} maskBackgroundColor 蒙版颜色
  40. * @property {Boolean} safeArea 是否适配底部安全区
  41. * @event {Function} change 打开关闭弹窗触发,e={show: false}
  42. * @event {Function} maskClick 点击遮罩触发
  43. */
  44. export default {
  45. name: 'uniPopup',
  46. components: {
  47. // #ifdef H5
  48. keypress
  49. // #endif
  50. },
  51. emits: ['change', 'maskClick'],
  52. props: {
  53. // 开启动画
  54. animation: {
  55. type: Boolean,
  56. default: true
  57. },
  58. // 弹出层类型,可选值,top: 顶部弹出层;bottom:底部弹出层;center:全屏弹出层
  59. // message: 消息提示 ; dialog : 对话框
  60. type: {
  61. type: String,
  62. default: 'center'
  63. },
  64. // maskClick
  65. isMaskClick: {
  66. type: Boolean,
  67. default: null
  68. },
  69. // TODO 2 个版本后废弃属性 ,使用 isMaskClick
  70. maskClick: {
  71. type: Boolean,
  72. default: null
  73. },
  74. backgroundColor: {
  75. type: String,
  76. default: 'none'
  77. },
  78. safeArea: {
  79. type: Boolean,
  80. default: true
  81. },
  82. maskBackgroundColor: {
  83. type: String,
  84. default: 'rgba(0, 0, 0, 0.4)'
  85. },
  86. },
  87. watch: {
  88. /**
  89. * 监听type类型
  90. */
  91. type: {
  92. handler: function(type) {
  93. if (!this.config[type]) return
  94. this[this.config[type]](true)
  95. },
  96. immediate: true
  97. },
  98. isDesktop: {
  99. handler: function(newVal) {
  100. if (!this.config[newVal]) return
  101. this[this.config[this.type]](true)
  102. },
  103. immediate: true
  104. },
  105. /**
  106. * 监听遮罩是否可点击
  107. * @param {Object} val
  108. */
  109. maskClick: {
  110. handler: function(val) {
  111. this.mkclick = val
  112. },
  113. immediate: true
  114. },
  115. isMaskClick: {
  116. handler: function(val) {
  117. this.mkclick = val
  118. },
  119. immediate: true
  120. },
  121. // H5 下禁止底部滚动
  122. showPopup(show) {
  123. // #ifdef H5
  124. // fix by mehaotian 处理 h5 滚动穿透的问题
  125. document.getElementsByTagName('body')[0].style.overflow = show ? 'hidden' : 'visible'
  126. // #endif
  127. }
  128. },
  129. data() {
  130. return {
  131. duration: 300,
  132. ani: [],
  133. showPopup: false,
  134. showTrans: false,
  135. popupWidth: 0,
  136. popupHeight: 0,
  137. config: {
  138. top: 'top',
  139. bottom: 'bottom',
  140. center: 'center',
  141. left: 'left',
  142. right: 'right',
  143. message: 'top',
  144. dialog: 'center',
  145. share: 'bottom'
  146. },
  147. maskClass: {
  148. position: 'fixed',
  149. bottom: 0,
  150. top: 0,
  151. left: 0,
  152. right: 0,
  153. backgroundColor: 'rgba(0, 0, 0, 0.4)'
  154. },
  155. transClass: {
  156. position: 'fixed',
  157. left: 0,
  158. right: 0
  159. },
  160. maskShow: true,
  161. mkclick: true,
  162. popupstyle: 'top'
  163. }
  164. },
  165. computed: {
  166. isDesktop() {
  167. return this.popupWidth >= 500 && this.popupHeight >= 500
  168. },
  169. bg() {
  170. if (this.backgroundColor === '' || this.backgroundColor === 'none') {
  171. return 'transparent'
  172. }
  173. return this.backgroundColor
  174. }
  175. },
  176. mounted() {
  177. const fixSize = () => {
  178. const {
  179. windowWidth,
  180. windowHeight,
  181. windowTop,
  182. safeArea,
  183. screenHeight,
  184. safeAreaInsets
  185. } = uni.getSystemInfoSync()
  186. this.popupWidth = windowWidth
  187. this.popupHeight = windowHeight + (windowTop || 0)
  188. // TODO fix by mehaotian 是否适配底部安全区 ,目前微信ios 、和 app ios 计算有差异,需要框架修复
  189. if (safeArea && this.safeArea) {
  190. // #ifdef MP-WEIXIN
  191. this.safeAreaInsets = screenHeight - safeArea.bottom
  192. // #endif
  193. // #ifndef MP-WEIXIN
  194. this.safeAreaInsets = safeAreaInsets.bottom
  195. // #endif
  196. } else {
  197. this.safeAreaInsets = 0
  198. }
  199. }
  200. fixSize()
  201. // #ifdef H5
  202. // window.addEventListener('resize', fixSize)
  203. // this.$once('hook:beforeDestroy', () => {
  204. // window.removeEventListener('resize', fixSize)
  205. // })
  206. // #endif
  207. },
  208. // #ifndef VUE3
  209. // TODO vue2
  210. destroyed() {
  211. this.setH5Visible()
  212. },
  213. // #endif
  214. // #ifdef VUE3
  215. // TODO vue3
  216. unmounted() {
  217. this.setH5Visible()
  218. },
  219. // #endif
  220. created() {
  221. // this.mkclick = this.isMaskClick || this.maskClick
  222. if (this.isMaskClick === null && this.maskClick === null) {
  223. this.mkclick = true
  224. } else {
  225. this.mkclick = this.isMaskClick !== null ? this.isMaskClick : this.maskClick
  226. }
  227. if (this.animation) {
  228. this.duration = 300
  229. } else {
  230. this.duration = 0
  231. }
  232. // TODO 处理 message 组件生命周期异常的问题
  233. this.messageChild = null
  234. // TODO 解决头条冒泡的问题
  235. this.clearPropagation = false
  236. this.maskClass.backgroundColor = this.maskBackgroundColor
  237. },
  238. methods: {
  239. setH5Visible() {
  240. // #ifdef H5
  241. // fix by mehaotian 处理 h5 滚动穿透的问题
  242. document.getElementsByTagName('body')[0].style.overflow = 'visible'
  243. // #endif
  244. },
  245. /**
  246. * 公用方法,不显示遮罩层
  247. */
  248. closeMask() {
  249. this.maskShow = false
  250. },
  251. /**
  252. * 公用方法,遮罩层禁止点击
  253. */
  254. disableMask() {
  255. this.mkclick = false
  256. },
  257. // TODO nvue 取消冒泡
  258. clear(e) {
  259. // #ifndef APP-NVUE
  260. e.stopPropagation()
  261. // #endif
  262. this.clearPropagation = true
  263. },
  264. open(direction) {
  265. // fix by mehaotian 处理快速打开关闭的情况
  266. if (this.showPopup) {
  267. return
  268. }
  269. let innerType = ['top', 'center', 'bottom', 'left', 'right', 'message', 'dialog', 'share']
  270. if (!(direction && innerType.indexOf(direction) !== -1)) {
  271. direction = this.type
  272. }
  273. if (!this.config[direction]) {
  274. console.error('缺少类型:', direction)
  275. return
  276. }
  277. this[this.config[direction]]()
  278. this.$emit('change', {
  279. show: true,
  280. type: direction
  281. })
  282. },
  283. close(type) {
  284. this.showTrans = false
  285. this.$emit('change', {
  286. show: false,
  287. type: this.type
  288. })
  289. clearTimeout(this.timer)
  290. // // 自定义关闭事件
  291. // this.customOpen && this.customClose()
  292. this.timer = setTimeout(() => {
  293. this.showPopup = false
  294. }, 300)
  295. },
  296. // TODO 处理冒泡事件,头条的冒泡事件有问题 ,先这样兼容
  297. touchstart() {
  298. this.clearPropagation = false
  299. },
  300. onTap() {
  301. if (this.clearPropagation) {
  302. // fix by mehaotian 兼容 nvue
  303. this.clearPropagation = false
  304. return
  305. }
  306. this.$emit('maskClick')
  307. if (!this.mkclick) return
  308. this.close()
  309. },
  310. /**
  311. * 顶部弹出样式处理
  312. */
  313. top(type) {
  314. this.popupstyle = this.isDesktop ? 'fixforpc-top' : 'top'
  315. this.ani = ['slide-top']
  316. this.transClass = {
  317. position: 'fixed',
  318. left: 0,
  319. right: 0,
  320. backgroundColor: this.bg
  321. }
  322. // TODO 兼容 type 属性 ,后续会废弃
  323. if (type) return
  324. this.showPopup = true
  325. this.showTrans = true
  326. this.$nextTick(() => {
  327. if (this.messageChild && this.type === 'message') {
  328. this.messageChild.timerClose()
  329. }
  330. })
  331. },
  332. /**
  333. * 底部弹出样式处理
  334. */
  335. bottom(type) {
  336. this.popupstyle = 'bottom'
  337. this.ani = ['slide-bottom']
  338. this.transClass = {
  339. position: 'fixed',
  340. left: 0,
  341. right: 0,
  342. bottom: 0,
  343. paddingBottom: this.safeAreaInsets + 'px',
  344. backgroundColor: this.bg
  345. }
  346. // TODO 兼容 type 属性 ,后续会废弃
  347. if (type) return
  348. this.showPopup = true
  349. this.showTrans = true
  350. },
  351. /**
  352. * 中间弹出样式处理
  353. */
  354. center(type) {
  355. this.popupstyle = 'center'
  356. //微信小程序下,组合动画会出现文字向上闪动问题,再此做特殊处理
  357. // #ifdef MP-WEIXIN
  358. this.ani = ['fade']
  359. // #endif
  360. // #ifndef MP-WEIXIN
  361. this.ani = ['zoom-out', 'fade']
  362. // #endif
  363. this.transClass = {
  364. position: 'fixed',
  365. /* #ifndef APP-NVUE */
  366. display: 'flex',
  367. flexDirection: 'column',
  368. /* #endif */
  369. bottom: 0,
  370. left: 0,
  371. right: 0,
  372. top: 0,
  373. justifyContent: 'center',
  374. alignItems: 'center'
  375. }
  376. // TODO 兼容 type 属性 ,后续会废弃
  377. if (type) return
  378. this.showPopup = true
  379. this.showTrans = true
  380. },
  381. left(type) {
  382. this.popupstyle = 'left'
  383. this.ani = ['slide-left']
  384. this.transClass = {
  385. position: 'fixed',
  386. left: 0,
  387. bottom: 0,
  388. top: 0,
  389. backgroundColor: this.bg,
  390. /* #ifndef APP-NVUE */
  391. display: 'flex',
  392. flexDirection: 'column'
  393. /* #endif */
  394. }
  395. // TODO 兼容 type 属性 ,后续会废弃
  396. if (type) return
  397. this.showPopup = true
  398. this.showTrans = true
  399. },
  400. right(type) {
  401. this.popupstyle = 'right'
  402. this.ani = ['slide-right']
  403. this.transClass = {
  404. position: 'fixed',
  405. bottom: 0,
  406. right: 0,
  407. top: 0,
  408. backgroundColor: this.bg,
  409. /* #ifndef APP-NVUE */
  410. display: 'flex',
  411. flexDirection: 'column'
  412. /* #endif */
  413. }
  414. // TODO 兼容 type 属性 ,后续会废弃
  415. if (type) return
  416. this.showPopup = true
  417. this.showTrans = true
  418. }
  419. }
  420. }
  421. </script>
  422. <style lang="scss">
  423. .uni-popup {
  424. position: fixed;
  425. /* #ifndef APP-NVUE */
  426. z-index: 99;
  427. /* #endif */
  428. &.top,
  429. &.left,
  430. &.right {
  431. /* #ifdef H5 */
  432. top: var(--window-top);
  433. /* #endif */
  434. /* #ifndef H5 */
  435. top: 0;
  436. /* #endif */
  437. }
  438. .uni-popup__wrapper {
  439. /* #ifndef APP-NVUE */
  440. display: block;
  441. /* #endif */
  442. position: relative;
  443. /* iphonex 等安全区设置,底部安全区适配 */
  444. /* #ifndef APP-NVUE */
  445. // padding-bottom: constant(safe-area-inset-bottom);
  446. // padding-bottom: env(safe-area-inset-bottom);
  447. /* #endif */
  448. &.left,
  449. &.right {
  450. /* #ifdef H5 */
  451. padding-top: var(--window-top);
  452. /* #endif */
  453. /* #ifndef H5 */
  454. padding-top: 0;
  455. /* #endif */
  456. flex: 1;
  457. }
  458. }
  459. }
  460. .fixforpc-z-index {
  461. /* #ifndef APP-NVUE */
  462. z-index: 999;
  463. /* #endif */
  464. }
  465. .fixforpc-top {
  466. top: 0;
  467. }
  468. </style>