u-modal.vue 8.7 KB

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