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