Button 按钮
执行操作或提交表单。
基础用法
使用 type 指定语义颜色,使用 variant 选择实体、轻表面、文本或链接样式
查看代码收起代码
vue
<script setup lang="ts">
import type { ButtonType, ButtonVariant } from '@codex-proxy/ui'
import { ZButton } from '@codex-proxy/ui'
const types: Record<ButtonType, string> = {
default: '默认',
primary: '主要',
success: '成功',
info: '信息',
warning: '警告',
danger: '危险',
}
const variants: ButtonVariant[] = ['solid', 'plain', 'text', 'link']
</script>
<template>
<div v-for="variant in variants" :key="variant" class="button-row">
<ZButton v-for="(label, type) in types" :key="type" :type="type" :variant="variant">
{{ label }}
</ZButton>
</div>
</template>
<style scoped>
.button-row {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
</style>图标按钮
图标配文字使用 ZButton 的 icon 插槽。只有图标时使用 ZIconButton,用 aria-label 命名操作
查看代码收起代码
vue
<script setup lang="ts">
import { ZIconButton } from '@codex-proxy/ui'
import { ref } from 'vue'
const count = ref(0)
</script>
<template>
<ZIconButton aria-label="刷新配置" @click="count++">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
<path d="M20 7v5h-5M4 17v-5h5M6.1 6.1A8 8 0 0 1 20 12M4 12a8 8 0 0 0 13.9 5.9" />
</svg>
</ZIconButton>
<output aria-label="刷新次数">{{ count }}</output>
</template>禁用状态
设置 disabled 禁用按钮,禁用时不触发点击事件
查看代码收起代码
vue
<script setup lang="ts">
import type { ButtonType } from '@codex-proxy/ui'
import { ZButton } from '@codex-proxy/ui'
const types: Record<ButtonType, string> = {
default: '默认',
primary: '主要',
success: '成功',
info: '信息',
warning: '警告',
danger: '危险',
}
</script>
<template>
<div style="display: flex; flex-wrap: wrap; gap: 12px">
<ZButton v-for="(label, type) in types" :key="type" :type="type" disabled>
{{ label }}
</ZButton>
</div>
</template>加载状态
操作执行期间设置 loading,显示加载图标并阻止重复点击
查看代码收起代码
vue
<script setup lang="ts">
import { ZButton } from '@codex-proxy/ui'
</script>
<template>
<div style="display: flex; gap: 12px">
<ZButton type="primary" loading>
保存中
</ZButton>
<ZButton variant="plain" loading>
加载中
</ZButton>
</div>
</template>按钮尺寸
使用 size 选择小、默认或大尺寸
查看代码收起代码
vue
<script setup lang="ts">
import { ZButton } from '@codex-proxy/ui'
</script>
<template>
<div style="display: flex; align-items: center; gap: 12px">
<ZButton size="small" type="primary">
小尺寸
</ZButton>
<ZButton type="primary">
中尺寸
</ZButton>
<ZButton size="large" type="primary">
大尺寸
</ZButton>
</div>
</template>Button API
属性
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
type | 语义类型 | 'default' | 'primary' | 'success' | 'info' | 'warning' | 'danger' | 'default' |
variant | 外观 | 'solid' | 'plain' | 'text' | 'link' | 'solid' |
size | 组件尺寸 | 'small' | 'default' | 'large' | 'default' |
native-type | 原生按钮行为 | 'button' | 'submit' | 'reset' | 'button' |
loading / disabled | 加载与禁用均阻止点击 | boolean | false |
事件
click(event: MouseEvent):用户点击,禁用或加载时不触发
插槽
default、icon、loading
实例
focus()、blur()、element(原生按钮)
IconButton API
尺寸、语义类型、原生按钮行为、加载与禁用、点击事件及实例方法与 Button 相同。图标放在默认插槽,加载图标可用 loading 插槽替换
| 属性 | 说明 | 默认值 |
|---|---|---|
aria-label | 按钮的可访问名称 | — |
variant | 与 Button 相同的外观选项 | 'text' |
pressed | 切换按钮的布尔状态,普通按钮省略 | — |