action-sheet.vue 3.02 KB
<template>
  <div v-transfer-dom :data-transfer="transfer" :class="actionCls">
    <transition name="action-sheet-mask">
      <div class="yoho-popup-mask" @click="maskClick" v-if="mask && isVisible"></div>
    </transition>
    <transition name="action-sheet-move">
      <div class="yoho-popup" v-show="isVisible" :style="{'z-index': zIndex}">
        <div class="yoho-popup-container" :style="{'max-height': maxHeight + 'px', 'height': contentHeight}">
          <slot></slot>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'YohoActionSheet',
  props: {
    maskClosable: {
      type: Boolean,
      default: true
    },
    mask: {
      type: Boolean,
      default: true
    },
    transfer: Boolean,
    zIndex: {
      type: Number,
      default: 100
    },
    visible: {
      type: Boolean,
      default: false
    },
    full: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isVisible: false,
      maxHeight: 0
    };
  },
  mounted() {
    this.maxHeight = document.body.offsetHeight;
    this.$watch('visible', (newVal) => {
      if (newVal) {
        this.show();
      } else {
        this.hide();
      }
    }, {
      immediate: true
    });
  },
  computed: {
    actionCls() {
      return [{ 'yoho-action-sheet': this.full }];
    },
    contentHeight() {
      return this.full ? `${this.maxHeight - 20}px` : 'auto';
    }
  },
  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-move-enter,
  .action-sheet-move-leave-active {
    transform: translate3d(0, 100%, 0);
  }

  .action-sheet-move-enter,
  .action-sheet-move-leave-to {
    opacity: 0.5;
  }

  .action-sheet-move-enter-active {
    transition: all 0.3s cubic-bezier(0.165, 0.84, 0.44, 1);
  }

  .action-sheet-move-leave-active {
    transition: all 0.1s linear;
  }

  .action-sheet-mask-enter,
  .action-sheet-mask-leave-to {
    opacity: 0;
  }

  .action-sheet-mask-enter-active {
    transition: opacity 0.3s linear;
  }

  .action-sheet-mask-leave-active {
    transition: opacity 0.1s linear;
  }

  .yoho-popup-mask {
    display: block;
    position: absolute;
    z-index: 99;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }

  .yoho-popup {
    width: 100%;
    position: fixed;
    bottom: 0;

    .yoho-popup-container {
      background-color: #fff;
      position: relative;
      opacity: 1;
    }

    &:after {
      content: "";
      width: 100%;
      height: 200px;
      position: absolute;
      left: 0;
      bottom: -200px;
      z-index: -1;
      background: #fff;
    }
  }

  .yoho-action-sheet {
    .yoho-popup-mask {
      background-color: #f5f5f5;
    }

    .yoho-popup-container {
      border-radius: 15PX 15PX 0 0;
      overflow: hidden;
    }
  }

</style>