u-popup.vue 13 KB

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