u-popup.vue 9.1 KB

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