action-sheet.vue 2.44 KB
<template>
  <div v-transfer-dom :data-transfer="transfer">
    <transition name="action-sheet-fade">
      <div class="yoho-popup" :class="actionCls" v-show="isVisible" :style="{'z-index': zIndex}">
        <div class="yoho-popup-mask" @click="maskClick"></div>
        <div class="yoho-popup-container">
          <div class="yoho-popup-content">
            <transition name="action-sheet-move">
              <div class="detail" v-show="isVisible">
                <slot></slot>
              </div>
            </transition>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'YohoActionSheet',
  props: {
    maskClosable: {
      type: Boolean,
      default: true
    },
    transfer: Boolean,
    zIndex: {
      type: Number,
      default: 5
    },
    visible: {
      type: Boolean,
      default: false
    },
    full: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isVisible: false,
    };
  },
  mounted() {
    this.$watch('visible', (newVal) => {
      if (newVal) {
        this.show();
      } else {
        this.hide();
      }
    }, {
      immediate: true
    });
  },
  computed: {
    actionCls() {
      return [{ 'yoho-action-sheet': this.full }];
    }
  },
  methods: {
    maskClick() {
      this.maskClosable && this.cancel();
    },

    cancel() {
      this.hide();
      this.$emit('cancel');
    },

    show() {
      this.isVisible = true;
    },

    hide() {
      this.isVisible = false;
    }
  }
};

</script>

<style lang="scss" scoped>
  .action-sheet-fade-enter,
  .action-sheet-fade-leave-active {
    opacity: 0;
  }

  .action-sheet-fade-enter-active,
  .action-sheet-fade-leave-active {
    transition: all 0.2s ease-in-out;
  }

  .action-sheet-move-enter,
  .action-sheet-move-leave-active {
    transform: translate3d(0, 100%, 0);
  }

  .action-sheet-move-enter-active,
  .action-sheet-move-leave-active {
    transition: all 0.2s ease-in-out;
  }

  .yoho-popup {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 5;

    .yoho-popup-mask {
      display: block;
    }

    .yoho-popup-mask,
    .yoho-popup-container {
      background-color: #fff;
      position: absolute;
      width: 100%;
      height: 100%;
    }
  }

  .yoho-action-sheet {
    .yoho-popup-content {
      height: 100%;
    }
  }

  .detail {
    position: relative;
    width: 100%;
    height: 100%;
  }

</style>