效果图
代码
- <script lang="ts" setup>
- import { computed, ref, watch } from "vue";
- /**
- * 按钮属性接口
- */
- interface ButtonProps {
- /** 按钮类型:default(默认)/dark/plain/link */
- type?: "default" | "dark" | "plain" | "link";
- /** 禁用状态 */
- disabled?: boolean;
- /** 按钮内容 */
- text?: string;
- /** 图标组件 */
- icon?: any;
- /** 图标宽度 */
- iconWidth?: number | string;
- /** 图标高度 */
- iconHeight?: number | string;
- /** 按钮宽度 */
- width?: number | string;
- /** 按钮高度 */
- height?: number | string;
- /** 宽度自适应 */
- widthAuto?: boolean;
- /** 高度自适应 */
- heightAuto?: boolean;
- /** 水平内边距 */
- paddingX?: number | string;
- /** 垂直内边距 */
- paddingY?: number | string;
- /** 自定义背景色 */
- backgroundColor?: string;
- /** 自定义边框 */
- border?: string;
- }
- const props = withDefaults(defineProps<ButtonProps>(), {
- type: "default",
- disabled: false,
- text: "",
- icon: undefined,
- iconWidth: 14,
- iconHeight: 14,
- width: 92,
- height: 32,
- widthAuto: false,
- heightAuto: false,
- paddingX: 0,
- paddingY: 0,
- backgroundColor: "",
- border: ""
- });
- /**
- * 导入图标
- */
- const iconSrc = ref("");
- /**
- * 处理图标路径
- */
- const loadIcon = () => {
- if (!props.icon) {
- iconSrc.value = "";
- return;
- }
- // 直接使用传入的图标路径或对象
- iconSrc.value = props.icon;
- };
- // 初始加载图标
- loadIcon();
- // 监听图标属性变化,重新加载图标
- watch(
- () => props.icon,
- () => {
- loadIcon();
- }
- );
- // 计算样式
- const buttonClass = computed(() => {
- return {
- "ds-button": true,
- [`ds-button--${props.type}`]: true,
- "is-disabled": props.disabled,
- "is-custom-style": props.backgroundColor || props.border
- };
- });
- // 计算图标样式
- const iconStyle = computed(() => {
- return {
- width:
- typeof props.iconWidth === "number"
- ? `${props.iconWidth}px`
- : props.iconWidth,
- height:
- typeof props.iconHeight === "number"
- ? `${props.iconHeight}px`
- : props.iconHeight,
- marginRight: props.text ? "4px" : "0"
- };
- });
- // 计算按钮样式
- const buttonStyle = computed(() => {
- const paddingXValue = typeof props.paddingX === "number" ? `${props.paddingX}px` : props.paddingX;
- const paddingYValue = typeof props.paddingY === "number" ? `${props.paddingY}px` : props.paddingY;
-
- const style = {
- width: props.widthAuto ? "auto" : (typeof props.width === "number" ? `${props.width}px` : props.width),
- height: props.heightAuto ? "auto" : (typeof props.height === "number" ? `${props.height}px` : props.height),
- padding: props.paddingX || props.paddingY ? `${paddingYValue} ${paddingXValue}` : "",
- boxSizing: "border-box" as const
- };
-
- // 添加自定义背景色 - 支持所有CSS颜色值(包括rgba、hex、颜色名称、渐变等)
- if (props.backgroundColor && !props.disabled) {
- style["background"] = props.backgroundColor;
- }
-
- // 添加自定义边框
- if (props.border) {
- style["border"] = props.border;
- }
-
- return style;
- });
- const emit = defineEmits(["click"]);
- /**
- * 按钮点击事件处理
- * @param event 鼠标事件
- */
- const handleClick = (event: MouseEvent) => {
- if (props.disabled) return;
- emit("click", event);
- };
- </script>
- <template>
- <button
- :class="buttonClass"
- :style="buttonStyle"
- :disabled="disabled"
- @click="handleClick"
- >
- <img v-if="icon" :src="iconSrc" :style="iconStyle" />
- <span v-if="text">{{ text }}</span>
- <slot v-else />
- </button>
- </template>
- <style lang="scss" scoped>
- .ds-button {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 4px 16px;
- font-size: 14px;
- font-weight: 600;
- text-align: center;
- cursor: pointer;
- border: none;
- border-radius: 4px;
- outline: none;
- transition: all 0.3s;
- &.is-custom-style {
- &:hover:not(.is-disabled) {
- opacity: 0.9;
- }
- &:active:not(.is-disabled) {
- opacity: 0.8;
- }
- &.is-disabled {
- opacity: 0.5;
- cursor: not-allowed;
- }
- }
- &--default {
- color: #fff;
- background: #06ad7e;
- &:hover:not(.is-disabled) {
- background: #07ca93;
- }
- &:active:not(.is-disabled) {
- background: #008661;
- }
- &.is-disabled {
- color: rgb(255 255 255 / 50%);
- cursor: not-allowed;
- background: rgb(6 173 126 / 50%);
- }
- }
- &--dark {
- color: #06ad7e;
- background: rgb(240 251 248 / 10%);
- &:hover:not(.is-disabled) {
- background: rgb(240 251 248 / 15%);
- }
- &:active:not(.is-disabled) {
- color: #008661;
- background: rgb(255 255 255 / 2%);
- }
- &.is-disabled {
- color: rgb(6 173 126 / 50%);
- cursor: not-allowed;
- background: rgb(240 251 248 / 5%);
- }
- }
- &--plain {
- color: #06ad7e;
- background: transparent;
- &:hover:not(.is-disabled) {
- background: rgb(255 255 255 / 5%);
- }
- &:active:not(.is-disabled) {
- color: #008661;
- background: rgb(255 255 255 / 2%);
- }
- &.is-disabled {
- color: rgb(6 173 126 / 50%);
- cursor: not-allowed;
- }
- }
- &--link {
- color: #06ad7e;
- background: transparent;
- padding: 4px 0;
- &:hover:not(.is-disabled) {
- color: #07ca93;
- background: transparent;
- }
- &:active:not(.is-disabled) {
- color: #008661;
- background: transparent;
- }
- &.is-disabled {
- color: rgb(6 173 126 / 50%);
- cursor: not-allowed;
- background: transparent;
- }
- }
- }
- </style>
复制代码 文档
DsButton 按钮组件
一个自界说按钮组件,提供多种样式范例和状态,实用于体系中的各种交互场景。
功能特点
- 支持四种样式范例:默认(default)、暗色(dark)、朴素(plain)和链接(link)
- 可自界说按钮尺寸、内容和图标
- 支持自界说配景色(支持rgba、渐变等全部CSS配景值)和边框样式
- 支持禁用状态
- 支持宽高自顺应和自界说内边距
- 响应式筹划,顺应差别场景
组件API
属性
属性名阐明范例可选值默认值type按钮范例stringdefault / dark / plain / linkdefaultdisabled禁用状态booleantrue / falsefalsetext按钮文本内容string-‘’icon图标路径string-undefinediconWidth图标宽度number / string-14iconHeight图标高度number / string-14width按钮宽度number / string-92height按钮高度number / string-32widthAuto宽度自顺应booleantrue / falsefalseheightAuto高度自顺应booleantrue / falsefalsepaddingX水平内边距number / string-0paddingY垂直内边距number / string-0backgroundColor自界说配景色string全部CSS颜色值/渐变‘’border自界说边框stringCSS边框值‘’变乱
变乱名阐明回调参数click点击按钮时触发event: MouseEvent使用示例
底子用法
- <template>
- <ds-button text="默认按钮" />
- <ds-button type="dark" text="暗色按钮" />
- <ds-button type="plain" text="朴素按钮" />
- <ds-button type="link" text="链接按钮" />
- </template>
复制代码 禁用状态
- <template>
- <ds-button text="默认按钮" disabled />
- <ds-button type="dark" text="暗色按钮" disabled />
- <ds-button type="plain" text="朴素按钮" disabled />
- <ds-button type="link" text="链接按钮" disabled />
- </template>
复制代码 带图标的按钮
- <template>
- <ds-button
- text="添加"
- icon="/src/assets/icons/add.png"
- />
-
- <ds-button
- type="plain"
- text="删除"
- icon="/src/assets/icons/delete.png"
- icon-width="16"
- icon-height="16"
- />
-
- <ds-button
- type="link"
- text="链接"
- icon="/src/assets/icons/link.png"
- />
- </template>
复制代码 自界说配景色和边框
- <template>
- <!-- 自定义背景色 - 十六进制 -->
- <ds-button
- text="蓝色按钮"
- background-color="#3a7afe"
- />
-
- <!-- 自定义背景色 - rgba -->
- <ds-button
- text="半透明按钮"
- background-color="rgba(58, 122, 254, 0.7)"
- />
-
- <!-- 自定义背景色 - 渐变 -->
- <ds-button
- text="渐变按钮"
- background-color="linear-gradient(90deg, #3a7afe, #6c5ce7)"
- width="120"
- />
-
- <!-- 自定义边框 -->
- <ds-button
- text="带边框按钮"
- type="plain"
- border="1px solid #34564c"
- />
-
- <!-- 背景色和边框都自定义 -->
- <ds-button
- text="自定义样式"
- background-color="rgba(255, 103, 0, 0.8)"
- border="1px solid #ff6700"
- />
- </template>
复制代码 自界说尺寸
- <template>
- <ds-button text="小按钮" width="80" height="28" />
- <ds-button text="中等按钮" width="100" height="36" />
- <ds-button text="大按钮" width="120" height="44" />
- </template>
复制代码 宽高自顺应
- <template>
- <ds-button text="宽度自适应" width-auto />
- <ds-button text="高度自适应" height-auto />
- <ds-button text="宽高都自适应" width-auto height-auto />
- </template>
复制代码 自界说内边距
- <template>
- <ds-button text="自定义内边距" padding-x="20" padding-y="10" />
- <ds-button
- type="dark"
- text="更大的内边距"
- padding-x="30"
- padding-y="15"
- width-auto
- />
- </template>
复制代码 使用插槽
除了使用text属性设置按钮内容外,还可以使用默认插槽:- <template>
- <ds-button>
- <span style="color: yellow">自定义内容</span>
- </ds-button>
- </template>
复制代码 样式定制
按钮具有四种根本范例,每种范例有差别的样式:
- default: 绿色配景,白色笔墨
- 悬停时配景色变亮
- 按下时配景色变暗
- 禁用时透明度低沉
- dark: 半透明配景,绿色笔墨
- 悬停时配景色变亮
- 按下时笔墨颜色变暗
- 禁用时透明度低沉
- plain: 透明配景,绿色笔墨
- 悬停时有稍微配景色
- 按下时笔墨颜色变暗
- 禁用时透明度低沉
- link: 透明配景,绿色笔墨,无边框和内边距
- 悬停时笔墨颜色变亮,无配景色变化
- 按下时笔墨颜色变暗,无配景色变化
- 禁用时透明度低沉,鼠标样式为禁用状态
自界说样式
除了预设的四种范例,还可以通过以部属性自界说按钮样式:
- backgroundColor: 设置按钮的配景,支持全部CSS配景属性值:
- 十六进制颜色:#3a7afe
- RGB/RGBA颜色:rgb(58, 122, 254) 或 rgba(58, 122, 254, 0.7)
- 颜色名称:blue、red 等
- 渐变:linear-gradient(90deg, #3a7afe, #6c5ce7)
- 其他CSS配景值
- border: 设置按钮的边框样式,可使用任何有效的CSS边框值
当使用自界说配景色或边框时,按钮的悬停和激活效果会变为透明度变化。
全局注册
该组件已通过index.ts设置为可全局注册:- import DsButton from '@/components/DsButton';
- app.use(DsButton);
复制代码 注册后,可在任何组件中直接使用<ds-button>标签,无需单独导入。
注册入口
- import type { App } from "vue";
- import Button from "./index.vue";
- /**
- * 注册按钮组件
- */
- export default {
- install(app: App) {
- app.component("DsButton", Button);
- }
- };
- export { Button as DsButton };
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|