u-popup.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 && pageInline == false"
  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. class="u-popup__content—transition"
  22. :style="contentStyleWrap"
  23. :show="pageInline ? true : show"
  24. :customStyle="transitionStyle"
  25. :mode="pageInline ? 'none' : position"
  26. :duration="duration"
  27. @afterEnter="afterEnter"
  28. @click="clickHandler"
  29. >
  30. <!-- @click.stop不能去除,去除会导致居中模式下点击内容区域触发关闭弹窗 -->
  31. <view
  32. class="u-popup__content"
  33. :style="[contentStyle]"
  34. @click.stop="noop"
  35. @touchmove.stop.prevent="noop"
  36. >
  37. <u-status-bar v-if="safeAreaInsetTop"></u-status-bar>
  38. <!-- 增加触摸区域,用于处理手势 -->
  39. <view
  40. v-if="touchable && mode === 'bottom'"
  41. class="u-popup__content__touch-area"
  42. @touchstart="onTouchStart"
  43. @touchmove="onTouchMove"
  44. @touchend="onTouchEnd"
  45. @touchcancel="onTouchEnd"
  46. >
  47. <!-- iOS风格的横条指示器 -->
  48. <view class="u-popup__content__indicator"></view>
  49. </view>
  50. <slot></slot>
  51. <view
  52. v-if="closeable"
  53. @tap.stop="close"
  54. class="u-popup__content__close"
  55. :class="['u-popup__content__close--' + closeIconPos]"
  56. hover-class="u-popup__content__close--hover"
  57. hover-stay-time="150"
  58. >
  59. <up-icon
  60. name="close"
  61. color="#909399"
  62. size="18"
  63. bold
  64. ></up-icon>
  65. </view>
  66. <u-safe-bottom v-if="safeAreaInsetBottom"></u-safe-bottom>
  67. </view>
  68. <slot name="bottom"></slot>
  69. </u-transition>
  70. </view>
  71. </template>
  72. <script>
  73. import { props } from './props';
  74. import { mpMixin } from '../../libs/mixin/mpMixin';
  75. import { mixin } from '../../libs/mixin/mixin';
  76. import { addUnit, addStyle, deepMerge, sleep, getWindowInfo } from '../../libs/function/index';
  77. /**
  78. * popup 弹窗
  79. * @description 弹出层容器,用于展示弹窗、信息提示等内容,支持上、下、左、右和中部弹出。组件只提供容器,内部内容由用户自定义
  80. * @tutorial https://ijry.github.io/uview-plus/components/popup.html
  81. * @property {Boolean} show 是否展示弹窗 (默认 false )
  82. * @property {Boolean} overlay 是否显示遮罩 (默认 true )
  83. * @property {String} mode 弹出方向(默认 'bottom' )
  84. * @property {String | Number} duration 动画时长,单位ms (默认 300 )
  85. * @property {String | Number} overlayDuration 遮罩层动画时长,单位ms (默认 350 )
  86. * @property {Boolean} closeable 是否显示关闭图标(默认 false )
  87. * @property {Object | String} overlayStyle 自定义遮罩的样式
  88. * @property {String | Number} overlayOpacity 遮罩透明度,0-1之间(默认 0.5)
  89. * @property {Boolean} closeOnClickOverlay 点击遮罩是否关闭弹窗 (默认 true )
  90. * @property {String | Number} zIndex 层级 (默认 10075 )
  91. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离 (默认 true )
  92. * @property {Boolean} safeAreaInsetTop 是否留出顶部安全距离(状态栏高度) (默认 false )
  93. * @property {String} closeIconPos 自定义关闭图标位置(默认 'top-right' )
  94. * @property {String | Number} round 圆角值(默认 20px)
  95. * @property {String } bgColor 背景色值(默认 '' )
  96. * @property {Boolean} zoom 当mode=center时 是否开启缩放(默认 true )
  97. * @property {Boolean} touchable 是否开启底部弹窗手势功能(默认 false )
  98. * @property {String} minHeight 最小高度,单位任意,数值默认为px(默认 '200px' )
  99. * @property {String} maxHeight 最大高度,单位任意,数值默认为px(默认 '80%' )
  100. * @property {Object} customStyle 组件的样式,对象形式
  101. * @event {Function} open 弹出层打开
  102. * @event {Function} close 弹出层收起
  103. * @example <u-popup v-model:show="show"><text>出淤泥而不染,濯清涟而不妖</text></u-popup>
  104. */
  105. export default {
  106. name: 'u-popup',
  107. mixins: [mpMixin, mixin, props],
  108. data() {
  109. return {
  110. overlayDuration: this.duration + 50,
  111. // 触摸相关数据
  112. touchStartY: 0,
  113. touchStartHeight: 0,
  114. isTouching: false,
  115. // 当前弹窗高度
  116. currentHeight: 'auto'
  117. }
  118. },
  119. watch: {
  120. show(newValue, oldValue) {
  121. if (newValue === true) {
  122. // #ifdef MP-WEIXIN
  123. const children = this.$children
  124. this.retryComputedComponentRect(children)
  125. // #endif
  126. }
  127. }
  128. },
  129. computed: {
  130. transitionStyle() {
  131. const style = {
  132. display: 'flex',
  133. }
  134. if (!this.pageInline) {
  135. style.zIndex = this.zIndex
  136. style.position = 'fixed'
  137. }
  138. style[this.mode] = 0
  139. if (this.mode === 'left') {
  140. return deepMerge(style, {
  141. bottom: 0,
  142. top: 0,
  143. })
  144. } else if (this.mode === 'right') {
  145. return deepMerge(style, {
  146. bottom: 0,
  147. top: 0,
  148. })
  149. } else if (this.mode === 'top') {
  150. return deepMerge(style, {
  151. left: 0,
  152. right: 0
  153. })
  154. } else if (this.mode === 'bottom') {
  155. return deepMerge(style, {
  156. left: 0,
  157. right: 0,
  158. })
  159. } else if (this.mode === 'center') {
  160. return deepMerge(style, {
  161. alignItems: 'center',
  162. 'justify-content': 'center',
  163. top: 0,
  164. left: 0,
  165. right: 0,
  166. bottom: 0
  167. })
  168. }
  169. },
  170. contentStyleWrap() {
  171. const style = {}
  172. // 处理手势滑动时的高度变化
  173. if (this.mode === 'bottom' && this.touchable) {
  174. if (this.currentHeight !== 'auto') {
  175. style.height = this.currentHeight
  176. }
  177. if (this.maxHeight) {
  178. style.maxHeight = addUnit(this.maxHeight)
  179. }
  180. if (this.minHeight) {
  181. style.minHeight = addUnit(this.minHeight)
  182. }
  183. }
  184. return style;
  185. },
  186. contentStyle() {
  187. const style = {}
  188. // 通过设备信息的safeAreaInsets值来判断是否需要预留顶部状态栏和底部安全局的位置
  189. // 不使用css方案,是因为nvue不支持css的iPhoneX安全区查询属性
  190. const {
  191. safeAreaInsets
  192. } = getWindowInfo()
  193. if (this.mode !== 'center') {
  194. style.flex = 1
  195. }
  196. // 背景色,一般用于设置为transparent,去除默认的白色背景
  197. if (this.bgColor) {
  198. style.backgroundColor = this.bgColor
  199. }
  200. if(this.round) {
  201. const value = addUnit(this.round)
  202. if(this.mode === 'top') {
  203. style.borderBottomLeftRadius = value
  204. style.borderBottomRightRadius = value
  205. } else if(this.mode === 'bottom') {
  206. style.borderTopLeftRadius = value
  207. style.borderTopRightRadius = value
  208. } else if(this.mode === 'center') {
  209. style.borderRadius = value
  210. }
  211. }
  212. return deepMerge(style, addStyle(this.customStyle))
  213. },
  214. position() {
  215. if (this.mode === 'center') {
  216. return this.zoom ? 'fade-zoom' : 'fade'
  217. }
  218. if (this.mode === 'left') {
  219. return 'slide-left'
  220. }
  221. if (this.mode === 'right') {
  222. return 'slide-right'
  223. }
  224. if (this.mode === 'bottom') {
  225. return 'slide-up'
  226. }
  227. if (this.mode === 'top') {
  228. return 'slide-down'
  229. }
  230. },
  231. },
  232. emits: ["open", "close", "click", "update:show"],
  233. methods: {
  234. // 点击遮罩
  235. overlayClick() {
  236. if (this.closeOnClickOverlay) {
  237. this.$emit('update:show', false)
  238. this.$emit('close')
  239. }
  240. },
  241. open(e) {
  242. this.$emit('update:show', true)
  243. },
  244. close(e) {
  245. this.$emit('update:show', false)
  246. this.$emit('close')
  247. },
  248. afterEnter() {
  249. this.$emit('open')
  250. },
  251. clickHandler() {
  252. // 由于中部弹出时,其u-transition占据了整个页面相当于遮罩,此时需要发出遮罩点击事件,是否无法通过点击遮罩关闭弹窗
  253. if(this.mode === 'center') {
  254. this.overlayClick()
  255. }
  256. this.$emit('click')
  257. },
  258. // #ifdef MP-WEIXIN
  259. retryComputedComponentRect(children) {
  260. // 组件内部需要计算节点的组件
  261. const names = ['u-calendar-month', 'u-album', 'u-collapse-item', 'u-dropdown', 'u-index-item', 'u-index-list',
  262. 'u-line-progress', 'u-list-item', 'u-rate', 'u-read-more', 'u-row', 'u-row-notice', 'u-scroll-list',
  263. 'u-skeleton', 'u-slider', 'u-steps-item', 'u-sticky', 'u-subsection', 'u-swipe-action-item', 'u-tabbar',
  264. 'u-tabs', 'u-tooltip'
  265. ]
  266. // 历遍所有的子组件节点
  267. for (let i = 0; i < children.length; i++) {
  268. const child = children[i]
  269. // 拿到子组件的子组件
  270. const grandChild = child.$children
  271. // 判断如果在需要重新初始化的组件数组中名中,并且存在init方法的话,则执行
  272. if (names.includes(child.$options.name) && typeof child?.init === 'function') {
  273. // 需要进行一定的延时,因为初始化页面需要时间
  274. sleep(50).then(() => {
  275. child.init()
  276. })
  277. }
  278. // 如果子组件还有孙组件,进行递归历遍
  279. if (grandChild.length) {
  280. this.retryComputedComponentRect(grandChild)
  281. }
  282. }
  283. },
  284. // #endif
  285. // 触摸开始
  286. onTouchStart(e) {
  287. if (!this.touchable || this.mode !== 'bottom') return;
  288. this.isTouching = true;
  289. this.touchStartY = e.touches[0].clientY;
  290. // 保存当前高度
  291. this.touchStartHeight = this.$el.querySelector('.u-popup__content—transition').offsetHeight;
  292. },
  293. // 触摸移动
  294. onTouchMove(e) {
  295. if (!this.isTouching || !this.touchable || this.mode !== 'bottom') return;
  296. const touchY = e.touches[0].clientY;
  297. const deltaY = touchY - this.touchStartY;
  298. // 只处理向上滑动(减小高度)和向下滑动(增加高度)
  299. if (deltaY !== 0) {
  300. const newHeight = this.touchStartHeight - deltaY;
  301. const minHeight = parseFloat(addUnit(this.minHeight)) || 200;
  302. const maxHeight = this.maxHeight ?
  303. (this.maxHeight.toString().includes('%') ?
  304. getWindowInfo().windowHeight * (parseFloat(this.maxHeight) / 100) :
  305. parseFloat(addUnit(this.maxHeight))) :
  306. getWindowInfo().windowHeight * 0.8;
  307. // 限制高度在最小值和最大值之间
  308. if (newHeight >= minHeight && newHeight <= maxHeight) {
  309. this.currentHeight = newHeight + 'px';
  310. }
  311. }
  312. // 阻止默认滚动行为
  313. e.preventDefault();
  314. },
  315. // 触摸结束
  316. onTouchEnd(e) {
  317. if (!this.isTouching || !this.touchable || this.mode !== 'bottom') return;
  318. this.isTouching = false;
  319. const touchY = e.changedTouches[0].clientY;
  320. const deltaY = touchY - this.touchStartY;
  321. const velocity = Math.abs(deltaY) / (e.timeStamp - e.changedTouches[0].timestamp); // 简单的速度计算
  322. // 快速向下滑动时关闭弹窗
  323. if (deltaY > 100 || (deltaY > 30 && velocity > 0.5)) {
  324. this.close();
  325. } else {
  326. // 恢复到自适应高度
  327. // this.currentHeight = 'auto';
  328. }
  329. }
  330. }
  331. }
  332. </script>
  333. <style lang="scss" scoped>
  334. $u-popup-flex:1 !default;
  335. $u-popup-content-background-color: #fff !default;
  336. .u-popup {
  337. flex: $u-popup-flex;
  338. &__trigger {
  339. position: relative;
  340. &__cover {
  341. position: absolute;
  342. top: 0;
  343. left: 0;
  344. right: 0;
  345. bottom: 0;
  346. }
  347. }
  348. &__content {
  349. background-color: $u-popup-content-background-color;
  350. position: relative;
  351. &--round-top {
  352. border-top-left-radius: 0;
  353. border-top-right-radius: 0;
  354. border-bottom-left-radius: 10px;
  355. border-bottom-right-radius: 10px;
  356. }
  357. &--round-left {
  358. border-top-left-radius: 0;
  359. border-top-right-radius: 10px;
  360. border-bottom-left-radius: 0;
  361. border-bottom-right-radius: 10px;
  362. }
  363. &--round-right {
  364. border-top-left-radius: 10px;
  365. border-top-right-radius: 0;
  366. border-bottom-left-radius: 10px;
  367. border-bottom-right-radius: 0;
  368. }
  369. &--round-bottom {
  370. border-top-left-radius: 10px;
  371. border-top-right-radius: 10px;
  372. border-bottom-left-radius: 0;
  373. border-bottom-right-radius: 0;
  374. }
  375. &--round-center {
  376. border-top-left-radius: 10px;
  377. border-top-right-radius: 10px;
  378. border-bottom-left-radius: 10px;
  379. border-bottom-right-radius: 10px;
  380. }
  381. &__touch-area {
  382. // position: absolute;
  383. top: 0;
  384. left: 0;
  385. right: 0;
  386. height: 42rpx;
  387. z-index: 10;
  388. /* #ifndef APP-NVUE */
  389. display: flex;
  390. /* #endif */
  391. justify-content: center;
  392. align-items: center;
  393. }
  394. &__indicator {
  395. width: 100px;
  396. height: 5px;
  397. border-radius: 100px;
  398. background-color: #c0c4cc;
  399. }
  400. &__close {
  401. position: absolute;
  402. &--hover {
  403. opacity: 0.4;
  404. }
  405. }
  406. &__close--top-left {
  407. top: 15px;
  408. left: 15px;
  409. }
  410. &__close--top-right {
  411. top: 15px;
  412. right: 15px;
  413. }
  414. &__close--bottom-left {
  415. bottom: 15px;
  416. left: 15px;
  417. }
  418. &__close--bottom-right {
  419. right: 15px;
  420. bottom: 15px;
  421. }
  422. }
  423. }
  424. </style>