uni-popup.vue 10 KB

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