MyToast.vue 3.0 KB
<template>
  <div class="dyson-toast" v-if="ylTotasOpen">
    <div v-if="ylToastType === 'hint'" class="toast-view hint-toast">
      <div class="totas-text">{{ ylTotasHintText }}</div>
    </div>
    <div v-if="ylToastType === 'loading'" class="toast-view loading-toast">
      <div class="loading-img" v-if="isShowIcon">
        <span class="iconfont" :class="ylTotasLoadingIcon"></span>
      </div>
      <div class="totas-text">{{ ylTotasHintText }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyToast',
  props: {
    isOpen: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    isShowIcon: {
      type: Boolean,
      default: () => {
        return true
      }
    },
    toastType: {
      type: String,
      default: 'hint'
    },
    hintText: {
      type: String,
      default: ''
    },
    duration: {
      type: Number,
      default: 3000
    },
    loadingIcon: {
      type: String,
      default: 'icon-loading-3'
    }
  },
  data() {
    return {
      ylTotasOpen: false,
      ylToastType: '',
      ylTotasHintText: '',
      ylTotasDuration: 0,
      ylTotasLoadingIcon: '',
      st: null
    }
  },
  watch: {
    ylTotasOpen(newVal) {
      if (newVal && this.ylToastType !== 'loading') {
        if (this.ylTotasDuration !== 0) {
          this.st = setTimeout(() => {
            this.ylTotasOpen = false
          }, this.ylTotasDuration)
        }
      }
    },
    isOpen(newVal) {
      this.ylTotasOpen = newVal
    },
    hintText(newVal) {
      this.ylTotasHintText = newVal
    }
  },
  created() {
    this.ylTotasOpen = this.isOpen
    this.ylTotasHintText = this.hintText
    this.ylToastType = this.toastType
    this.ylTotasDuration = this.duration
    this.ylTotasLoadingIcon = this.loadingIcon
  },
  methods: {
    loading(config) {
      if (this.st) {
        clearTimeout(this.st)
        this.st = null
      }
      this.ylToastType = 'loading'
      this.ylTotasHintText = config.hintText
      this.ylTotasDuration = config.duration
      this.ylTotasOpen = config.isOpen
      if (config.loadingIcon) {
        this.ylTotasLoadingIcon = config.loadingIcon
      }
      return this
    }
  }
}
</script>

<style lang="scss" scoped>
.dyson-toast {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  .toast-view {
    min-width: 130px;
    padding: 8px 30px;
    border-radius: 6px;
    background-color: #333;
    .totas-text {
      font-size: 12px;
      color: #fff;
      text-align: center;
    }
  }
  .loading-toast {
    .loading-img {
      text-align: center;
      margin-top: 10px;
      margin-bottom: 10px;
      animation: rotate 1s linear infinite;
      .iconfont {
        color: #fff;
        font-size: 18px;
      }
    }
  }
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  25% {
    transform: rotate(90deg);
  }
  50% {
    transform: rotate(180deg);
  }
  75% {
    transform: rotate(270deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>