u-popup.vue 9.9 KB

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