u-popup.vue 9.2 KB

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