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