u-popup.vue 12 KB

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