uni-popup.vue 9.2 KB

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