u-popup.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <view class="u-popup" :class="[customClass]"
  3. :style="{width: show == false ? '0px' : '',
  4. height: show == false ? '0px' : ''}">
  5. <view class="u-popup__trigger">
  6. <slot name="trigger">
  7. </slot>
  8. <view @click="open"
  9. class="u-popup__trigger__cover"></view>
  10. </view>
  11. <u-overlay
  12. :show="show"
  13. @click="overlayClick"
  14. v-if="overlay"
  15. :zIndex="zIndex"
  16. :duration="overlayDuration"
  17. :customStyle="overlayStyle"
  18. :opacity="overlayOpacity"
  19. ></u-overlay>
  20. <u-transition
  21. :show="show"
  22. :customStyle="transitionStyle"
  23. :mode="position"
  24. :duration="duration"
  25. @afterEnter="afterEnter"
  26. @click="clickHandler"
  27. >
  28. <!-- @click.stop不能去除,去除会导致居中模式下点击内容区域触发关闭弹窗 -->
  29. <view
  30. class="u-popup__content"
  31. :style="[contentStyle]"
  32. @click.stop="noop"
  33. @touchmove.stop.prevent="noop"
  34. >
  35. <u-status-bar v-if="safeAreaInsetTop"></u-status-bar>
  36. <slot></slot>
  37. <view
  38. v-if="closeable"
  39. @tap.stop="close"
  40. class="u-popup__content__close"
  41. :class="['u-popup__content__close--' + closeIconPos]"
  42. hover-class="u-popup__content__close--hover"
  43. hover-stay-time="150"
  44. >
  45. <u-icon
  46. name="close"
  47. color="#909399"
  48. size="18"
  49. bold
  50. ></u-icon>
  51. </view>
  52. <u-safe-bottom v-if="safeAreaInsetBottom"></u-safe-bottom>
  53. </view>
  54. <slot name="bottom"></slot>
  55. </u-transition>
  56. </view>
  57. </template>
  58. <script>
  59. import { props } from './props';
  60. import { mpMixin } from '../../libs/mixin/mpMixin';
  61. import { mixin } from '../../libs/mixin/mixin';
  62. import { addUnit, addStyle, deepMerge, sleep, getWindowInfo } from '../../libs/function/index';
  63. /**
  64. * popup 弹窗
  65. * @description 弹出层容器,用于展示弹窗、信息提示等内容,支持上、下、左、右和中部弹出。组件只提供容器,内部内容由用户自定义
  66. * @tutorial https://ijry.github.io/uview-plus/components/popup.html
  67. * @property {Boolean} show 是否展示弹窗 (默认 false )
  68. * @property {Boolean} overlay 是否显示遮罩 (默认 true )
  69. * @property {String} mode 弹出方向(默认 'bottom' )
  70. * @property {String | Number} duration 动画时长,单位ms (默认 300 )
  71. * @property {String | Number} overlayDuration 遮罩层动画时长,单位ms (默认 350 )
  72. * @property {Boolean} closeable 是否显示关闭图标(默认 false )
  73. * @property {Object | String} overlayStyle 自定义遮罩的样式
  74. * @property {String | Number} overlayOpacity 遮罩透明度,0-1之间(默认 0.5)
  75. * @property {Boolean} closeOnClickOverlay 点击遮罩是否关闭弹窗 (默认 true )
  76. * @property {String | Number} zIndex 层级 (默认 10075 )
  77. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离 (默认 true )
  78. * @property {Boolean} safeAreaInsetTop 是否留出顶部安全距离(状态栏高度) (默认 false )
  79. * @property {String} closeIconPos 自定义关闭图标位置(默认 'top-right' )
  80. * @property {String | Number} round 圆角值(默认 0)
  81. * @property {String } bgColor 背景色值(默认 '' )
  82. * @property {Boolean} zoom 当mode=center时 是否开启缩放(默认 true )
  83. * @property {Object} customStyle 组件的样式,对象形式
  84. * @event {Function} open 弹出层打开
  85. * @event {Function} close 弹出层收起
  86. * @example <u-popup v-model:show="show"><text>出淤泥而不染,濯清涟而不妖</text></u-popup>
  87. */
  88. export default {
  89. name: 'u-popup',
  90. mixins: [mpMixin, mixin, props],
  91. data() {
  92. return {
  93. overlayDuration: this.duration + 50
  94. }
  95. },
  96. watch: {
  97. show(newValue, oldValue) {
  98. if (newValue === true) {
  99. // #ifdef MP-WEIXIN
  100. const children = this.$children
  101. this.retryComputedComponentRect(children)
  102. // #endif
  103. }
  104. }
  105. },
  106. computed: {
  107. transitionStyle() {
  108. const style = {
  109. zIndex: this.zIndex,
  110. position: 'fixed',
  111. display: 'flex',
  112. }
  113. style[this.mode] = 0
  114. if (this.mode === 'left') {
  115. return deepMerge(style, {
  116. bottom: 0,
  117. top: 0,
  118. })
  119. } else if (this.mode === 'right') {
  120. return deepMerge(style, {
  121. bottom: 0,
  122. top: 0,
  123. })
  124. } else if (this.mode === 'top') {
  125. return deepMerge(style, {
  126. left: 0,
  127. right: 0
  128. })
  129. } else if (this.mode === 'bottom') {
  130. return deepMerge(style, {
  131. left: 0,
  132. right: 0,
  133. })
  134. } else if (this.mode === 'center') {
  135. return deepMerge(style, {
  136. alignItems: 'center',
  137. 'justify-content': 'center',
  138. top: 0,
  139. left: 0,
  140. right: 0,
  141. bottom: 0
  142. })
  143. }
  144. },
  145. contentStyle() {
  146. const style = {}
  147. // 通过设备信息的safeAreaInsets值来判断是否需要预留顶部状态栏和底部安全局的位置
  148. // 不使用css方案,是因为nvue不支持css的iPhoneX安全区查询属性
  149. const {
  150. safeAreaInsets
  151. } = getWindowInfo()
  152. if (this.mode !== 'center') {
  153. style.flex = 1
  154. }
  155. // 背景色,一般用于设置为transparent,去除默认的白色背景
  156. if (this.bgColor) {
  157. style.backgroundColor = this.bgColor
  158. }
  159. if(this.round) {
  160. const value = addUnit(this.round)
  161. if(this.mode === 'top') {
  162. style.borderBottomLeftRadius = value
  163. style.borderBottomRightRadius = value
  164. } else if(this.mode === 'bottom') {
  165. style.borderTopLeftRadius = value
  166. style.borderTopRightRadius = value
  167. } else if(this.mode === 'center') {
  168. style.borderRadius = value
  169. }
  170. }
  171. return deepMerge(style, addStyle(this.customStyle))
  172. },
  173. position() {
  174. if (this.mode === 'center') {
  175. return this.zoom ? 'fade-zoom' : 'fade'
  176. }
  177. if (this.mode === 'left') {
  178. return 'slide-left'
  179. }
  180. if (this.mode === 'right') {
  181. return 'slide-right'
  182. }
  183. if (this.mode === 'bottom') {
  184. return 'slide-up'
  185. }
  186. if (this.mode === 'top') {
  187. return 'slide-down'
  188. }
  189. },
  190. },
  191. emits: ["open", "close", "click", "update:show"],
  192. methods: {
  193. // 点击遮罩
  194. overlayClick() {
  195. if (this.closeOnClickOverlay) {
  196. this.$emit('update:show', false)
  197. this.$emit('close')
  198. }
  199. },
  200. open(e) {
  201. this.$emit('update:show', true)
  202. },
  203. close(e) {
  204. this.$emit('update:show', false)
  205. this.$emit('close')
  206. },
  207. afterEnter() {
  208. this.$emit('open')
  209. },
  210. clickHandler() {
  211. // 由于中部弹出时,其u-transition占据了整个页面相当于遮罩,此时需要发出遮罩点击事件,是否无法通过点击遮罩关闭弹窗
  212. if(this.mode === 'center') {
  213. this.overlayClick()
  214. }
  215. this.$emit('click')
  216. },
  217. // #ifdef MP-WEIXIN
  218. retryComputedComponentRect(children) {
  219. // 组件内部需要计算节点的组件
  220. const names = ['u-calendar-month', 'u-album', 'u-collapse-item', 'u-dropdown', 'u-index-item', 'u-index-list',
  221. 'u-line-progress', 'u-list-item', 'u-rate', 'u-read-more', 'u-row', 'u-row-notice', 'u-scroll-list',
  222. 'u-skeleton', 'u-slider', 'u-steps-item', 'u-sticky', 'u-subsection', 'u-swipe-action-item', 'u-tabbar',
  223. 'u-tabs', 'u-tooltip'
  224. ]
  225. // 历遍所有的子组件节点
  226. for (let i = 0; i < children.length; i++) {
  227. const child = children[i]
  228. // 拿到子组件的子组件
  229. const grandChild = child.$children
  230. // 判断如果在需要重新初始化的组件数组中名中,并且存在init方法的话,则执行
  231. if (names.includes(child.$options.name) && typeof child?.init === 'function') {
  232. // 需要进行一定的延时,因为初始化页面需要时间
  233. sleep(50).then(() => {
  234. child.init()
  235. })
  236. }
  237. // 如果子组件还有孙组件,进行递归历遍
  238. if (grandChild.length) {
  239. this.retryComputedComponentRect(grandChild)
  240. }
  241. }
  242. }
  243. // #endif
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>
  248. $u-popup-flex:1 !default;
  249. $u-popup-content-background-color: #fff !default;
  250. .u-popup {
  251. flex: $u-popup-flex;
  252. &__trigger {
  253. position: relative;
  254. &__cover {
  255. position: absolute;
  256. top: 0;
  257. left: 0;
  258. right: 0;
  259. bottom: 0;
  260. }
  261. }
  262. &__content {
  263. background-color: $u-popup-content-background-color;
  264. position: relative;
  265. &--round-top {
  266. border-top-left-radius: 0;
  267. border-top-right-radius: 0;
  268. border-bottom-left-radius: 10px;
  269. border-bottom-right-radius: 10px;
  270. }
  271. &--round-left {
  272. border-top-left-radius: 0;
  273. border-top-right-radius: 10px;
  274. border-bottom-left-radius: 0;
  275. border-bottom-right-radius: 10px;
  276. }
  277. &--round-right {
  278. border-top-left-radius: 10px;
  279. border-top-right-radius: 0;
  280. border-bottom-left-radius: 10px;
  281. border-bottom-right-radius: 0;
  282. }
  283. &--round-bottom {
  284. border-top-left-radius: 10px;
  285. border-top-right-radius: 10px;
  286. border-bottom-left-radius: 0;
  287. border-bottom-right-radius: 0;
  288. }
  289. &--round-center {
  290. border-top-left-radius: 10px;
  291. border-top-right-radius: 10px;
  292. border-bottom-left-radius: 10px;
  293. border-bottom-right-radius: 10px;
  294. }
  295. &__close {
  296. position: absolute;
  297. &--hover {
  298. opacity: 0.4;
  299. }
  300. }
  301. &__close--top-left {
  302. top: 15px;
  303. left: 15px;
  304. }
  305. &__close--top-right {
  306. top: 15px;
  307. right: 15px;
  308. }
  309. &__close--bottom-left {
  310. bottom: 15px;
  311. left: 15px;
  312. }
  313. &__close--bottom-right {
  314. right: 15px;
  315. bottom: 15px;
  316. }
  317. }
  318. }
  319. </style>