u-modal.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <u-popup
  3. mode="center"
  4. :zoom="zoom"
  5. :show="show"
  6. :customStyle="{
  7. borderRadius: '6px',
  8. overflow: 'hidden',
  9. marginTop: `-${$u.addUnit(negativeTop)}`
  10. }"
  11. :closeOnClickOverlay="closeOnClickOverlay"
  12. :safeAreaInsetBottom="false"
  13. :duration="duration"
  14. @click="clickHandler"
  15. >
  16. <view
  17. class="u-modal"
  18. :style="{
  19. width: $u.addUnit(width),
  20. }"
  21. >
  22. <text
  23. class="u-modal__title"
  24. v-if="title"
  25. >{{ title }}</text>
  26. <view
  27. class="u-modal__content"
  28. :style="{
  29. paddingTop: `${title ? 12 : 25}px`
  30. }"
  31. >
  32. <slot>
  33. <text class="u-modal__content__text">{{ content }}</text>
  34. </slot>
  35. </view>
  36. <view
  37. class="u-modal__button-group--confirm-button"
  38. v-if="$slots.confirmButton"
  39. >
  40. <slot name="confirmButton"></slot>
  41. </view>
  42. <template v-else>
  43. <u-line></u-line>
  44. <view
  45. class="u-modal__button-group"
  46. :style="{
  47. flexDirection: buttonReverse ? 'row-reverse' : 'row'
  48. }"
  49. >
  50. <view
  51. class="u-modal__button-group__wrapper u-modal__button-group__wrapper--cancel"
  52. :hover-stay-time="150"
  53. hover-class="u-modal__button-group__wrapper--hover"
  54. :class="[showCancelButton && !showConfirmButton && 'u-modal__button-group__wrapper--only-cancel']"
  55. v-if="showCancelButton"
  56. @tap="cancelHandler"
  57. >
  58. <text
  59. class="u-modal__button-group__wrapper__text"
  60. :style="{
  61. color: cancelColor
  62. }"
  63. >{{ cancelText }}</text>
  64. </view>
  65. <u-line
  66. direction="column"
  67. v-if="showConfirmButton && showCancelButton"
  68. ></u-line>
  69. <view
  70. class="u-modal__button-group__wrapper u-modal__button-group__wrapper--confirm"
  71. :hover-stay-time="150"
  72. hover-class="u-modal__button-group__wrapper--hover"
  73. :class="[!showCancelButton && showConfirmButton && 'u-modal__button-group__wrapper--only-confirm']"
  74. v-if="showConfirmButton"
  75. @tap="confirmHandler"
  76. >
  77. <u-loading-icon v-if="loading"></u-loading-icon>
  78. <text
  79. v-else
  80. class="u-modal__button-group__wrapper__text"
  81. :style="{
  82. color: confirmColor
  83. }"
  84. >{{ confirmText }}</text>
  85. </view>
  86. </view>
  87. </template>
  88. </view>
  89. </u-popup>
  90. </template>
  91. <script>
  92. import props from './props.js';
  93. /**
  94. * Modal 模态框
  95. * @description 弹出模态框,常用于消息提示、消息确认、在当前页面内完成特定的交互操作。
  96. * @tutorial https://www.uviewui.com/components/modul.html
  97. * @property {Boolean} show 是否显示模态框,请赋值给show (默认 false )
  98. * @property {String} title 标题内容
  99. * @property {String} content 模态框内容,如传入slot内容,则此参数无效
  100. * @property {String} confirmText 确认按钮的文字 (默认 '确认' )
  101. * @property {String} cancelText 取消按钮的文字 (默认 '取消' )
  102. * @property {Boolean} showConfirmButton 是否显示确认按钮 (默认 true )
  103. * @property {Boolean} showCancelButton 是否显示取消按钮 (默认 false )
  104. * @property {String} confirmColor 确认按钮的颜色 (默认 '#2979ff' )
  105. * @property {String} cancelColor 取消按钮的颜色 (默认 '#606266' )
  106. * @property {Number} duration 弹窗动画过度时间 (默认 400 )
  107. * @property {Boolean} buttonReverse 对调确认和取消的位置 (默认 false )
  108. * @property {Boolean} zoom 是否开启缩放模式 (默认 true )
  109. * @property {Boolean} asyncClose 是否异步关闭,只对确定按钮有效,见上方说明 (默认 false )
  110. * @property {Boolean} closeOnClickOverlay 是否允许点击遮罩关闭Modal (默认 false )
  111. * @property {String | Number} negativeTop 往上偏移的值,给一个负的margin-top,往上偏移,避免和键盘重合的情况,单位任意,数值则默认为px单位 (默认 0 )
  112. * @property {String | Number} width modal宽度,不支持百分比,可以数值,px,rpx单位 (默认 '650rpx' )
  113. * @property {String} confirmButtonShape 确认按钮的样式,如设置,将不会显示取消按钮
  114. * @event {Function} confirm 点击确认按钮时触发
  115. * @event {Function} cancel 点击取消按钮时触发
  116. * @event {Function} close 点击遮罩关闭出发,closeOnClickOverlay为true有效
  117. * @example <u-modal :show="true" title="title" content="content"></u-modal>
  118. */
  119. export default {
  120. name: 'u-modal',
  121. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  122. data() {
  123. return {
  124. loading: false
  125. }
  126. },
  127. watch: {
  128. show(n) {
  129. // 为了避免第一次打开modal,又使用了异步关闭的loading
  130. // 第二次打开modal时,loading依然存在的情况
  131. if (n && this.loading) this.loading = false
  132. }
  133. },
  134. methods: {
  135. // 点击确定按钮
  136. confirmHandler() {
  137. // 如果配置了异步关闭,将按钮值为loading状态
  138. if (this.asyncClose) {
  139. this.loading = true;
  140. }
  141. this.$emit('confirm')
  142. },
  143. // 点击取消按钮
  144. cancelHandler() {
  145. this.$emit('cancel')
  146. },
  147. // 点击遮罩
  148. // 从原理上来说,modal的遮罩点击,并不是真的点击到了遮罩
  149. // 因为modal依赖于popup的中部弹窗类型,中部弹窗比较特殊,虽然有遮罩,但是为了让弹窗内容能flex居中
  150. // 多了一个透明的遮罩,此透明的遮罩会覆盖在灰色的遮罩上,所以实际上是点击不到灰色遮罩的,popup内部在
  151. // 透明遮罩的子元素做了.stop处理,所以点击内容区,也不会导致误触发
  152. clickHandler() {
  153. if (this.closeOnClickOverlay) {
  154. this.$emit('close')
  155. }
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. @import "../../libs/css/components.scss";
  162. $u-modal-border-radius: 6px;
  163. .u-modal {
  164. width: 650rpx;
  165. border-radius: $u-modal-border-radius;
  166. overflow: hidden;
  167. &__title {
  168. font-size: 16px;
  169. font-weight: bold;
  170. color: $u-content-color;
  171. text-align: center;
  172. padding-top: 25px;
  173. }
  174. &__content {
  175. padding: 12px 25px 25px 25px;
  176. @include flex;
  177. justify-content: center;
  178. &__text {
  179. font-size: 15px;
  180. color: $u-content-color;
  181. flex: 1;
  182. }
  183. }
  184. &__button-group {
  185. @include flex;
  186. &--confirm-button {
  187. flex-direction: column;
  188. padding: 0px 25px 15px 25px;
  189. }
  190. &__wrapper {
  191. flex: 1;
  192. @include flex;
  193. justify-content: center;
  194. align-items: center;
  195. height: 48px;
  196. &--confirm,
  197. &--only-cancel {
  198. border-bottom-right-radius: $u-modal-border-radius;
  199. }
  200. &--cancel,
  201. &--only-confirm {
  202. border-bottom-left-radius: $u-modal-border-radius;
  203. }
  204. &--hover {
  205. background-color: $u-bg-color;
  206. }
  207. &__text {
  208. color: $u-content-color;
  209. font-size: 16px;
  210. text-align: center;
  211. }
  212. }
  213. }
  214. }
  215. </style>