cl-popup.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view
  3. v-if="show"
  4. :class="[
  5. 'cl-popup__wrapper',
  6. `cl-popup__wrapper--${direction}`,
  7. `is-${status ? 'open' : 'close'}`
  8. ]"
  9. @touchmove.stop.prevent
  10. >
  11. <view class="cl-popup__modal" @tap="modalClose" v-if="modal"></view>
  12. <view
  13. :class="['cl-popup']"
  14. :style="{ height, width, backgroundColor, borderRadius: parseRpx(borderRadius) }"
  15. >
  16. <view class="cl-popup__container" :style="{ padding: parseRpx(padding) }">
  17. <slot></slot>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import { parseRpx } from "../../utils";
  24. /**
  25. * popup 弹出框
  26. * @description 支持各方向的弹出框
  27. * @tutorial https://docs.cool-js.com/uni/components/view/popup.html
  28. * @property {Boolean} visible 是否可见
  29. * @property {Function} beforeClose 关闭前钩子函数
  30. * @property {String} direction 弹出方向, top | right | bottom | left | center,默认left
  31. * @property {Boolean} closeOnClickModal 点击遮罩层是否关闭,默认true
  32. * @property {String, Number} size 弹出框大小,默认auto
  33. * @property {String} backgroundColor 背景颜色,默认#fff
  34. * @property {String, Number} borderRadius 内容圆角
  35. * @property {String, Number} padding 内容内间据,默认20
  36. * @property {Boolean} modal 是否显示遮罩层
  37. * @example <cl-popup :visible.sync="visible">Hello !</cl-popup>
  38. */
  39. export default {
  40. name: "cl-popup",
  41. props: {
  42. // 是否可见
  43. visible: Boolean,
  44. // 关闭前钩子函数
  45. beforeClose: Function,
  46. // 弹出方向
  47. direction: {
  48. type: String,
  49. default: "left"
  50. },
  51. // 点击遮罩层是否关闭
  52. closeOnClickModal: {
  53. type: Boolean,
  54. default: true
  55. },
  56. // 弹出框大小
  57. size: {
  58. type: [String, Number],
  59. default: "auto"
  60. },
  61. // 背景颜色
  62. backgroundColor: {
  63. type: String,
  64. default: "#fff"
  65. },
  66. // 内容圆角
  67. borderRadius: [String, Number],
  68. // 内容内间据
  69. padding: {
  70. type: [String, Number],
  71. default: 20
  72. },
  73. // 是否显示遮罩层
  74. modal: {
  75. type: Boolean,
  76. default: true
  77. }
  78. },
  79. data() {
  80. return {
  81. show: false,
  82. status: false,
  83. timer: null
  84. };
  85. },
  86. computed: {
  87. height() {
  88. switch (this.direction) {
  89. case "top":
  90. case "bottom":
  91. return parseRpx(this.size);
  92. case "left":
  93. case "right":
  94. return "100%";
  95. }
  96. },
  97. width() {
  98. switch (this.direction) {
  99. case "top":
  100. case "bottom":
  101. return "100%";
  102. case "left":
  103. case "right":
  104. case "center":
  105. return parseRpx(this.size);
  106. }
  107. }
  108. },
  109. watch: {
  110. visible: {
  111. immediate: true,
  112. handler(val) {
  113. if (val) {
  114. this.open();
  115. } else {
  116. this.close();
  117. }
  118. }
  119. }
  120. },
  121. methods: {
  122. parseRpx,
  123. open() {
  124. if (!this.show) {
  125. // 显示内容
  126. this.show = true;
  127. this.$emit("update:visible", true);
  128. this.$emit("open");
  129. clearTimeout(this.timer);
  130. this.timer = setTimeout(() => {
  131. // 开始动画
  132. this.status = true;
  133. // 等待动画结束
  134. this.timer = setTimeout(() => {
  135. this.$emit("opened");
  136. }, 350);
  137. }, 50);
  138. }
  139. },
  140. close() {
  141. if (this.status) {
  142. const done = () => {
  143. // 关闭动画
  144. this.status = false;
  145. this.$emit("close");
  146. clearTimeout(this.timer);
  147. this.timer = setTimeout(() => {
  148. // 隐藏内容
  149. this.show = false;
  150. this.$emit("update:visible", false);
  151. this.$emit("closed");
  152. }, 300);
  153. };
  154. if (this.beforeClose) {
  155. this.beforeClose(done);
  156. } else {
  157. done();
  158. }
  159. }
  160. },
  161. modalClose() {
  162. if (!this.closeOnClickModal) {
  163. return false;
  164. }
  165. this.close();
  166. }
  167. }
  168. };
  169. </script>