toast.vue 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
<template>
  <transition name="uni-fade">
    <uni-toast
fxy060608's avatar
fxy060608 已提交
4
      v-if="visible"
fxy060608's avatar
fxy060608 已提交
5 6
      :data-duration="duration"
    >
fxy060608's avatar
fxy060608 已提交
7 8 9 10
      <div
        v-if="mask"
        class="uni-mask"
        style="background: transparent;"
fxy060608's avatar
fxy060608 已提交
11 12
        @touchmove.prevent
      />
fxy060608's avatar
fxy060608 已提交
13 14
      <div
        v-if="!image&&!iconClass"
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19
        class="uni-sample-toast"
      >
        <p class="uni-simple-toast__text">
          {{ title }}
        </p>
fxy060608's avatar
fxy060608 已提交
20 21 22
      </div>
      <div
        v-else
fxy060608's avatar
fxy060608 已提交
23 24
        class="uni-toast"
      >
fxy060608's avatar
fxy060608 已提交
25 26 27
        <img
          v-if="image"
          :src="image"
fxy060608's avatar
fxy060608 已提交
28 29
          class="uni-toast__icon"
        >
fxy060608's avatar
fxy060608 已提交
30 31 32
        <i
          v-else
          :class="iconClass"
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37
          class="uni-icon_toast"
        />
        <p class="uni-toast__content">
          {{ title }}
        </p>
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      </div>
    </uni-toast>
  </transition>
</template>
<script>
import transition from './mixins/transition'
export default {
  name: 'Toast',
  mixins: [transition],
  props: {
    title: {
      type: String,
      default: ''
    },
    icon: {
      default: 'success',
      validator (value) {
        return ['success', 'loading', 'none'].indexOf(value) !== -1
      }
    },
    image: {
      type: String,
      default: ''
    },
    duration: {
      type: Number,
      default: 1500
    },
    mask: {
      type: Boolean,
      default: false
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    iconClass () {
      if (this.icon === 'success') {
        return 'uni-icon-success-no-circle'
      }
      if (this.icon === 'loading') {
        return 'uni-loading'
fxy060608's avatar
fxy060608 已提交
82 83
      }
      return ''
fxy060608's avatar
fxy060608 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
    }
  },
  beforeUpdate () {
    if (this.visible) {
      this.timeoutId && clearTimeout(this.timeoutId)
      this.timeoutId = setTimeout(() => {
        UniServiceJSBridge.emit('onHideToast')
      }, this.duration)
    }
  }
}
</script>
<style>
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
uni-toast {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 999;
  display: block;
  box-sizing: border-box;
  pointer-events: none;
}

uni-toast .uni-sample-toast {
  position: fixed;
  z-index: 999;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  max-width: 80%;
}
fxy060608's avatar
fxy060608 已提交
118

119 120 121 122 123 124 125 126 127 128 129 130 131
uni-toast .uni-simple-toast__text {
  display: inline-block;
  vertical-align: middle;
  color: #ffffff;
  background-color: rgba(17, 17, 17, 0.7);
  padding: 10px 20px;
  border-radius: 5px;
  font-size: 13px;
  text-align: center;
  max-width: 100%;
  word-break: break-all;
  white-space: normal;
}
fxy060608's avatar
fxy060608 已提交
132

133 134 135
uni-toast .uni-mask {
  pointer-events: auto;
}
fxy060608's avatar
fxy060608 已提交
136

137 138 139 140 141 142 143 144 145 146 147 148
uni-toast .uni-toast {
  position: fixed;
  z-index: 999;
  width: 8em;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(17, 17, 17, 0.7);
  text-align: center;
  border-radius: 5px;
  color: #ffffff;
}
fxy060608's avatar
fxy060608 已提交
149

150 151 152
uni-toast .uni-toast * {
  box-sizing: border-box;
}
fxy060608's avatar
fxy060608 已提交
153

154 155 156 157 158 159
uni-toast .uni-toast__icon {
  margin: 20px 0 0;
  width: 38px;
  height: 38px;
  vertical-align: baseline;
}
fxy060608's avatar
fxy060608 已提交
160

161 162 163
uni-toast .uni-icon_toast {
  margin: 15px 0 0;
}
fxy060608's avatar
fxy060608 已提交
164

165 166 167 168
uni-toast .uni-icon_toast.uni-icon-success-no-circle:before {
  color: #ffffff;
  font-size: 55px;
}
fxy060608's avatar
fxy060608 已提交
169

170 171 172 173 174 175
uni-toast .uni-icon_toast.uni-loading {
  margin: 20px 0 0;
  width: 38px;
  height: 38px;
  vertical-align: baseline;
}
fxy060608's avatar
fxy060608 已提交
176

177 178 179 180
uni-toast .uni-toast__content {
  margin: 0 0 15px;
}
</style>