modal.vue 2.63 KB
<template>
  <div class="modal-box" v-show="value" v-transfer-dom :data-transfer="transfer">
    <div class="modal-mask"></div>
    <div class="modal-wrap">
      <div class="modal modal-content">
        <div class="modal-body">
          <slot>
            <div class="text">{{title}}</div>
          </slot>
        </div>
        <div class="modal-footer">
          <slot name="footer">
            <button class="btn" v-if="sureText" :class="{active: loading}" type="button" @click="onSure">{{sureText}}</button>
            <button class="btn cancel-btn" v-if="cancelText" type="button" @click="onCancel">{{cancelText}}</button>
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Modal',
  props: {
    transfer: [Boolean],
    title: String,
    value: Boolean,
    loading: Boolean,
    sureText: {
      type: String,
      default: ''
    },
    cancelText: {
      type: String,
      default: ''
    }
  },
  methods: {
    onCancel() {
      this.$emit('on-cancel');
      this.$emit('input', false);
    },
    onSure() {
      if (!this.loading) {
        this.$emit('on-sure');
      }
    }
  },
};
</script>

<style lang="scss">
  .modal-box {
    font-size: 24px;
  }

  .modal-mask {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.4);
    height: 100%;
    z-index: 99;

    &-hidden {
      display: none;
    }
  }

  .modal {
    width: auto;
    margin: 0 auto;
    position: relative;
    outline: none;

    &-hidden {
      display: none !important;
    }

    &-wrap {
      position: fixed;
      overflow: auto;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: 99;
      -webkit-overflow-scrolling: touch;
      outline: 0;
      display: flex;
      align-items: center;
    }

    &-content {
      position: relative;
      background-color: #fff;
      border: 0;
      width: 600px;
      background-clip: padding-box;

      & .text {
        text-align: center;
        padding-top: 30px;
        padding-bottom: 30px;
      }
    }

    &-body {
      padding: 16px 38px;
    }

    &-footer {
      width: 100%;
      border-top: 1px solid #eee;
      display: flex;

      button {
        width: 100%;
        overflow: hidden;
        height: 100px;
        background: none;
        border: none;

        &:first-child {
          color: #c94353;
          font-weight: 500;
        }

        &.cancel-btn {
          color: #000;
        }
      }

      button + button {
        margin-left: 8px;
        margin-bottom: 0;
        border-left: 1px solid #eee;
      }
    }
  }
</style>