u-popup.vue 8.6 KB

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