u-popup.vue 12 KB

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