1
0

u-popup.vue 13 KB

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