u-popup.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <view v-show="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. @tap.stop.prevent
  16. :style="[style]"
  17. >
  18. <view class="u-mode-center-box" @tap.stop.prevent v-if="mode == 'center'" :style="[centerStyle]">
  19. <u-icon
  20. @click="close"
  21. v-if="closeable"
  22. class="u-close"
  23. :class="['u-close--' + closeIconPos]"
  24. :name="closeIcon"
  25. :color="closeIconColor"
  26. :size="closeIconSize"
  27. ></u-icon>
  28. <scroll-view class="u-drawer__scroll-view" scroll-y="true">
  29. <slot />
  30. </scroll-view>
  31. </view>
  32. <scroll-view class="u-drawer__scroll-view" scroll-y="true" v-else>
  33. <slot />
  34. </scroll-view>
  35. <view @tap="close" class="u-close" :class="['u-close--' + closeIconPos]">
  36. <u-icon
  37. v-if="mode != 'center' && closeable"
  38. :name="closeIcon"
  39. :color="closeIconColor"
  40. :size="closeIconSize"
  41. ></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: ''
  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. // 遮罩打开或收起的动画过渡时间,单位ms
  191. duration: {
  192. type: [String, Number],
  193. default: 250
  194. }
  195. },
  196. data() {
  197. return {
  198. visibleSync: false,
  199. showDrawer: false,
  200. timer: null,
  201. closeFromInner: false, // value的值改变,是发生在内部还是外部
  202. };
  203. },
  204. computed: {
  205. // 根据mode的位置,设定其弹窗的宽度(mode = left|right),或者高度(mode = top|bottom)
  206. style() {
  207. let style = {};
  208. // 如果是左边或者上边弹出时,需要给translate设置为负值,用于隐藏
  209. if (this.mode == 'left' || this.mode == 'right') {
  210. style = {
  211. width: this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length),
  212. height: '100%',
  213. transform: `translate3D(${this.mode == 'left' ? '-100%' : '100%'},0px,0px)`
  214. };
  215. } else if (this.mode == 'top' || this.mode == 'bottom') {
  216. style = {
  217. width: '100%',
  218. height: this.height ? this.getUnitValue(this.height) : this.getUnitValue(this.length),
  219. transform: `translate3D(0px,${this.mode == 'top' ? '-100%' : '100%'},0px)`
  220. };
  221. }
  222. style.zIndex = this.uZindex;
  223. // 如果用户设置了borderRadius值,添加弹窗的圆角
  224. if (this.borderRadius) {
  225. switch (this.mode) {
  226. case 'left':
  227. style.borderRadius = `0 ${this.borderRadius}rpx ${this.borderRadius}rpx 0`;
  228. break;
  229. case 'top':
  230. style.borderRadius = `0 0 ${this.borderRadius}rpx ${this.borderRadius}rpx`;
  231. break;
  232. case 'right':
  233. style.borderRadius = `${this.borderRadius}rpx 0 0 ${this.borderRadius}rpx`;
  234. break;
  235. case 'bottom':
  236. style.borderRadius = `${this.borderRadius}rpx ${this.borderRadius}rpx 0 0`;
  237. break;
  238. default:
  239. }
  240. // 不加可能圆角无效
  241. style.overflow = 'hidden';
  242. }
  243. if(this.duration) style.transition = `all ${this.duration / 1000}s linear`;
  244. return style;
  245. },
  246. // 中部弹窗的特有样式
  247. centerStyle() {
  248. let style = {};
  249. style.width = this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length);
  250. // 中部弹出的模式,如果没有设置高度,就用auto值,由内容撑开高度
  251. style.height = this.height ? this.getUnitValue(this.height) : 'auto';
  252. style.zIndex = this.uZindex;
  253. style.marginTop = `-${this.$u.addUnit(this.negativeTop)}`;
  254. if (this.borderRadius) {
  255. style.borderRadius = `${this.borderRadius}rpx`;
  256. // 不加可能圆角无效
  257. style.overflow = 'hidden';
  258. }
  259. return style;
  260. },
  261. // 计算整理后的z-index值
  262. uZindex() {
  263. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  264. }
  265. },
  266. watch: {
  267. value(val) {
  268. if (val) {
  269. this.open();
  270. } else if(!this.closeFromInner) {
  271. this.close();
  272. }
  273. this.closeFromInner = false;
  274. }
  275. },
  276. mounted() {
  277. // 组件渲染完成时,检查value是否为true,如果是,弹出popup
  278. this.value && this.open();
  279. },
  280. methods: {
  281. // 判断传入的值,是否带有单位,如果没有,就默认用rpx单位
  282. getUnitValue(val) {
  283. if(/(%|px|rpx|auto)$/.test(val)) return val;
  284. else return val + 'rpx'
  285. },
  286. // 遮罩被点击
  287. maskClick() {
  288. this.close();
  289. },
  290. close() {
  291. // 标记关闭是内部发生的,否则修改了value值,导致watch中对value检测,导致再执行一遍close
  292. // 造成@close事件触发两次
  293. this.closeFromInner = true;
  294. this.change('showDrawer', 'visibleSync', false);
  295. },
  296. // 中部弹出时,需要.u-drawer-content将居中内容,此元素会铺满屏幕,点击需要关闭弹窗
  297. // 让其只在mode=center时起作用
  298. modeCenterClose(mode) {
  299. if (mode != 'center' || !this.maskCloseAble) return;
  300. this.close();
  301. },
  302. open() {
  303. this.change('visibleSync', 'showDrawer', true);
  304. },
  305. // 此处的原理是,关闭时先通过动画隐藏弹窗和遮罩,再移除整个组件
  306. // 打开时,先渲染组件,延时一定时间再让遮罩和弹窗的动画起作用
  307. change(param1, param2, status) {
  308. // 如果this.popup为false,意味着为picker,actionsheet等组件调用了popup组件
  309. if (this.popup == true) {
  310. this.$emit('input', status);
  311. }
  312. this[param1] = status;
  313. if(status) {
  314. // #ifdef H5 || MP
  315. this.timer = setTimeout(() => {
  316. this[param2] = status;
  317. this.$emit(status ? 'open' : 'close');
  318. }, 50);
  319. // #endif
  320. // #ifndef H5 || MP
  321. this.$nextTick(() => {
  322. this[param2] = status;
  323. this.$emit(status ? 'open' : 'close');
  324. })
  325. // #endif
  326. } else {
  327. this.timer = setTimeout(() => {
  328. this[param2] = status;
  329. this.$emit(status ? 'open' : 'close');
  330. }, this.duration);
  331. }
  332. }
  333. }
  334. };
  335. </script>
  336. <style scoped lang="scss">
  337. @import "../../libs/css/style.components.scss";
  338. .u-drawer {
  339. /* #ifndef APP-NVUE */
  340. display: block;
  341. /* #endif */
  342. position: fixed;
  343. top: 0;
  344. left: 0;
  345. right: 0;
  346. bottom: 0;
  347. overflow: hidden;
  348. }
  349. .u-drawer-content {
  350. margin: 0 auto;
  351. max-width: 400px;
  352. /* #ifndef APP-NVUE */
  353. display: block;
  354. /* #endif */
  355. // position: absolute;
  356. position: fixed;
  357. z-index: 1003;
  358. transition: all 0.25s linear;
  359. }
  360. .u-drawer__scroll-view {
  361. width: 100%;
  362. height: 100%;
  363. }
  364. .u-drawer-left {
  365. top: 0;
  366. bottom: 0;
  367. left: 0;
  368. background-color: #ffffff;
  369. }
  370. .u-drawer-right {
  371. right: 0;
  372. top: 0;
  373. bottom: 0;
  374. background-color: #ffffff;
  375. }
  376. .u-drawer-top {
  377. top: 0;
  378. left: 0;
  379. right: 0;
  380. background-color: #ffffff;
  381. }
  382. .u-drawer-bottom {
  383. bottom: 0;
  384. left: 0;
  385. right: 0;
  386. background-color: #ffffff;
  387. }
  388. .u-drawer-center {
  389. @include vue-flex;
  390. flex-direction: column;
  391. bottom: 0;
  392. left: 0;
  393. right: 0;
  394. top: 0;
  395. justify-content: center;
  396. align-items: center;
  397. opacity: 0;
  398. z-index: 99999;
  399. }
  400. .u-mode-center-box {
  401. min-width: 100rpx;
  402. min-height: 100rpx;
  403. /* #ifndef APP-NVUE */
  404. display: block;
  405. /* #endif */
  406. position: relative;
  407. background-color: #ffffff;
  408. }
  409. .u-drawer-content-visible.u-drawer-center {
  410. transform: scale(1);
  411. opacity: 1;
  412. }
  413. .u-animation-zoom {
  414. transform: scale(1.15);
  415. }
  416. .u-drawer-content-visible {
  417. transform: translate3D(0px, 0px, 0px) !important;
  418. }
  419. .u-close {
  420. position: absolute;
  421. z-index: 3;
  422. }
  423. .u-close--top-left {
  424. top: 30rpx;
  425. left: 30rpx;
  426. }
  427. .u-close--top-right {
  428. top: 30rpx;
  429. right: 30rpx;
  430. }
  431. .u-close--bottom-left {
  432. bottom: 30rpx;
  433. left: 30rpx;
  434. }
  435. .u-close--bottom-right {
  436. right: 30rpx;
  437. bottom: 30rpx;
  438. }
  439. </style>