uni-popup.vue 8.8 KB

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