button组件几乎是每个组件库都有的;其实实现一个button组件是很简单的。本篇文章将带你一步一步的实现一个button组件。如果你想了解完整的组件库搭建,你可以先看使用Vite和TypeScript带你从零打造一个属于自己的Vue3组件库,这篇文章有详细介绍。当然如果你只想知道一个button组件如何开发出来的,只看这篇也就够了。(样式部分参造了elementui组件库)

首先我们先看下我们这个button组件要实现的功能

  • 使用type,plain属性来定义按钮基本样式
  • round,size控制按钮形状大小
  • 通过disabled来控制按钮是否可点击
  • 支持icon加入图标增强辨识度

type实现

我们的type可以传入的值可以是primary, success, info,warning, danger分别展示不同按钮颜色,type传入text显示文字按钮(没有边框和背景色的按钮)

这里只展示了一个primary的样式,因为其它值的样式实现是一样的。需要的话可以到button组件样式进行查看。

所以在button/types.ts文件中我们定义一下type的类型:


import { ExtractPropTypes } from 'vue' export const ButtonType = ['primary', 'success', 'info', 'warning', 'danger','text'] export const buttonProps = {
type: {
type: String,
validator(value: string) {
//这里表示type只能接收这些值
return ButtonType.includes(value)
}
}
} export type ButtonProps = ExtractPropTypes<typeof buttonProps>

接下来在button.vue中实现传入不同值赋予不同类名,从而实现显示不同效果。

<template>
<button class="k-button" :class="styleClass">
<slot />
</button>
</template> <script lang="ts">
import './style/index.less'
import { defineComponent, computed } from 'vue'
import { buttonProps } from './types'
export default defineComponent({
name: 'k-button',
props: buttonProps,
setup(props) {
const styleClass = computed(() => {
return {
[`k-button--${props.type}`]: props.type
}
}) return {
styleClass
};
},
});
</script>

这样一来传入primary组件就会有个类名k-button--primary吗,传入其它值也一样。然后我们就可以给它们写样式了。进入style/index.less:

.k-button {
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
background: #fff;
border: 1px solid #dcdfe6;
color: #606266;
-webkit-appearance: none;
text-align: center;
box-sizing: border-box;
outline: none;
margin: 0;
transition: 0.1s;
font-weight: 500;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
&:hover {
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
}
.k-button--primary {
color: #fff;
background-color: #409eff;
border-color: #409eff;
&:hover {
background: #66b1ff;
border-color: #66b1ff;
color: #fff;
}
}
.k-button--text {
border-color: transparent;
color: #409eff;
background: transparent;
padding-left: 0;
padding-right: 0;
&:hover {
color: #66b1ff;
border-color: transparent;
background-color: transparent;
}
}

plain(朴素按钮)和round(圆角按钮)

我们可以通过传入plain和round来决定这个按钮是否为朴素按钮和圆角按钮,很显然它们是个布尔类型

//types.ts

...

export const buttonProps = {
type: {
type: String,
validator(value: string) {
return ButtonType.includes(value)
}
},
plain: Boolean,
round: Boolean
} ...

然后在button.vue定义我们的styleClass

//button.vue

const styleClass = computed(() => {
return {
[`k-button--${props.type}`]: props.type,
'is-plain': props.plain,
'is-round': props.round
}
})

最后在style/index.less写上我们的样式


... .k-button--primary.is-plain {
color: #409eff;
background: #ecf5ff;
border-color: #b3d8ff;
&:hover {
color: #fff;
background-color: #409eff;
border-color: #409eff;
}
}
.is-round {
border-radius: 20px;
}

注意

两个连在一起的类名如:.k-button--primary.is-plain 代表一个元素只有包含这两个类名,定义的样式才生效

禁用按钮

同样的,在types.ts定义disabled的类型

//types.ts

...
export const ButtonType = ['primary', 'success', 'info', 'warning', 'danger'] export const buttonProps = {
type: {
type: String,
validator(value: string) {
return ButtonType.includes(value)
}
},
plain: Boolean,
round: Boolean,
disabled: Boolean
} ...

然后在button.vue定义我们的styleClass

//button.vue

const styleClass = computed(() => {
return {
[`k-button--${props.type}`]: props.type,
'is-plain': props.plain,
'is-round': props.round,
'is-disabled': props.disabled
}
})

最后添加上我们的样式

...
.k-button.is-disabled {
color: #c0c4cc;
cursor: not-allowed;
background-image: none;
background-color: #fff;
border-color: #ebeef5;
}
.k-button--primary.is-disabled,
.k-button--primary.is-disabled:active,
.k-button--primary.is-disabled:focus,
.k-button--primary.is-disabled:hover {
color: #fff;
background-color: #a0cfff;
border-color: #a0cfff;
}
//朴素按钮禁用
.k-button--primary.is-disabled.is-plain,
.k-button--primary.is-disabled.is-plain:active,
.k-button--primary.is-disabled.is-plain:focus,
.k-button--primary.is-disabled.is-plain:hover {
color: #8cc5ff;
background-color: #ecf5ff;
border-color: #d9ecff;
}
...

size

通过size我们可以控制按钮的大小,组件接收的size值有:midium, small, mini。实现方式和上面差不多,这里就直接展示部分代码了

  • types.ts
//types.ts

...

export const ButtonSize = ['midium', 'small', 'mini'];

export const buttonProps = {
type: {
type: String,
validator(value: string) {
return ButtonSize.includes(value)
}
},
plain: Boolean,
round: Boolean,
disabled: Boolean
} ...
  • button.vue
//button.vue

const styleClass = computed(() => {
return {
[`k-button--${props.type}`]: props.type,
'is-plain': props.plain,
'is-round': props.round,
'is-disabled': props.disabled,
[`k-button--${props.size}`]: props.size,
}
})
  • style/index.less
//index.less
...
.k-button--medium {
padding: 10px 20px;
font-size: 14px;
border-radius: 4px;
} .k-button--small {
padding: 9px 15px;
font-size: 12px;
border-radius: 3px;
} .k-button--mini {
padding: 7px 15px;
font-size: 12px;
border-radius: 3px;
}

最后我们在项目中引用(examples)来查看效果;

<-- App.vue -->
<template>
<div>
<Button>默认按钮</Button>
<Button type="primary">主要按钮</Button>
<Button type="success">成功按钮</Button>
<Button type="info">信息按钮</Button>
<Button type="warning">警告按钮</Button>
<Button type="danger">危险按钮</Button>
<Button type="text">文字按钮</Button>
<br>
<br>
<Button plain>朴素按钮</Button>
<Button type="primary" plain>主要按钮</Button>
<Button type="success" plain>成功按钮</Button>
<Button type="info" plain>信息按钮</Button>
<Button type="warning" plain>警告按钮</Button>
<Button type="danger" plain>危险按钮</Button>
<br>
<br>
<Button round>圆角按钮</Button>
<Button type="primary" round>主要按钮</Button>
<Button type="success" round>成功按钮</Button>
<Button type="info" round>信息按钮</Button>
<Button type="warning" round>警告按钮</Button>
<Button type="danger" round>危险按钮</Button>
<br>
<br>
<Button disabled>禁用按钮</Button>
<Button type="primary" disabled>主要按钮</Button>
<Button type="success" disabled>成功按钮</Button>
<Button type="info" disabled>信息按钮</Button>
<Button type="warning" disabled>警告按钮</Button>
<Button type="danger" disabled>危险按钮</Button>
<br>
<br>
<Button disabled>禁用按钮</Button>
<Button type="primary" disabled plain>主要按钮</Button>
<Button type="success" disabled plain>成功按钮</Button>
<Button type="info" disabled plain>信息按钮</Button>
<Button type="warning" disabled plain>警告按钮</Button>
<Button type="danger" disabled plain>危险按钮</Button>
<br>
<br>
<Button>默认按钮</Button>
<Button size="medium">中等按钮</Button>
<Button size="small">小型按钮</Button>
<Button size="mini">超小按钮</Button>
</div>
</template>
<script lang="ts" setup>
import { Button } from 'kitty-ui'
</script>
<style lang="less">
.k-button {
margin-right: 10px;
}
</style>

启动项目我们就会在浏览器中看到我们的各式各样的button组件了

图标

通过icon属性设置按钮图标,支持Icon组件里的所有图标(Icon组件下一篇文章将会详细介绍)。

  • types.ts中设置icon类型
export const buttonProps = {
type: {
type: String,
validator(value: string) {
return ButtonType.includes(value)
}
},
plain: Boolean,
round: Boolean,
disabled: Boolean,
icon: String,
iconPosition: String,
size: {
type: String,
validator(value: string) {
return ButtonSize.includes(value)
}
}
}
  • 修改button组件

icon可以传入icon组件中定义的name,iconPosition可选right使图标在按钮右侧。

<!-- button.vue -->
<template>
<button class="k-button" :class="styleClass">
<Icon class="icon" v-if="iconFont.iconName && iconFont.iconPosition != 'right'" :name="iconFont.iconName" />
<slot />
<Icon class="icon" v-if="iconFont.iconPosition == 'right' && iconFont.iconName" :name="iconFont.iconName" />
</button>
</template> <script lang="ts">
import './style/index.less'
import { defineComponent, computed } from 'vue'
import { buttonProps } from './types'
import Icon from '../Icon/icon.vue'
export default defineComponent({
name: 'k-button',
props: buttonProps,
components: { Icon },
setup(props) { const styleClass = computed(() => {
return {
[`k-button--${props.type}`]: props.type,
'is-plain': props.plain,
'is-round': props.round,
'is-disabled': props.disabled,
[`k-button--${props.size}`]: props.size,
}
}) //图标
const iconFont = computed(() => {
const iconName = props.icon
const position = props.iconPosition
return {
iconName,
iconPosition
}
}) return {
styleClass,
Icon,
iconFont
};
},
});
</script>

然后在examples/App.vue使用并查看效果

<template>
<div>
<Button type="success" icon="edit">图标按钮</Button>
<Button type="primary" icon="map" icon-position="right">图标按钮</Button>
<Button type="primary" icon="ashbin"></Button>
</div>
</template>
<script lang="ts" setup>
import { Button } from 'kitty-ui' </script>
<style lang="less">
.k-button {
margin-right: 10px;
}
</style>

最后

到这里一个button组件的开发基本就结束了,看起来一个不起眼的button组件里面其实还是包含些许内容的。关注我将不定期更新其它组件的实现。

如果你觉得本篇文章对你有帮助的话,动动指头点个赞吧orz,你的鼓励将会是我持续创作的动力。

从0搭建Vue3组件库:button组件的更多相关文章

  1. 从0搭建vue3组件库:Shake抖动组件

    先看下效果 其实就是个抖动效果组件,实现起来也非常简单.之所以做这样一个组件是为了后面写Form表单的时候会用到它做一个规则校验,比如下面一个简单的登录页面,当点击登录会提示用户哪个信息没输入,当然这 ...

  2. 从0搭建vue3组件库: 如何完整搭建一个前端脚手架?

    相信大家在前端开发中都使用过很多前端脚手架,如vue-cli,create-vite,create-vue等:本篇文章将会为大家详细介绍这些前端脚手架是如何实现的,并且从零实现一个create-kit ...

  3. 从0搭建vue3组件库:自动化发布、管理版本号、生成 changelog、tag

    今天看到一篇文章中提到了一个好用的工具release-it.刚好可以用在我正在开发的vue3组件库.纸上得来终觉浅,绝知此事要躬行,说干就干,下面就介绍如何将release-it应用到实际项目中,让组 ...

  4. 从0搭建vue3组件库: Input组件

    本篇文章将为我们的组件库添加一个新成员:Input组件.其中Input组件要实现的功能有: 基础用法 禁用状态 尺寸大小 输入长度 可清空 密码框 带Icon的输入框 文本域 自适应文本高度的文本域 ...

  5. 05 - Vue3 UI Framework - Button 组件

    官网基本做好了,接下来开始做核心组件 返回阅读列表点击 这里 目录准备 在项目 src 目录下创建 lib 文件夹,用来存放所有的核心组件吧.然后再在 lib 文件夹下创建 Button.vue 文件 ...

  6. Sprite组件和Button组件的使用

    一.Sprint组件的使用 1.游戏中显示一张图片,通常我们称之为"精灵" sprite 2.cocos creator如果需要显示一个图片,那么需要在节点上挂一个精灵组件,为这个 ...

  7. antd组件库BackTop组件设置动态背景图片的问题

    有这么一个需求,利用antd组件库中的BackTop组件的逻辑,但是自己写样式. 我的目标样式是:有两张图片,一张是normal(正常情况),一张是hover(悬停情况). 这时候就要用到css的动画 ...

  8. Blazor Bootstrap 组件库语音组件介绍

    Speech 语音识别与合成 通过麦克风语音采集转换为文字(STT),或者通过文字通过语音朗读出来(TTS) 本组件依赖于 BootstrapBlazor.AzureSpeech,使用本组件时需要引用 ...

  9. Vitepress搭建组件库文档(下)—— 组件 Demo

    上文 <Vitepress搭建组件库文档(上)-- 基本配置>已经讨论了 vitepress 搭建组件库文档的基本配置,包括站点 Logo.名称.首页 home 布局.顶部导航.左侧导航等 ...

随机推荐

  1. [WUSTCTF2020]颜值成绩查询-1

    分享下自己在完成[WUSTCTF2020]颜值成绩查询-1关卡的手工过程和自动化脚本. 1.通过payload:1,payload:1 ,payload:1 or 1=1--+,进行判断是否存在注入, ...

  2. SAP -熟练使用T-Code SHD0

    SHD0 业务顾问和开发顾问都非常熟悉的一个T-Code, 如果能合理使用它,可以省去许多增强和程序修改工作. 当我需要时,我在这里找不到任何相关文档,这就是为什么我想借此机会向我们自己的SCN提供内 ...

  3. SAP Using Text Modules in Adobe Forms

    In this demo we will create an adobe form which displays text in two different languages (English or ...

  4. NC25136 [USACO 2006 Ope B]Cows on a Leash

    NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...

  5. Overfitting & Train Set & Test Set

    假设数据集是独立同分布的,可以将数据集划分为不同的比例:Train Set and Test Set. 同时在Train Set and Test Set上做精度测试,或者隔一段时间在Test Set ...

  6. IDEA的概述和IDEA的安装

    开发工具概述 IDEA是一个专门针对Java的集成开发工具(IDE),由Java语言编写.所以,需要有JRE运行环境并配置好环境变量. 它可以极大地提升我们的开发效率.可以自动编译,检查错误.在公司中 ...

  7. java后端接受Vue传递的List

    Failed to resolve argument 1 of type 'java.util.List' org.springframework.web.bind.MissingServletReq ...

  8. Linux串口编程进阶

    在<Linux串口编程>编程一文中介绍了串口应用中常用的基本操作,如:串口打开关闭.串口设置.数据收发等.本篇文章主要基于常规串口操作进行了扩充,主要介绍如下操作: Linux系统使用非标 ...

  9. 如何应对外包公司(文思海辉)的Python后端面试

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_151 最近春招如火如荼,我接触到的几位同学也纷纷去市场里试了试水,不得不说由于疫情的影响,导致目前中等水平的开发者择业有了些许困难 ...

  10. Nginx 文件传输效率、实时、压缩配置指令

    # sendfile 开启文件高效传输模式 # 默认值:off # 位置:http.servcer.location-- # 开启和不开启worker访问的文件发送到浏览器的过程不同. # 不开启的时 ...