uni-popup.vue 12 KB

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