uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
8.7 KiB

3 years ago
  1. <template>
  2. <view class="u-popup">
  3. <u-overlay
  4. :show="show"
  5. @click="overlayClick"
  6. v-if="overlay"
  7. :duration="duration"
  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. @after-enter="afterEnter"
  17. @click="clickHandler"
  18. >
  19. <view
  20. class="u-popup__content"
  21. :style="[contentStyle]"
  22. @tap.stop="noop"
  23. :class="[round && `u-popup__content--round-${mode}`]"
  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.js';
  49. /**
  50. * popup 弹窗
  51. * @description 弹出层容器用于展示弹窗信息提示等内容支持上右和中部弹出组件只提供容器内部内容由用户自定义
  52. * @tutorial https://www.uviewui.com/components/popup.html
  53. * @property {Boolean} show 是否展示弹窗 (默认 false )
  54. * @property {Boolean} overlay 是否显示遮罩 默认 true
  55. * @property {String} mode 弹出方向默认 'bottom'
  56. * @property {String | Number} duration 动画时长单位ms 默认 300
  57. * @property {String | Number} borderRadius 弹窗圆角值默认 0
  58. * @property {Boolean} closeable 是否显示关闭图标默认 false
  59. * @property {Object | String} overlayStyle 自定义遮罩的样式
  60. * @property {String | Number} overlayOpacity 遮罩透明度0-1之间默认 0.5
  61. * @property {Boolean} closeOnClickOverlay 点击遮罩是否关闭弹窗 默认 true
  62. * @property {String | Number} zIndex 层级 默认 10075
  63. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离 默认 true
  64. * @property {Boolean} safeAreaInsetTop 是否留出顶部安全距离状态栏高度 默认 false
  65. * @property {String} closeIconPos 自定义关闭图标位置默认 'top-right'
  66. * @property {Boolean} round 是否显示圆角默认 false
  67. * @property {Boolean} zoom 当mode=center时 是否开启缩放默认 true
  68. * @property {Object} customStyle 组件的样式对象形式
  69. * @event {Function} open 弹出层打开
  70. * @event {Function} close 弹出层收起
  71. * @example <u-popup v-model="show"><text>出淤泥而不染濯清涟而不妖</text></u-popup>
  72. */
  73. export default {
  74. name: 'u-popup',
  75. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  76. data() {
  77. return {}
  78. },
  79. watch: {
  80. show(newValue, oldValue) {
  81. if (newValue === true) {
  82. // #ifdef MP-WEIXIN
  83. const children = this.$children
  84. this.retryComputedComponentRect(children)
  85. // #endif
  86. }
  87. }
  88. },
  89. computed: {
  90. transitionStyle() {
  91. const style = {
  92. zIndex: this.zIndex,
  93. position: 'fixed',
  94. display: 'flex',
  95. }
  96. style[this.mode] = 0
  97. if (this.mode === 'left') {
  98. return this.$u.deepMerge(style, {
  99. bottom: 0,
  100. top: 0,
  101. })
  102. } else if (this.mode === 'right') {
  103. return this.$u.deepMerge(style, {
  104. bottom: 0,
  105. top: 0,
  106. })
  107. } else if (this.mode === 'top') {
  108. return this.$u.deepMerge(style, {
  109. left: 0,
  110. right: 0
  111. })
  112. } else if (this.mode === 'bottom') {
  113. return this.$u.deepMerge(style, {
  114. left: 0,
  115. right: 0,
  116. })
  117. } else if (this.mode === 'center') {
  118. return this.$u.deepMerge(style, {
  119. alignItems: 'center',
  120. 'justify-content': 'center',
  121. top: 0,
  122. left: 0,
  123. right: 0,
  124. bottom: 0
  125. })
  126. }
  127. },
  128. contentStyle() {
  129. const style = {}
  130. // 通过设备信息的safeAreaInsets值来判断是否需要预留顶部状态栏和底部安全局的位置
  131. // 不使用css方案,是因为nvue不支持css的iPhoneX安全区查询属性
  132. const {
  133. safeAreaInsets
  134. } = uni.$u.sys()
  135. if (this.mode !== 'center') {
  136. style.flex = 1
  137. }
  138. // 背景色,一般用于设置为transparent,去除默认的白色背景
  139. if (this.bgColor) {
  140. style.backgroundColor = this.bgColor
  141. }
  142. // // 如果用户设置了borderRadius值,添加弹窗的圆角
  143. // if (this.round && this.mode === 'bottom' || this.mode === 'center') {
  144. // switch (this.mode) {
  145. // case 'left':
  146. // style.borderRadius = `0 15px 15px 0`;
  147. // break;
  148. // case 'top':
  149. // style.borderRadius = `0 0 15px 15px`;
  150. // break;
  151. // case 'right':
  152. // style.borderRadius = `15px 0 0 15px`;
  153. // break;
  154. // case 'bottom':
  155. // style.borderRadius = `15px 15px 0 0`;
  156. // break;
  157. // default:
  158. // }
  159. // // 不加可能圆角无效
  160. // style.overflow = 'hidden';
  161. // }
  162. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  163. },
  164. position() {
  165. if (this.mode === 'center') {
  166. return this.zoom ? 'fade-zoom' : 'fade'
  167. }
  168. if (this.mode === 'left') {
  169. return 'slide-left'
  170. }
  171. if (this.mode === 'right') {
  172. return 'slide-right'
  173. }
  174. if (this.mode === 'bottom') {
  175. return 'slide-up'
  176. }
  177. if (this.mode === 'top') {
  178. return 'slide-down'
  179. }
  180. },
  181. },
  182. methods: {
  183. // 点击遮罩
  184. overlayClick() {
  185. if (this.closeOnClickOverlay) {
  186. this.$emit('close')
  187. }
  188. },
  189. close(e) {
  190. this.$emit('close')
  191. },
  192. afterEnter() {
  193. this.$emit('open')
  194. },
  195. clickHandler() {
  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">
  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>