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.

308 lines
8.6 KiB

2 months ago
  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"><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"],
  180. methods: {
  181. // 点击遮罩
  182. overlayClick() {
  183. if (this.closeOnClickOverlay) {
  184. this.$emit('close')
  185. }
  186. },
  187. close(e) {
  188. this.$emit('close')
  189. },
  190. afterEnter() {
  191. this.$emit('open')
  192. },
  193. clickHandler() {
  194. // 由于中部弹出时,其u-transition占据了整个页面相当于遮罩,此时需要发出遮罩点击事件,是否无法通过点击遮罩关闭弹窗
  195. if(this.mode === 'center') {
  196. this.overlayClick()
  197. }
  198. this.$emit('click')
  199. },
  200. // #ifdef MP-WEIXIN
  201. retryComputedComponentRect(children) {
  202. // 组件内部需要计算节点的组件
  203. const names = ['u-calendar-month', 'u-album', 'u-collapse-item', 'u-dropdown', 'u-index-item', 'u-index-list',
  204. 'u-line-progress', 'u-list-item', 'u-rate', 'u-read-more', 'u-row', 'u-row-notice', 'u-scroll-list',
  205. 'u-skeleton', 'u-slider', 'u-steps-item', 'u-sticky', 'u-subsection', 'u-swipe-action-item', 'u-tabbar',
  206. 'u-tabs', 'u-tooltip'
  207. ]
  208. // 历遍所有的子组件节点
  209. for (let i = 0; i < children.length; i++) {
  210. const child = children[i]
  211. // 拿到子组件的子组件
  212. const grandChild = child.$children
  213. // 判断如果在需要重新初始化的组件数组中名中,并且存在init方法的话,则执行
  214. if (names.includes(child.$options.name) && typeof child?.init === 'function') {
  215. // 需要进行一定的延时,因为初始化页面需要时间
  216. sleep(50).then(() => {
  217. child.init()
  218. })
  219. }
  220. // 如果子组件还有孙组件,进行递归历遍
  221. if (grandChild.length) {
  222. this.retryComputedComponentRect(grandChild)
  223. }
  224. }
  225. }
  226. // #endif
  227. }
  228. }
  229. </script>
  230. <style lang="scss" scoped>
  231. @import "../../libs/css/components.scss";
  232. $u-popup-flex:1 !default;
  233. $u-popup-content-background-color: #fff !default;
  234. .u-popup {
  235. flex: $u-popup-flex;
  236. &__content {
  237. background-color: $u-popup-content-background-color;
  238. position: relative;
  239. &--round-top {
  240. border-top-left-radius: 0;
  241. border-top-right-radius: 0;
  242. border-bottom-left-radius: 10px;
  243. border-bottom-right-radius: 10px;
  244. }
  245. &--round-left {
  246. border-top-left-radius: 0;
  247. border-top-right-radius: 10px;
  248. border-bottom-left-radius: 0;
  249. border-bottom-right-radius: 10px;
  250. }
  251. &--round-right {
  252. border-top-left-radius: 10px;
  253. border-top-right-radius: 0;
  254. border-bottom-left-radius: 10px;
  255. border-bottom-right-radius: 0;
  256. }
  257. &--round-bottom {
  258. border-top-left-radius: 10px;
  259. border-top-right-radius: 10px;
  260. border-bottom-left-radius: 0;
  261. border-bottom-right-radius: 0;
  262. }
  263. &--round-center {
  264. border-top-left-radius: 10px;
  265. border-top-right-radius: 10px;
  266. border-bottom-left-radius: 10px;
  267. border-bottom-right-radius: 10px;
  268. }
  269. &__close {
  270. position: absolute;
  271. &--hover {
  272. opacity: 0.4;
  273. }
  274. }
  275. &__close--top-left {
  276. top: 15px;
  277. left: 15px;
  278. }
  279. &__close--top-right {
  280. top: 15px;
  281. right: 15px;
  282. }
  283. &__close--bottom-left {
  284. bottom: 15px;
  285. left: 15px;
  286. }
  287. &__close--bottom-right {
  288. right: 15px;
  289. bottom: 15px;
  290. }
  291. }
  292. }
  293. </style>