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