You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

u-popup.vue 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <template>
  2. <view v-if="visibleSync" :style="[customStyle, {
  3. zIndex: uZindex - 1
  4. }]" class="u-drawer" hover-stop-propagation>
  5. <u-mask :blur="blur" :duration="duration" :custom-style="maskCustomStyle" :maskClickAble="maskCloseAble"
  6. :z-index="uZindex - 2" :show="showDrawer && mask" @click="maskClick"></u-mask>
  7. <!-- 移除 @tap.stop.prevent -->
  8. <view class="u-drawer-content" @tap="modeCenterClose(mode)" :class="[
  9. safeAreaInsetBottom ? 'safe-area-inset-bottom' : '',
  10. 'u-drawer-' + mode,
  11. showDrawer ? 'u-drawer-content-visible' : '',
  12. zoom && mode == 'center' ? 'u-animation-zoom' : ''
  13. ]" @touchmove.stop.prevent :style="[style]">
  14. <view class="u-mode-center-box" @tap.stop.prevent @touchmove.stop.prevent v-if="mode == 'center'"
  15. :style="[centerStyle]">
  16. <u-icon @click="close" v-if="closeable" class="u-close" :class="['u-close--' + closeIconPos]"
  17. :name="closeIcon" :color="closeIconColor" :size="closeIconSize"></u-icon>
  18. <scroll-view class="u-drawer__scroll-view" scroll-y="true">
  19. <slot></slot>
  20. </scroll-view>
  21. </view>
  22. <scroll-view class="u-drawer__scroll-view" scroll-y="true" v-else>
  23. <slot></slot>
  24. </scroll-view>
  25. <view @tap="close" class="u-close" :class="['u-close--' + closeIconPos]">
  26. <u-icon v-if="mode != 'center' && closeable" :name="closeIcon" :color="closeIconColor"
  27. :size="closeIconSize"></u-icon>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. /**
  34. * popup 弹窗
  35. * @description 弹出层容器,用于展示弹窗、信息提示等内容,支持上、下、左、右和中部弹出。组件只提供容器,内部内容由用户自定义
  36. * @tutorial https://www.uviewui.com/components/popup.html
  37. * @property {String} mode 弹出方向(默认left)
  38. * @value left 左侧弹出
  39. * @value top 顶部弹出
  40. * @value right 右侧弹出
  41. * @value bottom 底部弹出
  42. * @value center 中间弹出
  43. * @property {Boolean} mask 是否显示遮罩(默认true)
  44. * @property {Stringr | Number} length mode=left | 见官网说明(默认auto)
  45. * @property {Boolean} zoom 是否开启缩放动画,只在mode为center时有效(默认true)
  46. * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
  47. * @property {Boolean} mask-close-able 点击遮罩是否可以关闭弹出层(默认true)
  48. * @property {Object} custom-style 用户自定义样式
  49. * @property {Stringr | Number} negative-top 中部弹出时,往上偏移的值
  50. * @property {Numberr | String} border-radius 弹窗圆角值(默认0)
  51. * @property {Numberr | String} z-index 弹出内容的z-index值(默认1075)
  52. * @property {Boolean} closeable 是否显示关闭图标(默认false)
  53. * @property {String} close-icon 关闭图标的名称,只能uView的内置图标
  54. * @property {String} close-icon-pos 自定义关闭图标位置(默认top-right)
  55. * @property {String} close-icon-color 关闭图标的颜色(默认#909399)
  56. * @property {Number | String} close-icon-size 关闭图标的大小,单位rpx(默认30)
  57. * @event {Function} open 弹出层打开
  58. * @event {Function} close 弹出层收起
  59. * @example <u-popup v-model="show"><view>出淤泥而不染,濯清涟而不妖</view></u-popup>
  60. */
  61. export default {
  62. name: 'u-popup',
  63. emits: ["update:modelValue", "input", "open", "close"],
  64. props: {
  65. value: {
  66. type: Boolean,
  67. default: false
  68. },
  69. modelValue: {
  70. type: Boolean,
  71. default: false
  72. },
  73. /**
  74. * 显示状态
  75. */
  76. show: {
  77. type: Boolean,
  78. default: false
  79. },
  80. /**
  81. * 弹出方向,left|right|top|bottom|center
  82. */
  83. mode: {
  84. type: String,
  85. default: 'left'
  86. },
  87. /**
  88. * 是否显示遮罩
  89. */
  90. mask: {
  91. type: Boolean,
  92. default: true
  93. },
  94. // 抽屉的宽度(mode=left|right),或者高度(mode=top|bottom),单位rpx,或者"auto"
  95. // 或者百分比"50%",表示由内容撑开高度或者宽度
  96. length: {
  97. type: [Number, String],
  98. default: 'auto'
  99. },
  100. // 是否开启缩放动画,只在mode=center时有效
  101. zoom: {
  102. type: Boolean,
  103. default: true
  104. },
  105. // 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距
  106. safeAreaInsetBottom: {
  107. type: Boolean,
  108. default: false
  109. },
  110. // 是否可以通过点击遮罩进行关闭
  111. maskCloseAble: {
  112. type: Boolean,
  113. default: true
  114. },
  115. // 用户自定义样式
  116. customStyle: {
  117. type: Object,
  118. default () {
  119. return {};
  120. }
  121. },
  122. // 此为内部参数,不在文档对外使用,为了解决Picker和keyboard等融合了弹窗的组件
  123. // 对v-model双向绑定多层调用造成报错不能修改props值的问题
  124. popup: {
  125. type: Boolean,
  126. default: true
  127. },
  128. // 显示显示弹窗的圆角,单位rpx
  129. borderRadius: {
  130. type: [Number, String],
  131. default: 0
  132. },
  133. zIndex: {
  134. type: [Number, String],
  135. default: ''
  136. },
  137. // 是否显示关闭图标
  138. closeable: {
  139. type: Boolean,
  140. default: false
  141. },
  142. // 关闭图标的名称,只能uView的内置图标
  143. closeIcon: {
  144. type: String,
  145. default: 'close'
  146. },
  147. // 自定义关闭图标位置,top-left为左上角,top-right为右上角,bottom-left为左下角,bottom-right为右下角
  148. closeIconPos: {
  149. type: String,
  150. default: 'top-right'
  151. },
  152. // 关闭图标的颜色
  153. closeIconColor: {
  154. type: String,
  155. default: '#909399'
  156. },
  157. // 关闭图标的大小,单位rpx
  158. closeIconSize: {
  159. type: [String, Number],
  160. default: '30'
  161. },
  162. // 宽度,只对左,右,中部弹出时起作用,单位rpx,或者"auto"
  163. // 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数
  164. width: {
  165. type: String,
  166. default: ''
  167. },
  168. // 高度,只对上,下,中部弹出时起作用,单位rpx,或者"auto"
  169. // 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数
  170. height: {
  171. type: String,
  172. default: ''
  173. },
  174. // 给一个负的margin-top,往上偏移,避免和键盘重合的情况,仅在mode=center时有效
  175. negativeTop: {
  176. type: [String, Number],
  177. default: 0
  178. },
  179. // 遮罩的样式,一般用于修改遮罩的透明度
  180. maskCustomStyle: {
  181. type: Object,
  182. default () {
  183. return {}
  184. }
  185. },
  186. // 遮罩打开或收起的动画过渡时间,单位ms
  187. duration: {
  188. type: [String, Number],
  189. default: 250
  190. },
  191. // 遮罩的模糊度
  192. blur: {
  193. type: [String, Number],
  194. default: 0
  195. },
  196. },
  197. data() {
  198. return {
  199. visibleSync: false,
  200. showDrawer: false,
  201. timer: null,
  202. closeFromInner: false, // value的值改变,是发生在内部还是外部
  203. };
  204. },
  205. computed: {
  206. valueCom() {
  207. // #ifndef VUE3
  208. return this.value;
  209. // #endif
  210. // #ifdef VUE3
  211. return this.modelValue;
  212. // #endif
  213. },
  214. // 根据mode的位置,设定其弹窗的宽度(mode = left|right),或者高度(mode = top|bottom)
  215. style() {
  216. let style = {};
  217. // 如果是左边或者上边弹出时,需要给translate设置为负值,用于隐藏
  218. if (this.mode == 'left' || this.mode == 'right') {
  219. style = {
  220. width: this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length),
  221. height: '100%',
  222. transform: `translate3D(${this.mode == 'left' ? '-100%' : '100%'},0px,0px)`
  223. };
  224. } else if (this.mode == 'top' || this.mode == 'bottom') {
  225. style = {
  226. width: '100%',
  227. height: this.height ? this.getUnitValue(this.height) : this.getUnitValue(this.length),
  228. transform: `translate3D(0px,${this.mode == 'top' ? '-100%' : '100%'},0px)`
  229. };
  230. }
  231. style.zIndex = this.uZindex;
  232. // 如果用户设置了borderRadius值,添加弹窗的圆角
  233. if (this.borderRadius) {
  234. switch (this.mode) {
  235. case 'left':
  236. style.borderRadius = `0 ${this.borderRadius}rpx ${this.borderRadius}rpx 0`;
  237. break;
  238. case 'top':
  239. style.borderRadius = `0 0 ${this.borderRadius}rpx ${this.borderRadius}rpx`;
  240. break;
  241. case 'right':
  242. style.borderRadius = `${this.borderRadius}rpx 0 0 ${this.borderRadius}rpx`;
  243. break;
  244. case 'bottom':
  245. style.borderRadius = `${this.borderRadius}rpx ${this.borderRadius}rpx 0 0`;
  246. break;
  247. default:
  248. }
  249. // 不加可能圆角无效
  250. style.overflow = 'hidden';
  251. }
  252. if (this.duration) style.transition = `all ${this.duration / 1000}s linear`;
  253. return style;
  254. },
  255. // 中部弹窗的特有样式
  256. centerStyle() {
  257. let style = {};
  258. style.width = this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length);
  259. // 中部弹出的模式,如果没有设置高度,就用auto值,由内容撑开高度
  260. style.height = this.height ? this.getUnitValue(this.height) : 'auto';
  261. style.zIndex = this.uZindex;
  262. style.marginTop = `-${this.$u.addUnit(this.negativeTop)}`;
  263. if (this.borderRadius) {
  264. style.borderRadius = `${this.borderRadius}rpx`;
  265. // 不加可能圆角无效
  266. style.overflow = 'hidden';
  267. }
  268. return style;
  269. },
  270. // 计算整理后的z-index值
  271. uZindex() {
  272. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  273. }
  274. },
  275. watch: {
  276. valueCom: {
  277. immediate: true,
  278. handler(val) {
  279. if (val) {
  280. this.open();
  281. } else if (!this.closeFromInner) {
  282. this.close();
  283. }
  284. this.closeFromInner = false;
  285. }
  286. }
  287. },
  288. mounted() {
  289. // 组件渲染完成时,检查value是否为true,如果是,弹出popup
  290. this.valueCom && this.open();
  291. },
  292. methods: {
  293. // 判断传入的值,是否带有单位,如果没有,就默认用rpx单位
  294. getUnitValue(val) {
  295. if (/(%|px|rpx|auto)$/.test(val)) return val;
  296. else return val + 'rpx'
  297. },
  298. // 遮罩被点击
  299. maskClick() {
  300. this.close();
  301. },
  302. close() {
  303. // 标记关闭是内部发生的,否则修改了value值,导致watch中对value检测,导致再执行一遍close
  304. // 造成@close事件触发两次
  305. this.closeFromInner = true;
  306. this.change('showDrawer', 'visibleSync', false);
  307. },
  308. // 中部弹出时,需要.u-drawer-content将居中内容,此元素会铺满屏幕,点击需要关闭弹窗
  309. // 让其只在mode=center时起作用
  310. modeCenterClose(mode) {
  311. if (mode != 'center' || !this.maskCloseAble) return;
  312. this.close();
  313. },
  314. open() {
  315. this.change('visibleSync', 'showDrawer', true);
  316. },
  317. // 此处的原理是,关闭时先通过动画隐藏弹窗和遮罩,再移除整个组件
  318. // 打开时,先渲染组件,延时一定时间再让遮罩和弹窗的动画起作用
  319. change(param1, param2, status) {
  320. // 如果this.popup为false,意味着为picker,actionsheet等组件调用了popup组件
  321. if (this.popup == true) {
  322. this.$emit('input', status);
  323. this.$emit("update:modelValue", status);
  324. }
  325. this[param1] = status;
  326. if (status) {
  327. // #ifdef H5 || MP
  328. this.timer = setTimeout(() => {
  329. this[param2] = status;
  330. this.$emit(status ? 'open' : 'close');
  331. }, 50);
  332. // #endif
  333. // #ifndef H5 || MP
  334. this.$nextTick(() => {
  335. this[param2] = status;
  336. this.$emit(status ? 'open' : 'close');
  337. })
  338. // #endif
  339. } else {
  340. this.timer = setTimeout(() => {
  341. this[param2] = status;
  342. this.$emit(status ? 'open' : 'close');
  343. }, this.duration);
  344. }
  345. }
  346. }
  347. };
  348. </script>
  349. <style scoped lang="scss">
  350. @import "../../libs/css/style.components.scss";
  351. .u-drawer {
  352. /* #ifndef APP-NVUE */
  353. display: block;
  354. /* #endif */
  355. position: fixed;
  356. top: 0;
  357. left: 0;
  358. right: 0;
  359. bottom: 0;
  360. overflow: hidden;
  361. }
  362. .u-drawer-content {
  363. /* #ifndef APP-NVUE */
  364. display: block;
  365. /* #endif */
  366. position: absolute;
  367. z-index: 1003;
  368. transition: all 0.25s linear;
  369. }
  370. .u-drawer__scroll-view {
  371. width: 100%;
  372. height: 100%;
  373. }
  374. .u-drawer-left {
  375. top: 0;
  376. bottom: 0;
  377. left: 0;
  378. background-color: #ffffff;
  379. }
  380. .u-drawer-right {
  381. right: 0;
  382. top: 0;
  383. bottom: 0;
  384. background-color: #ffffff;
  385. }
  386. .u-drawer-top {
  387. top: 0;
  388. left: 0;
  389. right: 0;
  390. background-color: #ffffff;
  391. }
  392. .u-drawer-bottom {
  393. bottom: 0;
  394. left: 0;
  395. right: 0;
  396. background-color: #ffffff;
  397. }
  398. .u-drawer-center {
  399. @include vue-flex;
  400. flex-direction: column;
  401. bottom: 0;
  402. left: 0;
  403. right: 0;
  404. top: 0;
  405. justify-content: center;
  406. align-items: center;
  407. opacity: 0;
  408. z-index: 99999;
  409. }
  410. .u-mode-center-box {
  411. min-width: 100rpx;
  412. min-height: 100rpx;
  413. /* #ifndef APP-NVUE */
  414. display: block;
  415. /* #endif */
  416. position: relative;
  417. background-color: #ffffff;
  418. }
  419. .u-drawer-content-visible.u-drawer-center {
  420. transform: scale(1);
  421. opacity: 1;
  422. }
  423. .u-animation-zoom {
  424. transform: scale(1.15);
  425. }
  426. .u-drawer-content-visible {
  427. transform: translate3D(0px, 0px, 0px) !important;
  428. }
  429. .u-close {
  430. position: absolute;
  431. z-index: 3;
  432. }
  433. .u-close--top-left {
  434. top: 30rpx;
  435. left: 30rpx;
  436. }
  437. .u-close--top-right {
  438. top: 30rpx;
  439. right: 30rpx;
  440. }
  441. .u-close--bottom-left {
  442. bottom: 30rpx;
  443. left: 30rpx;
  444. }
  445. .u-close--bottom-right {
  446. right: 30rpx;
  447. bottom: 30rpx;
  448. }
  449. </style>