u-popup.vue 12 KB

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