Skip to content

按钮组件 第一步

css 样式

bash
   ├── src
    ├── hooks
    ├── styles
     ├── index.scss
     ├── chooseButton
     ├── index.scss

styles/index.scss

scss
@use "./chooseButton/index.scss";

chooseButton/index.scss

scss
.Y-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 80px;
  padding: 0px 16px;
  height: 32px;
  border-radius: 12px;
  box-sizing: border-box;
  /* border: 2px solid #e2e6f1; */
  border-width: 2px;
  border-color: #e2e6f1;
  border-style: solid;
  background-color: #fff;
  font-size: 14px;
  color: #4d5059;
  cursor: pointer;
  word-break: keep-all;
  user-select: none;
  outline: none;
  transition: all 0.3s;
  span {
    line-height: 1;
  }
  &:hover {
    background: #eaf0ff;
    border-color: #97b4ff;
    color: #3069ff;
  }
  &.is-round {
    border-radius: 100px;
  }
  &.is-disabled {
    background-color: #ebeef5;
    border-color: #ebeef5;
    color: #fff;
    cursor: not-allowed;
    span {
      color: #abb1bf;
    }
  }
  &.is-text {
    border: none;
    background-color: transparent;
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #7c808c;
      }
    }
  }
  &.is-link {
    height: auto;
    min-width: auto;
    padding: 0px;
    border: none;
    background-color: transparent;
    text-decoration: underline;
    text-underline-offset: 5px;
    text-decoration-color: #4d5059;
    span {
      color: #4d5059;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #7c808c;
        text-decoration-color: #7c808c;
      }
    }
  }
  &.is-block {
    width: 100%;
    display: flex;
  }
  &.is-border {
    background-color: transparent;
    span {
      color: #4d5059;
    }
    &.is-disabled,
    &:hover {
      background-color: white;
      border-color: #e2e6f1;
      span {
        color: #7c808c;
      }
    }
  }
  // 尺寸
  &.Y-button--size_large {
    height: 40px;
    font-size: 16px;
    // 圆形
    &.is-circle {
      padding: 0;
      width: 40px;
      min-width: 40px;
      border-radius: 100%;
      span {
        display: none;
      }
    }
  }
  &.Y-button--size_small {
    height: 24px;
    font-size: 12px;
    border-radius: 10px;
    // 圆形
    &.is-circle {
      padding: 0;
      width: 24px;
      min-width: 24px;
      border-radius: 100%;
      span {
        display: none;
      }
    }
  }
  // 圆形
  &.is-circle {
    padding: 0;
    width: 32px;
    min-width: 32px;
    border-radius: 100%;
    span {
      display: none;
    }
  }
}

// 成功样式
.Y-button--success {
  background-color: #14cd70;
  border-color: #14cd70;
  span {
    color: #fff;
  }
  &:hover {
    background: #5bdc9b;
    border-color: #5bdc9b;
    color: #fff;
  }
  &.is-disabled {
    &,
    &:hover,
    &:focus {
      background-color: #8ae6b8;
      border-color: #8ae6b8;
      span {
        color: #fff;
      }
      cursor: not-allowed;
    }
  }
  &.is-text {
    border: none;
    background-color: transparent;
    span {
      color: #14cd70;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #8ae6b8;
      }
    }
  }
  &.is-link {
    text-decoration-color: #14cd70;
    span {
      color: #14cd70;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #8ae6b8;
        text-decoration-color: #8ae6b8;
      }
    }
  }
  &.is-border {
    background-color: transparent;
    span {
      color: #14cd70;
    }
    &.is-disabled {
      background-color: transparent;
      border-color: #14cd70;
      span {
        color: #14cd70;
      }
      &:hover {
        background-color: transparent;
        border-color: #14cd70;
        span {
          color: #14cd70;
        }
      }
    }
    &:hover {
      background-color: #14cd70;
      border-color: #14cd70;
      span {
        color: white;
      }
    }
  }
}

.Y-button--primary {
  background-color: #3069ff;
  border-color: #3069ff;
  span {
    color: #fff;
  }

  &:hover {
    background: #6e96ff;
    border-color: #6e96ff;
    color: #fff;
  }
  &.is-disabled {
    &,
    &:hover,
    &:focus {
      background-color: #98b4ff;
      border-color: #98b4ff;
      span {
        color: #fff;
      }
      cursor: not-allowed;
    }
  }
  &.is-text {
    border: none;
    background-color: transparent;
    span {
      color: #3069ff;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #6e96ff;
      }
    }
  }
  &.is-link {
    text-decoration-color: #3069ff;
    span {
      color: #3069ff;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #98b4ff;
        text-decoration-color: #98b4ff;
      }
    }
  }
  &.is-border {
    background-color: transparent;
    span {
      color: #3069ff;
    }
    &.is-disabled {
      background-color: transparent;
      border-color: #3069ff;
      span {
        color: #3069ff;
      }
      &:hover {
        background-color: transparent;
        border-color: #3069ff;
        span {
          color: #3069ff;
        }
      }
    }
    &:hover {
      background-color: #3069ff;
      border-color: #3069ff;
      span {
        color: white;
      }
    }
  }
}

.Y-button--warning {
  background-color: #ffa81a;
  border-color: #ffa81a;
  span {
    color: #fff;
  }

  &:hover {
    background: #ffc25f;
    border-color: #ffc25f;
    span {
      color: #fff;
    }
  }
  &.is-disabled {
    &,
    &:hover,
    &:focus {
      background-color: #ffd48d;
      border-color: #ffd48d;
      span {
        color: #fff;
      }
      cursor: not-allowed;
    }
  }
  &.is-text {
    border: none;
    background-color: transparent;
    span {
      color: #ffa81a;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #ffd48d;
      }
    }
  }
  &.is-link {
    text-decoration-color: #ffa81a;
    span {
      color: #ffa81a;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #ffd48d;
        text-decoration-color: #ffd48d;
      }
    }
  }
  &.is-border {
    background-color: transparent;
    span {
      color: #ffa81a;
    }
    &.is-disabled {
      background-color: transparent;
      border-color: #ffa81a;
      span {
        color: #ffa81a;
      }
      &:hover {
        background-color: transparent;
        border-color: #ffa81a;
        span {
          color: #ffa81a;
        }
      }
    }
    &:hover {
      background-color: #ffa81a;
      border-color: #ffa81a;
      span {
        color: white;
      }
    }
  }
}
.Y-button--danger {
  background-color: #ff4a5b;
  border-color: #ff4a5b;
  span {
    color: #fff;
  }

  &:hover {
    background: #ff808c;
    border-color: #ff808c;
    span {
      color: #fff;
    }
  }
  &.is-disabled {
    &,
    &:hover,
    &:focus {
      background-color: #ffa5ad;
      border-color: #ffa5ad;
      span {
        color: #fff;
      }
      cursor: not-allowed;
    }
  }
  &.is-text {
    border: none;
    background-color: transparent;
    span {
      color: #ff4a5b;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #ff808c;
      }
    }
  }
  &.is-link {
    text-decoration-color: #ff4a5b;
    span {
      color: #ff4a5b;
    }
    &.is-disabled,
    &:hover {
      background-color: transparent;
      span {
        color: #ffa5ad;
        text-decoration-color: #ffa5ad;
      }
    }
  }
  &.is-border {
    background-color: transparent;
    span {
      color: #ff4a5b;
    }
    &.is-disabled {
      background-color: transparent;
      border-color: #ff4a5b;
      span {
        color: #ff4a5b;
      }
      &:hover {
        background-color: transparent;
        border-color: #ff4a5b;
        span {
          color: #ff4a5b;
        }
      }
    }
    &:hover {
      background-color: #ff4a5b;
      border-color: #ff4a5b;
      span {
        color: white;
      }
    }
  }
}

Button组件的Props类型定义

  • 完成了round、text、link、block、border、circle、size、disabled、type 的定义
vue
<template>
  <div>
    <button
      :class="[
        ns.b(),
        ns.m(props.type),
        ns.m('size', props.size),
        ns.is('round', props.round),
        ns.is('disabled', props.disabled),
        ns.is('text', props.text),
        ns.is('link', props.link),
        ns.is('block', props.block),
        ns.is('border', props.border),
        ns.is('circle', props.circle),
      ]"
      :disabled="disabled"
      @click="handleClick"
    >
      <span>测试1</span>
    </button>

    <!-- <button :class="[ns.b(), ns.e('button'), ns.m('size', props.size)]" @click="handleClick">
      <span> {{ name }}</span>
    </button> -->
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import { useButton, useNamespace } from "../../../hooks/index";

const props = defineProps({
  type: {
    type: String,
    default: "",
  },
  text: {
    type: Boolean,
    default: false,
  },
  round: {
    type: Boolean,
    default: false,
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  link: {
    type: Boolean,
    default: false,
  },
  block: {
    type: Boolean,
    default: false,
  },
  border: {
    type: Boolean,
    default: false,
  },
  size: {
    type: String,
    default: "", // 小small,中 就不用写 大large
  },
  circle: {
    type: Boolean,
    default: false,
  },
});

const ns = useNamespace("button");

console.log("useNameSpace", ns.b());

const { handleClick } = useButton();
onMounted(() => {
  console.log("按钮组件挂载");
});
</script>

pages里面引用

  • pages/chooseButton/index.vue
vue
<template>
  <div class="pagecontent">
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      Button按钮
    </div>
    <YButton></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success"></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary"></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger"></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" round></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      Button按钮text
    </div>
    <YButton text></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" text></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" text></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" text></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" text></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      Button按钮disabled
    </div>
    <YButton disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      Button按钮text下面的disabled
    </div>
    <YButton text disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" text disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" text disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" text disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" text disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      Button按钮LINK
    </div>
    <YButton link></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" link></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" link></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" link></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" link></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      Button按钮LINK下面的disabled
    </div>
    <YButton link disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" link disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" link disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" link disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" link disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      按钮变成block
    </div>
    <YButton block></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" block></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" block></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" block></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" block></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      边框按钮
    </div>
    <YButton border></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" border></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" border></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" border></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" border></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      边框按钮disabled
    </div>
    <YButton border disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="primary" border disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="success" border disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="danger" border disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton type="warning" border disabled></YButton>
    <div style="width: 100%; height: 15px"></div>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      尺寸大小size
    </div>
    <YButton size="large" type="primary"></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton size="small"></YButton>
    <div
      style="width: 100%; height: 15px; background: #ccc; font-size: 15px; margin-bottom: 15px"
    >
      circle圆形
    </div>
    <YButton circle type="primary"></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton circle></YButton>
    <div style="width: 100%; height: 15px"></div>
    <YButton circle size="large"></YButton>
  </div>
</template>

<script setup lang="ts"></script>

<style lang="scss" scoped>
.pagecontent {
  padding: 15px 15px;
}
</style>