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