Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

457 rader
13 KiB

  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" :style="indicatorStyle"></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="closeIconColor"
  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://uview-plus.jiangruyi.com/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. // 背景色,留空时跟随主题卡片背景
  197. style.backgroundColor = this.bgColor || this.upThemeVar('--up-card-bg-color', this.upThemeIsDark ? '#1c1c1e' : '#ffffff')
  198. if(this.round) {
  199. const value = addUnit(this.round)
  200. if(this.mode === 'top') {
  201. style.borderBottomLeftRadius = value
  202. style.borderBottomRightRadius = value
  203. } else if(this.mode === 'bottom') {
  204. style.borderTopLeftRadius = value
  205. style.borderTopRightRadius = value
  206. } else if(this.mode === 'center') {
  207. style.borderRadius = value
  208. }
  209. }
  210. return deepMerge(style, addStyle(this.customStyle))
  211. },
  212. closeIconColor() {
  213. return this.upThemeVar('--up-content-color', '#606266')
  214. },
  215. indicatorStyle() {
  216. return {
  217. backgroundColor: this.upThemeVar('--up-light-color', '#c0c4cc')
  218. }
  219. },
  220. position() {
  221. if (this.mode === 'center') {
  222. return this.zoom ? 'fade-zoom' : 'fade'
  223. }
  224. if (this.mode === 'left') {
  225. return 'slide-left'
  226. }
  227. if (this.mode === 'right') {
  228. return 'slide-right'
  229. }
  230. if (this.mode === 'bottom') {
  231. return 'slide-up'
  232. }
  233. if (this.mode === 'top') {
  234. return 'slide-down'
  235. }
  236. },
  237. },
  238. emits: ["open", "close", "click", "update:show"],
  239. methods: {
  240. // 点击遮罩
  241. overlayClick() {
  242. if (this.closeOnClickOverlay) {
  243. this.$emit('update:show', false)
  244. this.$emit('close')
  245. }
  246. },
  247. open(e) {
  248. this.$emit('update:show', true)
  249. },
  250. close(e) {
  251. this.$emit('update:show', false)
  252. this.$emit('close')
  253. },
  254. afterEnter() {
  255. this.$emit('open')
  256. },
  257. clickHandler() {
  258. // 由于中部弹出时,其u-transition占据了整个页面相当于遮罩,此时需要发出遮罩点击事件,是否无法通过点击遮罩关闭弹窗
  259. if(this.mode === 'center') {
  260. this.overlayClick()
  261. }
  262. this.$emit('click')
  263. },
  264. // #ifdef MP-WEIXIN
  265. retryComputedComponentRect(children) {
  266. // 组件内部需要计算节点的组件
  267. const names = ['u-calendar-month', 'u-album', 'u-collapse-item', 'u-dropdown', 'u-index-item', 'u-index-list',
  268. 'u-line-progress', 'u-list-item', 'u-rate', 'u-read-more', 'u-row', 'u-row-notice', 'u-scroll-list',
  269. 'u-skeleton', 'u-slider', 'u-steps-item', 'u-sticky', 'u-subsection', 'u-swipe-action-item', 'u-tabbar',
  270. 'u-tabs', 'u-tooltip'
  271. ]
  272. // 历遍所有的子组件节点
  273. for (let i = 0; i < children.length; i++) {
  274. const child = children[i]
  275. // 拿到子组件的子组件
  276. const grandChild = child.$children
  277. // 判断如果在需要重新初始化的组件数组中名中,并且存在init方法的话,则执行
  278. if (names.includes(child.$options.name) && typeof child?.init === 'function') {
  279. // 需要进行一定的延时,因为初始化页面需要时间
  280. sleep(50).then(() => {
  281. child.init()
  282. })
  283. }
  284. // 如果子组件还有孙组件,进行递归历遍
  285. if (grandChild.length) {
  286. this.retryComputedComponentRect(grandChild)
  287. }
  288. }
  289. },
  290. // #endif
  291. // 触摸开始
  292. onTouchStart(e) {
  293. if (!this.touchable || this.mode !== 'bottom') return;
  294. this.isTouching = true;
  295. this.touchStartY = e.touches[0].clientY;
  296. // 保存当前高度
  297. this.touchStartHeight = this.$el.querySelector('.u-popup__content—transition').offsetHeight;
  298. },
  299. // 触摸移动
  300. onTouchMove(e) {
  301. if (!this.isTouching || !this.touchable || this.mode !== 'bottom') return;
  302. const touchY = e.touches[0].clientY;
  303. const deltaY = touchY - this.touchStartY;
  304. // 只处理向上滑动(减小高度)和向下滑动(增加高度)
  305. if (deltaY !== 0) {
  306. const newHeight = this.touchStartHeight - deltaY;
  307. const minHeight = parseFloat(addUnit(this.minHeight)) || 200;
  308. const maxHeight = this.maxHeight ?
  309. (this.maxHeight.toString().includes('%') ?
  310. getWindowInfo().windowHeight * (parseFloat(this.maxHeight) / 100) :
  311. parseFloat(addUnit(this.maxHeight))) :
  312. getWindowInfo().windowHeight * 0.8;
  313. // 限制高度在最小值和最大值之间
  314. if (newHeight >= minHeight && newHeight <= maxHeight) {
  315. this.currentHeight = newHeight + 'px';
  316. }
  317. }
  318. // 阻止默认滚动行为
  319. e.preventDefault();
  320. },
  321. // 触摸结束
  322. onTouchEnd(e) {
  323. if (!this.isTouching || !this.touchable || this.mode !== 'bottom') return;
  324. this.isTouching = false;
  325. const touchY = e.changedTouches[0].clientY;
  326. const deltaY = touchY - this.touchStartY;
  327. const velocity = Math.abs(deltaY) / (e.timeStamp - e.changedTouches[0].timestamp); // 简单的速度计算
  328. // 快速向下滑动时关闭弹窗
  329. if (deltaY > 100 || (deltaY > 30 && velocity > 0.5)) {
  330. this.close();
  331. } else {
  332. // 恢复到自适应高度
  333. // this.currentHeight = 'auto';
  334. }
  335. }
  336. }
  337. }
  338. </script>
  339. <style lang="scss" scoped>
  340. $u-popup-flex:1 !default;
  341. .u-popup {
  342. flex: $u-popup-flex;
  343. &__trigger {
  344. position: relative;
  345. &__cover {
  346. position: absolute;
  347. top: 0;
  348. left: 0;
  349. right: 0;
  350. bottom: 0;
  351. }
  352. }
  353. &__content {
  354. position: relative;
  355. &--round-top {
  356. border-top-left-radius: 0;
  357. border-top-right-radius: 0;
  358. border-bottom-left-radius: 10px;
  359. border-bottom-right-radius: 10px;
  360. }
  361. &--round-left {
  362. border-top-left-radius: 0;
  363. border-top-right-radius: 10px;
  364. border-bottom-left-radius: 0;
  365. border-bottom-right-radius: 10px;
  366. }
  367. &--round-right {
  368. border-top-left-radius: 10px;
  369. border-top-right-radius: 0;
  370. border-bottom-left-radius: 10px;
  371. border-bottom-right-radius: 0;
  372. }
  373. &--round-bottom {
  374. border-top-left-radius: 10px;
  375. border-top-right-radius: 10px;
  376. border-bottom-left-radius: 0;
  377. border-bottom-right-radius: 0;
  378. }
  379. &--round-center {
  380. border-top-left-radius: 10px;
  381. border-top-right-radius: 10px;
  382. border-bottom-left-radius: 10px;
  383. border-bottom-right-radius: 10px;
  384. }
  385. &__touch-area {
  386. // position: absolute;
  387. top: 0;
  388. left: 0;
  389. right: 0;
  390. height: 42rpx;
  391. z-index: 10;
  392. /* #ifndef APP-NVUE */
  393. display: flex;
  394. /* #endif */
  395. justify-content: center;
  396. align-items: center;
  397. }
  398. &__indicator {
  399. width: 100px;
  400. height: 5px;
  401. border-radius: 100px;
  402. background-color: var(--up-light-color, #c0c4cc);
  403. }
  404. &__close {
  405. position: absolute;
  406. &--hover {
  407. opacity: 0.4;
  408. }
  409. }
  410. &__close--top-left {
  411. top: 15px;
  412. left: 15px;
  413. }
  414. &__close--top-right {
  415. top: 15px;
  416. right: 15px;
  417. }
  418. &__close--bottom-left {
  419. bottom: 15px;
  420. left: 15px;
  421. }
  422. &__close--bottom-right {
  423. right: 15px;
  424. bottom: 15px;
  425. }
  426. }
  427. }
  428. </style>