uni-popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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" mode-class="fade" :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: this.isDesktop ? 'fixforpc-top' : '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. this.safeAreaInsets = 0
  196. // #endif
  197. } else {
  198. this.safeAreaInsets = 0
  199. }
  200. }
  201. fixSize()
  202. // #ifdef H5
  203. // window.addEventListener('resize', fixSize)
  204. // this.$once('hook:beforeDestroy', () => {
  205. // window.removeEventListener('resize', fixSize)
  206. // })
  207. // #endif
  208. },
  209. // #ifndef VUE3
  210. // TODO vue2
  211. destroyed() {
  212. this.setH5Visible()
  213. },
  214. // #endif
  215. // #ifdef VUE3
  216. // TODO vue3
  217. unmounted() {
  218. this.setH5Visible()
  219. },
  220. // #endif
  221. created() {
  222. // this.mkclick = this.isMaskClick || this.maskClick
  223. if (this.isMaskClick === null && this.maskClick === null) {
  224. this.mkclick = true
  225. } else {
  226. this.mkclick = this.isMaskClick !== null ? this.isMaskClick : this.maskClick
  227. }
  228. if (this.animation) {
  229. this.duration = 300
  230. } else {
  231. this.duration = 0
  232. }
  233. // TODO 处理 message 组件生命周期异常的问题
  234. this.messageChild = null
  235. // TODO 解决头条冒泡的问题
  236. this.clearPropagation = false
  237. this.maskClass.backgroundColor = this.maskBackgroundColor
  238. },
  239. methods: {
  240. setH5Visible() {
  241. // #ifdef H5
  242. // fix by mehaotian 处理 h5 滚动穿透的问题
  243. document.getElementsByTagName('body')[0].style.overflow = 'visible'
  244. // #endif
  245. },
  246. /**
  247. * 公用方法,不显示遮罩层
  248. */
  249. closeMask() {
  250. this.maskShow = false
  251. },
  252. /**
  253. * 公用方法,遮罩层禁止点击
  254. */
  255. disableMask() {
  256. this.mkclick = false
  257. },
  258. // TODO nvue 取消冒泡
  259. clear(e) {
  260. // #ifndef APP-NVUE
  261. e.stopPropagation()
  262. // #endif
  263. this.clearPropagation = true
  264. },
  265. open(direction) {
  266. // fix by mehaotian 处理快速打开关闭的情况
  267. if (this.showPopup) {
  268. clearTimeout(this.timer)
  269. this.showPopup = false
  270. }
  271. let innerType = ['top', 'center', 'bottom', 'left', 'right', 'message', 'dialog', 'share']
  272. if (!(direction && innerType.indexOf(direction) !== -1)) {
  273. direction = this.type
  274. }
  275. if (!this.config[direction]) {
  276. console.error('缺少类型:', direction)
  277. return
  278. }
  279. this[this.config[direction]]()
  280. this.$emit('change', {
  281. show: true,
  282. type: direction
  283. })
  284. },
  285. close(type) {
  286. this.showTrans = false
  287. this.$emit('change', {
  288. show: false,
  289. type: this.type
  290. })
  291. clearTimeout(this.timer)
  292. // // 自定义关闭事件
  293. // this.customOpen && this.customClose()
  294. this.timer = setTimeout(() => {
  295. this.showPopup = false
  296. }, 300)
  297. },
  298. // TODO 处理冒泡事件,头条的冒泡事件有问题 ,先这样兼容
  299. touchstart() {
  300. this.clearPropagation = false
  301. },
  302. onTap() {
  303. if (this.clearPropagation) {
  304. // fix by mehaotian 兼容 nvue
  305. this.clearPropagation = false
  306. return
  307. }
  308. this.$emit('maskClick')
  309. if (!this.mkclick) return
  310. this.close()
  311. },
  312. /**
  313. * 顶部弹出样式处理
  314. */
  315. top(type) {
  316. this.popupstyle = this.isDesktop ? 'fixforpc-top' : 'top'
  317. this.ani = ['slide-top']
  318. this.transClass = {
  319. position: 'fixed',
  320. left: 0,
  321. right: 0,
  322. backgroundColor: this.bg
  323. }
  324. // TODO 兼容 type 属性 ,后续会废弃
  325. if (type) return
  326. this.showPopup = true
  327. this.showTrans = true
  328. this.$nextTick(() => {
  329. if (this.messageChild && this.type === 'message') {
  330. this.messageChild.timerClose()
  331. }
  332. })
  333. },
  334. /**
  335. * 底部弹出样式处理
  336. */
  337. bottom(type) {
  338. this.popupstyle = 'bottom'
  339. this.ani = ['slide-bottom']
  340. this.transClass = {
  341. position: 'fixed',
  342. left: 0,
  343. right: 0,
  344. bottom: 0,
  345. paddingBottom: this.safeAreaInsets + 'px',
  346. backgroundColor: this.bg
  347. }
  348. // TODO 兼容 type 属性 ,后续会废弃
  349. if (type) return
  350. this.showPopup = true
  351. this.showTrans = true
  352. },
  353. /**
  354. * 中间弹出样式处理
  355. */
  356. center(type) {
  357. this.popupstyle = 'center'
  358. this.ani = ['zoom-out', 'fade']
  359. this.transClass = {
  360. position: 'fixed',
  361. /* #ifndef APP-NVUE */
  362. display: 'flex',
  363. flexDirection: 'column',
  364. /* #endif */
  365. bottom: 0,
  366. left: 0,
  367. right: 0,
  368. top: 0,
  369. justifyContent: 'center',
  370. alignItems: 'center'
  371. }
  372. // TODO 兼容 type 属性 ,后续会废弃
  373. if (type) return
  374. this.showPopup = true
  375. this.showTrans = true
  376. },
  377. left(type) {
  378. this.popupstyle = 'left'
  379. this.ani = ['slide-left']
  380. this.transClass = {
  381. position: 'fixed',
  382. left: 0,
  383. bottom: 0,
  384. top: 0,
  385. backgroundColor: this.bg,
  386. /* #ifndef APP-NVUE */
  387. display: 'flex',
  388. flexDirection: 'column'
  389. /* #endif */
  390. }
  391. // TODO 兼容 type 属性 ,后续会废弃
  392. if (type) return
  393. this.showPopup = true
  394. this.showTrans = true
  395. },
  396. right(type) {
  397. this.popupstyle = 'right'
  398. this.ani = ['slide-right']
  399. this.transClass = {
  400. position: 'fixed',
  401. bottom: 0,
  402. right: 0,
  403. top: 0,
  404. backgroundColor: this.bg,
  405. /* #ifndef APP-NVUE */
  406. display: 'flex',
  407. flexDirection: 'column'
  408. /* #endif */
  409. }
  410. // TODO 兼容 type 属性 ,后续会废弃
  411. if (type) return
  412. this.showPopup = true
  413. this.showTrans = true
  414. }
  415. }
  416. }
  417. </script>
  418. <style lang="scss">
  419. .uni-popup {
  420. position: fixed;
  421. /* #ifndef APP-NVUE */
  422. z-index: 99;
  423. /* #endif */
  424. &.top,
  425. &.left,
  426. &.right {
  427. /* #ifdef H5 */
  428. top: var(--window-top);
  429. /* #endif */
  430. /* #ifndef H5 */
  431. top: 0;
  432. /* #endif */
  433. }
  434. .uni-popup__wrapper {
  435. /* #ifndef APP-NVUE */
  436. display: block;
  437. /* #endif */
  438. position: relative;
  439. /* iphonex 等安全区设置,底部安全区适配 */
  440. /* #ifndef APP-NVUE */
  441. // padding-bottom: constant(safe-area-inset-bottom);
  442. // padding-bottom: env(safe-area-inset-bottom);
  443. /* #endif */
  444. &.left,
  445. &.right {
  446. /* #ifdef H5 */
  447. padding-top: var(--window-top);
  448. /* #endif */
  449. /* #ifndef H5 */
  450. padding-top: 0;
  451. /* #endif */
  452. flex: 1;
  453. }
  454. }
  455. }
  456. .fixforpc-z-index {
  457. /* #ifndef APP-NVUE */
  458. z-index: 999;
  459. /* #endif */
  460. }
  461. .fixforpc-top {
  462. top: 0;
  463. }
  464. </style>