u-popup.vue 8.3 KB

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