uni-popup.vue 9.8 KB

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