uni-popup.vue 11 KB

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