u-popup.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <view class="u-popup">
  3. <view v-if="$slots.trigger || $slots.$trigger" class="u-popup__trigger" @click="openPopup">
  4. <slot name="trigger"></slot>
  5. </view>
  6. <u-overlay
  7. :show="showPopup"
  8. @click="overlayClick"
  9. v-if="overlay"
  10. :duration="overlayDuration"
  11. :customStyle="mergeStyle"
  12. :opacity="overlayOpacity"
  13. ></u-overlay>
  14. <u-transition
  15. :show="showPopup"
  16. :customStyle="transitionStyle"
  17. :mode="position"
  18. :duration="duration"
  19. @afterEnter="afterEnter"
  20. @click="clickHandler"
  21. >
  22. <view
  23. class="u-popup__content"
  24. :style="[contentStyle]"
  25. @tap.stop="noop"
  26. @touchmove.stop.prevent="noop"
  27. >
  28. <u-status-bar v-if="safeAreaInsetTop"></u-status-bar>
  29. <view class="u-popup__content__header">
  30. <slot name="header">
  31. <text v-if="title" class="u-popup__content__title" :style="[titleStyle]">{{ title }}</text>
  32. </slot>
  33. </view>
  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="closeIcon"
  45. :color="closeIconColor"
  46. :size="closeIconSize"
  47. ></u-icon>
  48. </view>
  49. <u-safe-bottom v-if="safeAreaInsetBottom"></u-safe-bottom>
  50. </view>
  51. </u-transition>
  52. </view>
  53. </template>
  54. <script>
  55. import props from './props.js';
  56. import mixin from '../../libs/mixin/mixin'
  57. import mpMixin from '../../libs/mixin/mpMixin';
  58. /**
  59. * popup 弹窗
  60. * @description 弹出层容器,用于展示弹窗、信息提示等内容,支持上、下、左、右和中部弹出。组件只提供容器,内部内容由用户自定义
  61. * @tutorial https://uview.d3u.cn/components/popup.html
  62. * @property {Boolean} show 是否展示弹窗 (默认 false )
  63. * @property {String} title 弹窗标题
  64. * @property {Object | String} titleStyle 弹窗标题样式
  65. * @property {Boolean} overlay 是否显示遮罩 (默认 true )
  66. * @property {String} mode 弹出方向(默认 'bottom' )
  67. * @property {String | Number} duration 动画时长,单位ms (默认 300 )
  68. * @property {String | Number} overlayDuration 遮罩层动画时长,单位ms (默认 300 )
  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} closeIcon 自定义关闭图标(默认 'close' )
  77. * @property {String} closeIconPos 自定义关闭图标位置(默认 'top-right' )
  78. * @property {String} closeIconColor 自定义关闭图标颜色(默认 '#909399' )
  79. * @property {String | Number} closeIconSize 自定义关闭图标大小(默认 18)
  80. * @property {String | Number} round 圆角值(默认 0)
  81. * @property {String | Number} margin 外边距(默认 0)
  82. * @property {Boolean} zoom 当mode=center时 是否开启缩放(默认 true )
  83. * @property {Object} customStyle 组件的样式,对象形式
  84. * @property {String | Number} width 宽度
  85. * @property {String | Number} height 高度
  86. * @property {String} bgColor 背景色(默认 '#ffffff' )
  87. * @slot title 弹窗标题
  88. * @event {Function} open 弹出层打开
  89. * @event {Function} close 弹出层收起
  90. * @example <u-popup v-model="show"><text>出淤泥而不染,濯清涟而不妖</text></u-popup>
  91. */
  92. let popupZIndexSeed = 0;
  93. export default {
  94. name: 'u-popup',
  95. mixins: [mpMixin, mixin, props],
  96. data() {
  97. return {
  98. showPopup: false,
  99. isTrigger: false,
  100. overlayDuration: Number(this.duration) + 50,
  101. instanceZIndex: 0
  102. }
  103. },
  104. watch: {
  105. show: {
  106. immediate: true,
  107. handler(newValue) {
  108. // #ifdef MP-WEIXIN
  109. if (newValue === true) {
  110. const children = this.$children
  111. this.retryComputedComponentRect(children)
  112. }
  113. // #endif
  114. if (newValue === true) {
  115. popupZIndexSeed ++
  116. this.instanceZIndex = Number(this.zIndex) + popupZIndexSeed * 2
  117. }
  118. this.isTrigger = false
  119. this.showPopup = newValue
  120. }
  121. }
  122. },
  123. computed: {
  124. transitionStyle() {
  125. const style = {
  126. zIndex: this.instanceZIndex + 1,
  127. position: 'fixed',
  128. display: 'flex',
  129. }
  130. style[this.mode] = 0
  131. let margin = uni.$u.addUnit(this.margin || 0)
  132. if (this.mode === 'left') {
  133. return uni.$u.deepMerge(style, {
  134. bottom: 0,
  135. top: 0,
  136. })
  137. } else if (this.mode === 'right') {
  138. return uni.$u.deepMerge(style, {
  139. bottom: 0,
  140. top: 0,
  141. })
  142. } else if (this.mode === 'top') {
  143. let stylees = {
  144. left: margin,
  145. right: margin
  146. }
  147. if(this.width) {
  148. stylees.alignItems = 'center'
  149. }
  150. if(margin){
  151. stylees.top = margin
  152. }
  153. return uni.$u.deepMerge(style, stylees)
  154. } else if (this.mode === 'bottom') {
  155. let stylees = {
  156. left: margin,
  157. right: margin,
  158. }
  159. if(this.width) {
  160. stylees.alignItems = 'center'
  161. }
  162. if(margin){
  163. stylees.bottom = margin
  164. }
  165. return uni.$u.deepMerge(style, stylees)
  166. } else if (this.mode === 'center') {
  167. return uni.$u.deepMerge(style, {
  168. alignItems: 'center',
  169. justifyContent: 'center',
  170. top: 0,
  171. left: 0,
  172. right: 0,
  173. bottom: 0
  174. })
  175. }
  176. },
  177. mergeStyle() {
  178. return {
  179. zIndex: this.instanceZIndex,
  180. ...uni.$u.addStyle(this.customStyle),
  181. }
  182. },
  183. contentStyle() {
  184. const style = {}
  185. // 通过设备信息的safeAreaInsets值来判断是否需要预留顶部状态栏和底部安全局的位置
  186. // 不使用css方案,是因为nvue不支持css的iPhoneX安全区查询属性
  187. const {
  188. safeAreaInsets
  189. } = uni.$u.window()
  190. if (this.mode !== 'center') {
  191. style.flex = 1
  192. }
  193. // 背景色,一般用于设置为transparent,去除默认的白色背景
  194. if (this.bgColor) {
  195. style.backgroundColor = this.bgColor
  196. }
  197. if(this.width) {
  198. style.width = uni.$u.addUnit(this.width)
  199. }
  200. if(this.height && this.mode != 'left' && this.mode != 'right') {
  201. style.height = uni.$u.addUnit(this.height)
  202. }
  203. if(this.round) {
  204. const value = uni.$u.addUnit(this.round)
  205. if(this.mode === 'top') {
  206. style.borderBottomLeftRadius = value
  207. style.borderBottomRightRadius = value
  208. if(this.margin){
  209. style.borderTopLeftRadius = value
  210. style.borderTopRightRadius = value
  211. }
  212. } else if(this.mode === 'bottom') {
  213. style.borderTopLeftRadius = value
  214. style.borderTopRightRadius = value
  215. if(this.margin){
  216. style.borderBottomLeftRadius = value
  217. style.borderBottomRightRadius = value
  218. }
  219. } else if(this.mode === 'center') {
  220. style.borderRadius = value
  221. }
  222. }
  223. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  224. },
  225. position() {
  226. if (this.mode === 'center') {
  227. return this.zoom ? 'fade-zoom' : 'fade'
  228. }
  229. if (this.mode === 'left') {
  230. return 'slide-left'
  231. }
  232. if (this.mode === 'right') {
  233. return 'slide-right'
  234. }
  235. if (this.mode === 'bottom') {
  236. return 'slide-up'
  237. }
  238. if (this.mode === 'top') {
  239. return 'slide-down'
  240. }
  241. },
  242. },
  243. // #ifdef VUE3
  244. emits: ["click", "close", "open"],
  245. // #endif
  246. methods: {
  247. openPopup() {
  248. popupZIndexSeed ++
  249. this.instanceZIndex = Number(this.zIndex) + popupZIndexSeed * 2
  250. this.isTrigger = true
  251. this.showPopup = true
  252. },
  253. // 点击遮罩
  254. overlayClick() {
  255. if (this.closeOnClickOverlay) {
  256. if(this.isTrigger){
  257. this.showPopup = false
  258. }
  259. this.$emit('close')
  260. }
  261. },
  262. close(e) {
  263. if(this.isTrigger){
  264. this.showPopup = false
  265. }
  266. this.$emit('close')
  267. },
  268. afterEnter() {
  269. this.$emit('open')
  270. },
  271. clickHandler() {
  272. // 由于中部弹出时,其u-transition占据了整个页面相当于遮罩,此时需要发出遮罩点击事件,是否无法通过点击遮罩关闭弹窗
  273. if(this.mode === 'center') {
  274. this.overlayClick()
  275. }
  276. this.$emit('click')
  277. },
  278. // #ifdef MP-WEIXIN
  279. retryComputedComponentRect(children) {
  280. // 组件内部需要计算节点的组件
  281. const names = ['u-calendar-month', 'u-album', 'u-collapse-item', 'u-dropdown', 'u-index-item', 'u-index-list',
  282. 'u-line-progress', 'u-list-item', 'u-rate', 'u-read-more', 'u-row', 'u-row-notice', 'u-scroll-list',
  283. 'u-skeleton', 'u-slider', 'u-steps-item', 'u-sticky', 'u-subsection', 'u-swipe-action-item', 'u-tabbar',
  284. 'u-tabs', 'u-tooltip'
  285. ]
  286. // 历遍所有的子组件节点
  287. for (let i = 0; i < children.length; i++) {
  288. const child = children[i]
  289. // 拿到子组件的子组件
  290. const grandChild = child.$children
  291. // 判断如果在需要重新初始化的组件数组中名中,并且存在init方法的话,则执行
  292. if (names.includes(child.$options.name) && child.init && typeof child.init === 'function') {
  293. // 需要进行一定的延时,因为初始化页面需要时间
  294. uni.$u.sleep(50).then(() => {
  295. child.init()
  296. })
  297. }
  298. // 如果子组件还有孙组件,进行递归历遍
  299. if (grandChild.length) {
  300. this.retryComputedComponentRect(grandChild)
  301. }
  302. }
  303. }
  304. // #endif
  305. },
  306. // #ifdef VUE2
  307. beforeDestroy() {},
  308. // #endif
  309. // #ifdef VUE3
  310. beforeUnmount() {}
  311. // #endif
  312. }
  313. </script>
  314. <style lang="scss" scoped>
  315. @import "../../libs/css/components.scss";
  316. $u-popup-flex:1 !default;
  317. $u-popup-content-background-color: #fff !default;
  318. .u-popup {
  319. flex: $u-popup-flex;
  320. &__content {
  321. background-color: $u-popup-content-background-color;
  322. position: relative;
  323. overflow: hidden;
  324. &--round-top {
  325. border-top-left-radius: 0;
  326. border-top-right-radius: 0;
  327. border-bottom-left-radius: 10px;
  328. border-bottom-right-radius: 10px;
  329. }
  330. &--round-left {
  331. border-top-left-radius: 0;
  332. border-top-right-radius: 10px;
  333. border-bottom-left-radius: 0;
  334. border-bottom-right-radius: 10px;
  335. }
  336. &--round-right {
  337. border-top-left-radius: 10px;
  338. border-top-right-radius: 0;
  339. border-bottom-left-radius: 10px;
  340. border-bottom-right-radius: 0;
  341. }
  342. &--round-bottom {
  343. border-top-left-radius: 10px;
  344. border-top-right-radius: 10px;
  345. border-bottom-left-radius: 0;
  346. border-bottom-right-radius: 0;
  347. }
  348. &--round-center {
  349. border-top-left-radius: 10px;
  350. border-top-right-radius: 10px;
  351. border-bottom-left-radius: 10px;
  352. border-bottom-right-radius: 10px;
  353. }
  354. &__header {
  355. @include flex(row);
  356. justify-content: center;
  357. align-items: center;
  358. // #ifndef APP-NVUE
  359. box-sizing: border-box;
  360. flex-shrink: 0;
  361. // #endif
  362. }
  363. &__title {
  364. padding: 15px;
  365. flex: 1;
  366. text-align: center;
  367. font-size: 16px;
  368. }
  369. &__close {
  370. position: absolute;
  371. &--hover {
  372. opacity: 0.4;
  373. }
  374. }
  375. &__close--inset-top-left, &__close--top-left {
  376. top: 15px;
  377. left: 15px;
  378. }
  379. &__close--inset-top-right, &__close--top-right {
  380. top: 15px;
  381. right: 15px;
  382. }
  383. &__close--inset-top-center, &__close--top-center {
  384. top: 15px;
  385. left: 50%;
  386. transform: translateX(-50%);
  387. }
  388. &__close--inset-bottom-left, &__close--bottom-left {
  389. bottom: 15px;
  390. left: 15px;
  391. }
  392. &__close--inset-bottom-right, &__close--bottom-right {
  393. right: 15px;
  394. bottom: 15px;
  395. }
  396. &__close--inset-bottom-center, &__close--bottom-center {
  397. bottom: 15px;
  398. left: 50%;
  399. transform: translateX(-50%);
  400. }
  401. &__close--outset-top-left {
  402. top: -50px;
  403. left: -50px;
  404. }
  405. &__close--outset-top-right {
  406. top: -50px;
  407. right: -50px;
  408. }
  409. &__close--outset-top-center {
  410. top: -60px;
  411. left: 50%;
  412. transform: translateX(-50%);
  413. }
  414. &__close--outset-bottom-left{
  415. bottom: -50px;
  416. left: -50px;
  417. }
  418. &__close--outset-bottom-right {
  419. right: -50px;
  420. bottom: -50px;
  421. }
  422. &__close--outset-bottom-center {
  423. bottom: -60px;
  424. left: 50%;
  425. transform: translateX(-50%);
  426. }
  427. }
  428. }
  429. </style>