Button
A button starts an action or navigates to a page. It renders a native <button>, or a link when href is set.
When to use
- Use a
primarybutton for the main action of a screen. There should be one per area. - Use default and dashed for secondary actions, text and link for the least important ones.
- Use
dangerfor destructive actions, andloadingwhile a request is in progress.
Examples
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary">Primary</Button>
<Button>Default</Button>
<Button type="dashed">Dashed</Button>
<Button type="text">Text</Button>
<Button type="link">Link</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary" size="large">Large</Button>
<Button type="primary">Medium</Button>
<Button type="primary" size="small">Small</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
{#snippet searchIcon()}
<svg viewBox="0 0 16 16" aria-hidden="true">
<circle cx="7" cy="7" r="4.5" />
<path d="m10.5 10.5 3 3" />
</svg>
{/snippet}
<Button type="primary">Default</Button>
<Button type="primary" shape="round">Round</Button>
<Button type="primary" shape="circle" icon={searchIcon} aria-label="Search" />
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
{#snippet uploadIcon()}
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M8 12V2m0 0L4 6m4-4 4 4M2 14h12" />
</svg>
{/snippet}
<Button type="primary" icon={uploadIcon}>Upload</Button>
<Button icon={uploadIcon} iconPlacement="end">Upload</Button>
<Button shape="circle" icon={uploadIcon} aria-label="Upload" />
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary" danger>Primary</Button>
<Button danger>Default</Button>
<Button type="dashed" danger>Dashed</Button>
<Button type="text" danger>Text</Button>
<Button type="link" danger>Link</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary" ghost>Primary</Button>
<Button ghost>Default</Button>
<Button type="dashed" ghost>Dashed</Button>
<Button type="primary" danger ghost>Danger</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
let saving = $state(false);
function save() {
saving = true;
setTimeout(() => (saving = false), 2000);
}
</script>
<Button type="primary" loading={saving} onclick={save}>Click me</Button>
<Button loading>Loading</Button>
<Button loading={{ delay: 500 }}>Delayed</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary" disabled>Primary</Button>
<Button disabled>Default</Button>
<Button type="dashed" disabled>Dashed</Button>
<Button type="link" disabled href="/docs">Link</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary" href="https://www.npmjs.com/package/svelte-ant-design" target="_blank" rel="noreferrer">npm</Button>
<Button href="https://github.com/prostreyd/ant-design-svelte" target="_blank" rel="noreferrer">GitHub</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
</script>
<Button type="primary" block>Primary</Button>
<Button block>Default</Button>
<script lang="ts">
import { Button } from 'svelte-ant-design';
let result = $state('');
function submit(event: SubmitEvent) {
event.preventDefault();
// The name and value of the button that was pressed are part of the form data.
const data = new FormData(event.currentTarget as HTMLFormElement, event.submitter);
result = `Submitted: ${data.get('action')}`;
}
</script>
<form onsubmit={submit} onreset={() => (result = '')}>
<input name="title" aria-label="Title" placeholder="Title" required />
<Button type="primary" htmlType="submit" name="action" value="save">Save</Button>
<Button htmlType="reset">Reset</Button>
</form>
<Button href="data:text/plain;charset=utf-8,Hello" download="hello.txt">Download</Button>
<span role="status">{result}</span>
API
| Property | Description | Type | Default |
|---|---|---|---|
| type | Visual style of the button. | 'primary' | 'default' | 'dashed' | 'text' | 'link' | 'default' |
| size | Size. middle is a compatibility alias of medium. In a SpaceCompact a button without a size takes the one of the block. | 'large' | 'medium' | 'middle' | 'small' | 'medium' |
| shape | Shape of the button. | 'default' | 'circle' | 'round' | 'default' |
| disabled | Prevents activation. A link is rendered without href. | boolean | false |
| loading | A spinner at once or after a delay in milliseconds. The button keeps focus. | boolean | { delay: number } | false |
| danger | Red colour scheme. | boolean | false |
| ghost | Transparent background for default, dashed and primary. | boolean | false |
| block | Stretches the button to the container width. | boolean | false |
| href | Renders an <a> instead of a <button>. An empty string also selects a link. | string | — |
| target / rel / download | Native link attributes, only together with href. | string / string / string | boolean | — |
| htmlType | Native button type; ignored for a link. | 'button' | 'submit' | 'reset' | 'button' |
| form / name / value | Native button attributes for forms. | string | — |
| icon | An icon snippet, hidden from assistive technology. | Snippet | — |
| iconPlacement | Position of the icon relative to the label. | 'start' | 'end' | 'start' |
| children | The label or content of the button. | Snippet | — |
| class | An additional CSS class. | string | — |
| native attributes | Native button or a attributes (including tabindex and aria-*) are forwarded to the element; the loading and disabled states take priority over a consumer aria-busy, aria-disabled and tabindex. | HTMLButtonAttributes | HTMLAnchorAttributes | — |
| onclick | Called when an enabled button is activated; not called while disabled or loading. | (event: MouseEvent) => void | — |
Reserved for v1.1 (additive, not implemented yet):
color, variant, classNames, styles.Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-control-height | Height of a medium button. | 32px |
| --ads-control-height-lg | Height of a large button. | 40px |
| --ads-control-height-sm | Height of a small button. | 24px |
| --ads-radius-md | Corner radius. | 6px |
| --ads-color-primary-solid | Fill of a primary button. tokens-accessible.css darkens it to #1668dc. | #1677ff |
| --ads-color-primary-text | Text colour of ghost and of the border and text of the default and dashed types on hover. | #1677ff |
| --ads-color-link | Text colour of the link type. The tokens -hover and -active hold its states. | #1677ff |
| --ads-color-link-hover | Text colour of the link type on hover. | #69b1ff |
| --ads-button-text-hover-bg | Background of the text type on hover. | rgba(0, 0, 0, 0.04) |
| --ads-button-loading-opacity | Opacity of a loading button. The dark theme and tokens-accessible.css set it to 1. | 0.65 |
| --ads-color-danger-solid | Fill of a danger button. tokens-accessible.css darkens it to #d9363e. | #ff4d4f |
| --ads-color-focus-outline | Colour of the focus ring. | #1677ff |
Accessibility
- A loading button stays focusable and exposes
aria-busyandaria-disabled; clicks, Enter and Space are ignored. - A disabled or loading link is rendered without
href, so it cannot be opened in a new tab. - An icon-only button needs an accessible name, for example
aria-label.