u-popup.vue 13 KB

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