uni-popup.vue 12 KB

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