uni-popup.vue 12 KB

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