Button
按钮会触发响应单击的操作。
何时使用
按钮触发操作:保存数据、打开窗口、添加记录或提交表单。标题应解释单击的结果:“保存更改”比“确定”更清晰。
- 主按钮突出显示主要操作。在一个操作组中,一个主按钮通常就足够了。
- 常规按钮适用于等效操作;文本 - 用于次要的。
- 虚线按钮可以方便地用于添加元素。
- 要转到另一个页面,请传递
href:该组件将创建一个真实的链接。如果用户选择其中一个值,请考虑 Radio 或 Select。
按钮 CSS 前缀 显示全局 ConfigProvider.prefixCls 和本地 prefixCls,无需重置状态。本地 prefixCls 是按钮的完整前缀,不会自动添加 -btn。按钮容器和内部变量使用提供者的公共前缀。
如何选择 的设计
从动作角色开始,然后选择设计。 type指定一个现成的选项; color和variant允许您分别自定义颜色和显示方式。
| 问题 | 设置 | 工作示例 |
|---|---|---|
| 主要动作 | type="primary" |
按钮类型 |
| 等效动作 | type="default" |
多个按钮 |
| 添加元素 | type="dashed" |
按钮类型 |
| 次要行动 | type="text" |
按钮类型 |
| 自定义颜色和填充 | color 和 variant |
颜色和选项 |
type="primary" 对应于组合 color="primary" variant="solid"。显式 color 和 variant 优先。对于删除和其他危险操作,请使用danger;如果同时指定color,请考虑其优先级。
ghost 改变了复杂背景上按钮的设计,block 将其拉伸到容器的宽度。尺寸通过size或常规设置ConfigProvider设置。这些属性不会改变操作的含义或事件的处理方式。
该图标通过 icon 作为 Svelte 片段传输; iconPlacement 设置侧面。对于仅图标按钮,请通过 aria-label 提供可访问的名称。 图标示例 包含这两个轨道及其源代码。
行动、链接和期望
如果没有 href,该组件将创建本机 button。它的 htmlType 默认为 button,因此它不会意外提交周围的表单。要发送Form,请使用htmlType="submit",要重置,请使用htmlType="reset"。
使用 href 创建本机 a:在新选项卡中打开,复制地址,target 和 rel 工作。 type="link" 设计本身没有 href,仍然是一个操作按钮。 示例状态 显示过渡和单击处理程序。
onclick 处理程序接收本机事件。要取消链路转换,请调用 event.preventDefault()。 bind:nativeElement 安装后提供按钮或链接;对于 SSR,还没有 DOM 元素。
loading 状态属于应用程序:在请求之前启用它,并在完成之后禁用它。 loading 对象允许您通过 delay 延迟指示器的出现,以便短时间操作不会闪烁。该组件不会触发请求,也不跟踪 Promise 处理程序。 加载中 显示切换和延迟。
按钮没有自定义图标时,默认加载指示器会在出现和消失时播放过渡动画。theme.token.motion=false 和 prefers-reduced-motion 均可关闭此过渡。按钮和链接在水合时会保留焦点。
对于单独的主题定制,请使用 设计令牌(design tokens)(Button),对于特定按钮部分,请使用 Semantic DOM 中支持的 classNames 和 styles。 CSS字符串传递给styles;这里没有使用具有 CSS 属性的 JavaScript 对象。
示例
按钮类型
基本、正常、虚线、文本按钮和链接。用你的主要风格突出主要动作。
<script lang="ts">
import { Button } from '@woodger/antd';
let action = $state(("单击按钮来测试操作。"));
const buttons = [
{ type: 'primary', label: "主要的", message: "主按钮已按下。" },
{ type: 'default', label: "常规的", message: "按下普通按钮。" },
{ type: 'dashed', label: "点状", message: "点状按钮被按下。" },
{ type: 'text', label: "文本", message: "按下文本按钮。" },
{ type: 'link', label: "关联", message: "单击链接按钮。" }
] as const;
</script>
<style>
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.status-line {
margin-top: 16px;
color: var(--ant-color-text-secondary);
font-size: 12px;
}
</style>
<div class="row">
{#each buttons as button (button.type)}
<Button type={button.type} onclick={() => { action = button.message; }}>{(button.label)}</Button>
{/each}
</div>
<div class="status-line" role="status">{action}</div>
颜色和选项
color 和 variant 是独立设置的:填充、边框、点线、文本或链接。
<script lang="ts">
import { onMount } from 'svelte';
import { Button, ConfigProvider } from '@woodger/antd';
let isWide = $state(false);
const colors = ['default', 'primary', 'danger', 'pink', 'purple', 'cyan'] as const;
const variants = ['solid', 'outlined', 'dashed', 'filled', 'text', 'link'] as const;
onMount(() => {
const query = window.matchMedia('(min-width: 1600px)');
const updateSize = () => { isWide = query.matches; };
updateSize();
query.addEventListener('change', updateSize);
return () => query.removeEventListener('change', updateSize);
});
</script>
<style>
.matrix {
display: grid;
gap: 16px;
}
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
<ConfigProvider componentSize={isWide ? 'medium' : 'small'}>
<div class="matrix">
{#each colors as color (color)}
<div class="row" role="group" aria-label={("颜色 " + String((color)) + "")}>
{#each variants as variant (variant)}
<Button
{color}
{variant}
aria-label={("" + String((color)) + ": " + String((variant)) + "")}
>{(variant)}</Button>
{/each}
</div>
{/each}
</div>
</ConfigProvider>
图标
通过 snippet 图标传递 icon。对于没有文本的按钮,请使用 aria-label 为其指定一个易于访问的名称。
<script lang="ts">
import { Button, Tooltip } from '@woodger/antd';
</script>
<style>
.stack {
display: grid;
gap: 16px;
}
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
{#snippet searchIcon()}
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.75" aria-hidden="true" focusable="false" style="display:block">
<circle cx="10.5" cy="10.5" r="7" /><path d="m16 16 5 5" />
</svg>
{/snippet}
<div class="stack">
<div class="row">
<Tooltip title={("搜索")}><Button type="primary" shape="circle" icon={searchIcon}
aria-label={("搜索,主轮")} /></Tooltip>
<Button type="primary" shape="circle">A</Button>
<Button type="primary" icon={searchIcon}>{("搜索")}</Button>
<Tooltip title={("搜索")}><Button shape="circle" icon={searchIcon}
aria-label={("搜索,圆形")} /></Tooltip>
<Button icon={searchIcon}>{("搜索")}</Button>
</div>
<div class="row">
<Tooltip title={("搜索")}><Button shape="circle" icon={searchIcon}
aria-label={("搜索,附加轮")} /></Tooltip>
<Button icon={searchIcon}>{("搜索")}</Button>
<Tooltip title={("搜索")}><Button type="dashed" shape="circle" icon={searchIcon}
aria-label={("搜索,虚线圆形")} /></Tooltip>
<Button type="dashed" icon={searchIcon}>{("搜索")}</Button>
<Button href={("/ant-design#overview")} icon={searchIcon} aria-label={("使用图标前往评论")} />
</div>
</div>
图标位置
iconPlacement 在标题之前或之后放置一个图标。加载指示器的位置也随之改变。
<script lang="ts">
import { Button, Divider, RadioGroup, Tooltip } from '@woodger/antd';
let position = $state<'start' | 'end'>('end');
</script>
<style>
.stack {
display: grid;
gap: 16px;
}
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
{#snippet searchIcon()}
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.75" aria-hidden="true" focusable="false" style="display:block">
<circle cx="10.5" cy="10.5" r="7" /><path d="m16 16 5 5" />
</svg>
{/snippet}
<div class="stack">
<RadioGroup
aria-label={("图标位置")}
optionType="button"
value={position}
options={([{ value: 'start', label: "一开始" }, {
value: 'end',
label: "在最后"
}])}
onValueChange={value => { if (value === 'start' || value === 'end') position = value; }}
/>
<Divider titlePlacement="start" plain>{("预览")}</Divider>
<div class="row">
<Tooltip title={("搜索")}><Button type="primary" shape="circle" icon={searchIcon}
aria-label={("搜索,主轮")} /></Tooltip>
<Button type="primary" shape="circle">A</Button>
<Button
type="primary"
icon={searchIcon}
iconPlacement={position}
>{("搜索")}</Button>
<Tooltip title={("搜索")}><Button shape="circle" icon={searchIcon}
aria-label={("搜索,圆形")} /></Tooltip>
<Button icon={searchIcon} iconPlacement={position}>{("搜索")}</Button>
</div>
<div class="row">
<Tooltip title={("搜索")}><Button shape="circle" icon={searchIcon}
aria-label={("搜索,附加轮")} /></Tooltip>
<Button type="text" icon={searchIcon} iconPlacement={position}>{("搜索")}</Button>
<Tooltip title={("搜索")}><Button type="dashed" shape="circle" icon={searchIcon}
aria-label={("搜索,虚线圆形")} /></Tooltip>
<Button
type="dashed"
icon={searchIcon}
iconPlacement={position}
>{("搜索")}</Button>
<Button href={("/ant-design#overview")} icon={searchIcon} iconPlacement={position}
aria-label={("使用图标前往评论")} />
<Button type="primary" loading iconPlacement={position}>{("加载中")}</Button>
</div>
</div>
方面
三种按钮尺寸:large、medium、small。大小可以单独设置或通过 ConfigProvider 设置。
<script lang="ts">
import { Button, Divider, RadioGroup } from '@woodger/antd';
let size = $state<'large' | 'medium' | 'small'>('large');
</script>
<style>
.stack {
display: grid;
gap: 16px;
}
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
{#snippet downloadIcon()}
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.75" aria-hidden="true" focusable="false" style="display:block">
<path d="M12 3v12m-4-4 4 4 4-4M5 17v4h14v-4" />
</svg>
{/snippet}
<div class="stack">
<RadioGroup
aria-label={("按钮尺寸示例")}
optionType="button"
value={size}
options={([
{ value: 'large', label: "大的" },
{ value: 'medium', label: "平均的" },
{ value: 'small', label: "小的" }
])}
onValueChange={value => {
if (value === 'large' || value === 'medium' || value === 'small') size = value;
}}
/>
<Divider titlePlacement="start" plain>{("预览")}</Divider>
<div class="row">
<Button type="primary" {size}>{("主要的")}</Button>
<Button {size}>{("常规的")}</Button>
<Button type="dashed" {size}>{("点状")}</Button>
</div>
<div><Button type="link" {size}>{("关联")}</Button></div>
<div class="row">
<Button type="primary" {size} icon={downloadIcon} aria-label={("下载,无需签名")} />
<Button type="primary" shape="circle" {size} icon={downloadIcon} aria-label={("下载,圆形")} />
<Button type="primary" shape="round" {size} icon={downloadIcon} aria-label={("下载,圆形")} />
<Button
type="primary"
shape="round"
{size}
icon={downloadIcon}
>{("下载")}</Button>
<Button type="primary" {size} icon={downloadIcon}>{("下载")}</Button>
</div>
</div>
不可用的按钮
disabled 禁用每个按钮类型的操作,包括链接按钮。
<script lang="ts">
import { Button } from '@woodger/antd';
const types = [
{ value: 'primary', label: "主要的" },
{ value: 'default', label: "常规的" },
{ value: 'dashed', label: "点状" },
{ value: 'text', label: "文本" },
{ value: 'link', label: "关联" }
] as const;
</script>
<style>
.stack {
display: grid;
gap: 16px;
}
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.row :global(button), .row :global(a) {
max-inline-size: 100%;
min-block-size: min-content;
white-space: normal;
}
.ghost-row {
padding: 16px;
border-radius: 6px;
background: rgb(190, 200, 200);
}
</style>
<div class="stack">
{#each types as type (type.value)}
<div class="row">
<Button type={type.value}>{(type.label)}</Button>
<Button
type={type.value}
disabled
>{(type.label)}{(" (无法使用)")}</Button>
</div>
{/each}
<div class="row">
<Button type="primary" href={("/ant-design#overview")}>
{("带有 href 的链接")}
</Button>
<Button type="primary" href={("/ant-design#overview")} disabled>
{("带 href 的链接(不可用)")}
</Button>
</div>
<div class="row">
<Button danger>{("危险动作")}</Button>
<Button danger disabled>{("危险行为(不可用)")}</Button>
</div>
<div class="row">
<Button danger type="text">{("危险的文字")}</Button>
<Button danger type="text" disabled>{("危险文字(不可用)")}</Button>
</div>
<div class="row">
<Button danger type="link">{("危险链接")}</Button>
<Button danger type="link" disabled>{("危险链接(不可用)")}</Button>
</div>
<div class="row ghost-row">
<Button ghost>{("透明的")}</Button>
<Button ghost disabled>{("透明(不可用)")}</Button>
</div>
</div>
加载中
指标的初始版本和带有延迟的附加示例。它的片段被旋转器取代;在 v1 中,该按钮在加载期间不可用。
<script lang="ts">
import { onDestroy } from 'svelte';
import { Button } from '@woodger/antd';
let active = $state([false, false, false, false, false]);
const timers = new Map<number, ReturnType<typeof setTimeout>>();
function startLoading(index: number) {
if (active[index]) {
return;
}
active[index] = true;
timers.set(index, setTimeout(() => {
active[index] = false;
timers.delete(index);
}, 3000));
}
onDestroy(() => {
for (const timer of timers.values()) clearTimeout(timer);
timers.clear();
});
</script>
<style>
.stack {
display: grid;
gap: 16px;
}
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.row :global(button) {
max-inline-size: 100%;
min-block-size: min-content;
white-space: normal;
}
.spinner {
display: block;
animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }
</style>
{#snippet powerIcon()}
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.75" aria-hidden="true" focusable="false" style="display:block">
<path d="M12 2v10m-6.4-6.4a9 9 0 1 0 12.8 0" />
</svg>
{/snippet}
{#snippet customSpinner()}
<svg class="spinner" width="1em" height="1em" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="1.75" aria-hidden="true" focusable="false">
<path d="M20 12a8 8 0 1 1-2.3-5.7M18 2v5h-5" />
</svg>
{/snippet}
<div class="stack">
<div class="row">
<Button type="primary" loading>{("加载中")}</Button>
<Button type="primary" size="small" loading>{("加载中")}</Button>
<Button type="primary" loading icon={powerIcon} aria-label={("上传时无需签名")} />
<Button
type="primary"
loading={{ icon: customSpinner }}
>{("您自己的指标")}</Button>
</div>
<div class="row">
<Button type="primary" loading={active[0]} onclick={() => startLoading(0)}>
{("\n 开始下载\n ")}
</Button>
<Button
type="primary"
loading={active[1]}
iconPlacement="end"
onclick={() => startLoading(1)}
>{("\n 右侧指示器\n ")}</Button>
<Button
type="primary"
loading={active[2]}
icon={powerIcon}
onclick={() => startLoading(2)}
>{("\n 替换图标\n ")}</Button>
<Button
type="primary"
loading={active[3]}
icon={powerIcon}
aria-label={("无需签名即可开始下载")}
onclick={() => startLoading(3)}
/>
<Button type="primary" loading={active[3] ? { icon: customSpinner } : false} icon={powerIcon}
onclick={() => startLoading(3)}>{("按下时的指示器")}</Button>
</div>
<div class="row">
<Button type="primary" loading={active[4] ? { delay: 500 } : false}
onclick={() => startLoading(4)}>{("延迟")}</Button>
</div>
</div>
多个动作
保持主要操作可见,并将其他操作合并到下拉列表中。 SpaceCompact 连接相邻的按钮。
<script lang="ts">
import { Button, Dropdown, SpaceCompact } from '@woodger/antd';
let action = $state(("从菜单中选择一个操作。"));
const items = [
{ key: 'first', label: "第一个动作" },
{ key: 'second', label: "第二幕" },
{ key: 'third', label: "第三幕" }
];
</script>
<style>
.row {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 8px;
}
output {
display: block;
margin-top: 16px;
color: var(--ant-color-text-secondary);
font-size: 12px;
}
</style>
{#snippet moreIcon()}
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="currentColor"
aria-hidden="true" focusable="false" style="display:block">
<circle cx="5" cy="12" r="1.6" /><circle cx="12" cy="12" r="1.6" />
<circle cx="19" cy="12" r="1.6" />
</svg>
{/snippet}
{#snippet actionButton()}<Button onclick={() => {
action = ("主要动作已经完成。");
}}>{("行动")}</Button>{/snippet}
{#snippet moreButton()}
<Dropdown trigger={['click']} placement="bottomRight" menu={({
items,
onClick: ({ key }) => {
const item = items.find(item => item.key === key);
const label = item?.label ?? key;
action = ("已选择: " + String((label)) + "");
}
})}>
<Button icon={moreIcon} aria-label={("附加操作")} />
</Dropdown>
{/snippet}
<div class="row">
<Button type="primary">{("主要的")}</Button>
<Button>{("额外的")}</Button>
<SpaceCompact items={([actionButton, moreButton])} />
</div>
<output aria-label={("选定的动作")}>{action}</output>
透明按钮
ghost 移除填充。此选项适用于彩色或其他复杂背景。
<script lang="ts">
import { Button } from '@woodger/antd';
</script>
<style>
.preview {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
padding: 16px;
border-radius: 6px;
background: rgb(190, 200, 200);
}
</style>
<div class="preview">
<Button type="primary" ghost>{("主要的")}</Button>
<Button ghost>{("常规的")}</Button>
<Button type="dashed" ghost>{("点状")}</Button>
<Button danger ghost>{("危险动作")}</Button>
</div>
危险行为
danger 突出显示需要注意的操作,例如删除数据。
<script lang="ts">
import { Button } from '@woodger/antd';
</script>
<style>
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
<div class="row">
<Button type="primary" danger>{("主要的")}</Button>
<Button danger>{("常规的")}</Button>
<Button type="dashed" danger>{("点状")}</Button>
<Button type="text" danger>{("文本")}</Button>
<Button type="link" danger>{("关联")}</Button>
</div>
全宽
block 将按钮拉伸到父容器的宽度。
<script lang="ts">
import { Button } from '@woodger/antd';
</script>
<style>
.stack {
display: grid;
gap: 8px;
}
</style>
<div class="stack">
<Button type="primary" block>{("主要的")}</Button>
<Button block>{("常规的")}</Button>
<Button type="dashed" block>{("点状")}</Button>
<Button disabled block>{("无法使用")}</Button>
<Button type="text" block>{("文本")}</Button>
<Button type="link" block>{("关联")}</Button>
</div>
坡度
通过 ConfigProvider.button.class 和 CSS 自定义主按钮样式,包括悬停状态。
<script lang="ts">
import { Button, ConfigProvider } from '@woodger/antd';
</script>
<style>
.preview {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.preview :global(.gradient-button.ant-btn-primary:not([disabled])) { position: relative; }
.preview :global(.gradient-button.ant-btn-primary:not([disabled]) > span) { position: relative; }
.preview :global(.gradient-button.ant-btn-primary:not([disabled])::before) {
content: '';
position: absolute;
inset: -1px;
border-radius: inherit;
background: linear-gradient(135deg, #6253e1, #04befe);
opacity: 1;
transition: opacity var(--ant-motion-duration-slow);
}
.preview :global(.gradient-button.ant-btn-primary:not([disabled]):hover::before) { opacity: 0; }
@media (prefers-reduced-motion: reduce) {
.preview :global(.gradient-button::before) { transition: none; }
}
</style>
{#snippet downloadIcon()}
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.75" aria-hidden="true" focusable="false" style="display:block">
<path d="M12 3v12m-4-4 4 4 4-4M5 17v4h14v-4" />
</svg>
{/snippet}
<div class="preview">
<ConfigProvider button={{ class: 'gradient-button' }}>
<div class="preview">
<Button type="primary" icon={downloadIcon}>{("坡度")}</Button>
<Button>{("常规的")}</Button>
</div>
</ConfigProvider>
</div>
自己的波
ConfigProvider.wave 禁用标准 wave 或用其自己的效果替换它。该示例考虑了减少的运动。
<script lang="ts">
import { onDestroy } from 'svelte';
import { Button, ConfigProvider, type ShowWaveEffect } from '@woodger/antd';
let effect = $state(("单击按钮即可比较效果。"));
const animations = new Set<Animation>();
const holders = new Set<HTMLElement>();
function playAnimation(node: HTMLElement, frames: Keyframe[], cleanup = () => {}) {
const animation = node.animate(frames, { duration: 500, easing: 'ease-out' });
animations.add(animation);
const finish = () => {
animations.delete(animation);
cleanup();
};
void animation.finished.then(finish, finish);
}
const showInsetEffect: ShowWaveEffect = (node, { component, event }) => {
if (component !== 'Button') {
return;
}
effect = ("内波");
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
return;
}
const rect = node.getBoundingClientRect();
const x = event.detail === 0 ? rect.width / 2 : event.clientX - rect.left;
const y = event.detail === 0 ? rect.height / 2 : event.clientY - rect.top;
const diameter = Math.hypot(rect.width, rect.height) * 2;
const holder = document.createElement('span');
holder.setAttribute('aria-hidden', 'true');
holder.style.cssText = `
position: absolute;
inset: 0;
overflow: hidden;
border-radius: inherit;
pointer-events: none;
z-index: 1;
`;
const dot = document.createElement('span');
dot.style.cssText = `
position: absolute;
left: ${x}px;
top: ${y}px;
width: ${diameter}px;
height: ${diameter}px;
border-radius: 50%;
background: rgba(255,255,255,0.65);
pointer-events: none;
`;
holder.append(dot);
node.append(holder);
holders.add(holder);
playAnimation(dot, [
{
transform: 'translate(-50%, -50%) scale(0)',
opacity: 1
},
{
transform: 'translate(-50%, -50%) scale(1)',
opacity: 0
}
], () => {
holder.remove();
holders.delete(holder);
});
};
const showShakeEffect: ShowWaveEffect = (node, { component }) => {
if (component !== 'Button') {
return;
}
effect = ("摇晃");
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
return;
}
for (const animation of node.getAnimations()) {
if (animations.has(animation)) animation.cancel();
}
playAnimation(node, [0, -12, 12, -5, 5, 0].map(angle => ({
transform: `rotate(${angle}deg)`
})));
};
onDestroy(() => {
for (const animation of animations) animation.cancel();
for (const holder of holders) holder.remove();
animations.clear();
holders.clear();
});
</script>
<style>
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 16px;
}
output {
display: block;
margin-top: 16px;
color: var(--ant-color-text-secondary);
font-size: 12px;
}
</style>
<div class="row">
<ConfigProvider wave={{ disabled: true }}>
<Button type="primary" onclick={() => { effect = ("波浪已禁用"); }}>
{("无波无浪")}
</Button>
</ConfigProvider>
<ConfigProvider>
<Button
type="primary"
onclick={() => { effect = ("标准波"); }}
>{("标准")}</Button>
</ConfigProvider>
<ConfigProvider wave={{ showEffect: showInsetEffect }}>
<Button type="primary">{("内波")}</Button>
</ConfigProvider>
<ConfigProvider wave={{ showEffect: showShakeEffect }}>
<Button type="primary">{("摇晃")}</Button>
</ConfigProvider>
</div>
<output aria-label={("波浪效应")}>{effect}</output>
无法访问的按钮的背景
defaultBgDisabled 和 dashedBgDisabled 令牌通过 ConfigProvider 主题更改禁用按钮的背景。
<script lang="ts">
import { Button, ConfigProvider } from '@woodger/antd';
</script>
<style>
.row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
<ConfigProvider theme={{
cssVar: {},
components: {
Button: {
defaultBgDisabled: 'rgba(0,0,0,0.1)',
dashedBgDisabled: 'rgba(0,0,0,0.4)'
}
}
}}>
<div class="row">
<Button type="primary" disabled>{("主要的")}</Button>
<Button disabled>{("常规的")}</Button>
<Button type="dashed" disabled>{("点状")}</Button>
</div>
</ConfigProvider>
语义风格
classNames 和样式 styles root 和 content。 styles 从按钮的属性中获取一个对象或函数。
<script lang="ts">
import { Button, type ButtonProps } from '@woodger/antd';
const classNames = { root: 'semantic-root', content: 'semantic-content' };
const styles: ButtonProps['styles'] = info => info.props.type === 'primary'
? {
root: `
background: #171717;
border-color: #171717;
`,
content: 'color:#fff'
}
: {};
</script>
<style>
.preview {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.preview :global(.semantic-root) {
height: auto;
padding: 8px 16px;
border-radius: var(--ant-border-radius);
}
.preview :global(.semantic-content) { font-weight: 500; }
</style>
<div class="preview">
<Button {classNames} styles={{ root: 'box-shadow:0 1px 2px rgba(0,0,0,0.05)' }}>
{("目的")}
</Button>
<Button type="primary" {classNames} {styles}>{("功能")}</Button>
</div>
颜色操作和按钮组
彩色操作按钮、链接以及由 Flex 排列的计数按钮。ConfigProvider 设置计数按钮的尺寸和外观。
<script lang="ts">
import { Button, ConfigProvider, Flex } from '@woodger/antd';
let clicks = $state(0);
</script>
<Flex gap={8} align="center" wrap>
<Button color="purple" variant="filled" iconPlacement="end" onclick={() => { clicks++; }}>
{#snippet icon()}
<span aria-hidden="true">★</span>
{/snippet}{("\n 颜色动作(")}{clicks})
</Button>
<Button href={("/ant-design#overview")}>{("前往评论")}</Button>
<ConfigProvider componentSize="small" button={{ color: 'green', variant: 'outlined' }}>
<Flex gap={8}>
<Button onclick={() => { clicks--; }}>−</Button>
<Button onclick={() => { clicks++; }}>+</Button>
</Flex>
</ConfigProvider>
</Flex>
prefixCls 指定按钮的完整前缀,不会自动追加后缀 -btn.
按钮容器和内部变量使用提供者的公共前缀。
按钮 CSS 前缀
通用命名空间ConfigProvider和本地 prefixCls;当名称更改时,组件的状态将被保留。
<script lang="ts">
import { Button, Checkbox, ConfigProvider, Flex } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-btn' : '');
let clicks = $state(0);
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("通用按钮套件前缀")}</Checkbox>
<Checkbox bind:checked={local}>{("按钮本地前缀")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'button-theme' } }}>
<div class="prefix-stage">
<Flex gap={8} wrap>
<Button prefixCls={localPrefix} type="primary" onclick={() => clicks++}>
{("带前缀保存")}
</Button>
<Button prefixCls={localPrefix} onclick={() => clicks = 0}>
{("重置计数器")}
</Button>
</Flex>
<output
aria-label={("带前缀的按钮操作")}
>{("行动: ")}{clicks}</output>
</div>
</ConfigProvider>
<output
aria-label={("按钮 CSS 前缀")}
>{("一般的: ")}{prefix}{(";当地的: ")}{(localPrefix
|| ("来自提供商"))}</output>
<p>
<code>prefixCls</code> {("指定按钮的完整前缀,不会自动追加后缀")} <code>-btn</code>.
{("按钮容器和内部变量使用提供者的公共前缀。")}
</p>
</div>
API
使用 Button 的基本参数。公共类型中提供精确签名
ButtonProps, ButtonLinkProps 和 ButtonConfig
安装包 @woodger/antd.
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 组件及其部件的完整 CSS 前缀。非空本地 prop 优先;空字符串和 undefined 继承ConfigProvider。主题的命名空间是单独设置的。 | ButtonProps['prefixCls'] | ant-btn |
type | 现成的设计:primary、default、dashed、text 或 link。 | ButtonProps['type'] | default |
color | 按钮颜色:default、primary、danger 或预设颜色之一。 | ButtonProps['color'] | default |
variant | 设计选项:solid、outlined、dashed、filled、text 或 link。 | ButtonProps['variant'] | outlined |
danger | 突出显示危险动作。显式 color+variant 对优先。 | ButtonProps['danger'] | false |
ghost | 复杂基材的透明背景;适用于带有边框或填充的按钮。 | ButtonProps['ghost'] | false |
block | 将按钮拉伸到其父容器的宽度。 | ButtonProps['block'] | false |
size | small、medium 或 large。没有本地 size,继承ConfigProvider。 | ButtonProps['size'] | medium |
shape | default 为 circle、round 或 square。 | ButtonProps['shape'] | default |
children | 带有按钮内容的标题或 Svelte 片段。 | ButtonProps['children'] | — |
icon | 类似片段或其他 ButtonContent 的图标。 | ButtonProps['icon'] | — |
iconPlacement | 图标位置:start 或 end。 | ButtonProps['iconPlacement'] | start |
loading | boolean 或 delay 以毫秒为单位的对象及其自己的 icon。对于 v1,主动加载使本机 button 不可用。 | ButtonProps['loading'] | false |
disabled | 禁止某项动作。对于 href,删除过渡并从 Tab 键顺序中排除链接。 | ButtonProps['disabled'] | false |
href | 创建本机链接而不是 button。如果没有 href,type="link" 仍然是一个操作按钮。 | ButtonLinkProps['href'] | — |
target | 链接导航目标; rel、download 和 referrerpolicy 也传递给本机元素。 | ButtonProps['target'] | — |
htmlType | 原生按钮类型:button、submit 或 reset。默认按钮。 | ButtonProps['htmlType'] | button |
onclick | 原生 Svelte 事件。对于链接,处理程序类型由 ButtonLinkProps 确定。 | ButtonProps['onclick'] | — |
classNames | 类语义槽 root、icon 和 content:来自 { props } 的对象或回调。 | ButtonProps['classNames'] | — |
styles | 语义槽的 CSS 字符串:来自 { props } 的对象或回调。 | ButtonProps['styles'] | — |
autoInsertSpace | 两个汉字之间自动间距;默认启用。 | ButtonProps['autoInsertSpace'] | true |
nativeElement | bind:nativeElement 获取挂载后的原生按钮或链接; unmount 会清除链接。 | ButtonProps['nativeElement'] | — |
onclick 优先于兼容 onClick。为了 href 处理程序接收本机链接事件。原生属性包括 form, name, value
和 aria-*,被传递给元素。
Preset Colors
bluepurplecyangreenmagentapinkredorangeyellowvolcanogeekbluelimegoldConfigProvider.button
常规按钮设置接受公共字段 ButtonConfig:
autoInsertSpacecolorvariantshapeloadingIconclassstyleclassNamesstyles
本地按钮参数优先。 iconPlacement 设置图标的位置; SpaceCompact 将相邻的按钮组合成一个紧凑的组。
Semantic DOM
选择一个元素以查看其在实际按钮上的边框。这些名称用于 classNames 和 styles.
classNames 分配CSS类,并且 styles 接受 CSS 字符串。两个选项都支持来自的对象或函数 { props }. 语义样式示例 显示两个选项。
注册令牌
组件设置通过传输 ConfigProvider.theme.components.Button。他们的名字经过公众验证 ComponentTokenMap['Button'].
<script lang="ts">
import { Button, ConfigProvider } from '@woodger/antd';
</script>
<ConfigProvider theme={{
cssVar: {},
components: {
Button: {
defaultBg: '#f6ffed',
defaultColor: '#389e0d',
defaultBorderColor: '#b7eb8f',
contentFontSize: 16
}
}
}}>
<Button>{("自定义按钮主题")}</Button>
</ConfigProvider>
| 代币 | 目的 |
|---|---|
fontWeight | 文本饱和度。 |
iconGap | 图标和标题之间的距离。 |
defaultShadow | 常规按钮的阴影。 |
primaryShadow | 主按钮的阴影。 |
dangerShadow | 危险按钮阴影。 |
primaryColor | 主按钮文本颜色。 |
defaultColor | 正常按钮文本颜色。 |
defaultBg | 常规按钮的背景。 |
defaultBorderColor | 常规按钮的框架。 |
dangerColor | 危险按钮文本颜色。 |
defaultHoverBg | 悬停时的背景。 |
defaultHoverColor | 悬停文本颜色。 |
defaultHoverBorderColor | 悬停时的框架。 |
defaultActiveBg | 单击时的背景。 |
defaultActiveColor | 单击时的文本颜色。 |
defaultActiveBorderColor | 按下时框架。 |
defaultGhostColor | 透明按钮文本颜色。 |
ghostBg | 透明按钮背景。 |
defaultGhostBorderColor | 透明按钮框架。 |
solidTextColor | 带填充的按钮文本颜色。 |
textTextColor | 文本按钮文本颜色。 |
textTextHoverColor | 悬停时文本按钮的文本颜色。 |
textTextActiveColor | 单击文本按钮时的文本颜色。 |
paddingInline | 正常尺寸的水平压痕。 |
paddingInlineLG | 横向空间大。 |
paddingInlineSM | 水平空间小。 |
onlyIconSize | 没有标题的按钮中的图标大小。 |
onlyIconSizeLG | 图标的大小在一个没有签名的大按钮中。 |
onlyIconSizeSM | 小按钮中的图标大小,无标题。 |
linkHoverBg | 悬停时 link 背景。 |
textHoverBg | 悬停时的背景文本。 |
contentFontSize | 常规按钮字体大小。 |
contentFontSizeLG | 大按钮字体大小。 |
contentFontSizeSM | 小按钮字体大小。 |
defaultBgDisabled | 无法访问的常规按钮的背景。 |
dashedBgDisabled | 无法访问的虚线按钮背景。 |
当前主题的常用标记
下面的值是通过public获得的 theme.useToken()。在页面面板中切换颜色、深色主题和紧凑性 - 表格将随之更新。
| 代币 | 目的 | 当前值 |
|---|---|---|
controlHeight | 常规按钮的高度,px。 | 32 |
controlHeightLG | 大按钮的高度,px。 | 40 |
controlHeightSM | 小按钮高度,px。 | 24 |
fontSize | 主要字体大小,px。 | 14 |
fontSizeLG | 大字体大小,px。 | 16 |
borderRadius | 基本圆角,px。 | 6 |
colorPrimary | 主要主题颜色。 | #2f54eb |
colorBgContainer | 容器背景。 | #ffffff |
colorBgContainerDisabled | 无法访问的控制背景。 | rgba(0,0,0,0.04) |
colorTextDisabled | 无法访问的文本的颜色。 | rgba(0,0,0,0.25) |
opacityLoading | 加载不透明度。 | 0.65 |
FAQ
为什么加载后按钮不可用?
当前@woodger/antd原生 button 在加载指示灯打开时接收disabled和aria-busy。它停止接受点击并从 Tab 键顺序中排除。对于与 href 的链接,加载会取消转换。这是我们实现的行为的一个特点;不要使用下载来代替检查是否允许您执行某项操作。
如何正确禁用某个操作?
如果该操作不可用,则传递 disabled。对于带有 href 的按钮,库会阻止转换并从 Tab 键顺序中排除链接。颜色和外观本身并不禁止点击。 不可用状态可以与普通按钮进行比较。
为什么主颜色不随 type 改变?
检查color和variant是否同时设置,包括ConfigProvider.button设置。对于非标准设计,请使用显式属性对和 颜色和选项示例。单独的按钮标记和通用主题调色板解决不同的问题。
如何去掉两个汉字之间的空格?
将 autoInsertSpace={false} 设置为按钮或 ConfigProvider.button。该设置是指短中文签名中的自动间距;它不会删除文本中的空格。
如何禁用或替换按下时的波形?
将 wave={{ disabled: true }} 传递给 ConfigProvider。其效果通过wave.showEffect设置;动画和创建的 DOM 节点由回调的所有者释放。 你自己的波浪的例子 显示这两个选项并考虑减少运动。
下面的按钮允许您比较等效的 type 和 color + variant,以及在没有波形的情况下按下。