all files / src/components/ button.vue

100% Statements 3/3
100% Branches 4/4
100% Functions 2/2
100% Lines 3/3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                                          6×                         2× 1×                                                                                                                                                                
<template>
  <button class="n-button" 
    :class="{[`icon-${iconPosition}`]: true, loading: loading, disabled: disabled}"
    @click="buttonClickEvent"
  >
    <n-icon v-if="icon&&(!loading)" :icon-name="icon"></n-icon>
    <n-icon v-if="loading" icon-name="loading"></n-icon>
    <div class="content">
      <slot></slot>
    </div>
    <div class="loading-mask" v-if="loading"></div>
  </button>
</template>
 
<script>
import NIcon from "./icon"
 
export default {
  name: "n-button",
  props: {
    icon: {},
    loading: {
      type: Boolean,
      default: false,
    },
    iconPosition: {
      type: String,
      default: 'left',
      validator(value) {
        return value == 'left' || value == 'right'
      }
    },
    disabled: {
      type: Boolean,
      default: false,
    }
  },
  components: {
    NIcon
  },
  methods: {
    buttonClickEvent() {
      if (!this.loading) {
        this.$emit('click')
      }
    }
  }
}
</script>
  
<style lang="scss" scoped> 
.n-button {
  height: $primary-height;
  font-size: $font-size;
  padding: 0 1em;
  border-radius: $border-radius-base;
  border: 1px solid $button-color-base;
  background-color: $primary-back-color;
  color: $primary-text-color;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: $box-shadow-button;
  transition: all .3s cubic-bezier(.645, .045, .355, 1);
  position: relative;
  &.loading {
    pointer-events: none;
    > .loading-mask {
      position: absolute;
      top: -1px;
      left: -1px;
      display: inline-block;
      width: calc(100% + 2px);
      height: calc(100% + 2px);
      pointer-events: none;
      opacity: 0.35;
      background-color: #fff; 
      transition: opacity .2s;
    }
  }
  &.disabled {
    
  }
  &.icon-left {
    >.icon {
      order: 1;
    }
    >.content {
      order: 2;
    }
  }
  &.icon-right {
    > .icon {
      order: 2;
    }
    >.content {
      order: 1;
    }
  }
  > .icon {
    vertical-align: middle;
    order: 1;
  }
  > .content {
    padding-left: .2em;
    padding-right: .2em;
    user-select: none;
  }
  &:hover {
    border-color: $primary-color-hover;
    background-color: $primary-back-color-hover;
  }
  &:active {
    border-color: $button-color-active;
    background-color: $primary-back-color-active;
  }
  &:focus {
    outline: none;
  }
}
</style>