u-popup.vue 8.5 KB

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