Ant Design Svelte

Button

The button triggers an action in response to a press.

When to use

Button triggers an action: saves data, opens a window, adds a record, or submits a form. The caption should explain the result of the click: “Save changes” is clearer than “OK”.

  • Primary button highlights the main action. In one action group, one main button is usually sufficient.
  • Regular buttons are suitable for equivalent actions; text - for secondary ones.
  • The dotted button is convenient to use to add an element.
  • To go to another page, pass href: the component will create a real link. If the user selects one of the values, consider Radio or Select.

Button CSS Prefixes show global ConfigProvider.prefixCls and local prefixCls without resetting state. Local prefixCls is the full prefix of the button, without automatically adding -btn. The button container and internal variables use the shared provider prefix.

How to choose the design

Start with the action role, then choose a design. type specifies a ready-made option; color and variant allow you to customize the color and display method separately.

Problem Setting Working example
Main action type="primary" Types of buttons
Equivalent action type="default" Multiple buttons
Add element type="dashed" Types of buttons
Secondary Action type="text" Types of buttons
Custom color and fill color and variant Color and option

type="primary" corresponds to the combination color="primary" variant="solid". Explicit color and variant take precedence. For removal and other risky actions, use danger; If you specify color at the same time, consider its priority.

ghost changes the design of the button on a complex background, and block stretches it to the width of the container. The size is set via size or general settings ConfigProvider. These properties do not change the meaning of the action or the way the event is processed.

The icon is transmitted via icon as Svelte snippet; iconPlacement sets the side. For an icon-only button, provide an accessible name via aria-label. Example of icons contains both tracks and their source code.

Actions, links and expectations

Without href, the component creates a native button. Its htmlType defaults to button, so it doesn't submit the surrounding form accidentally. To send Form use htmlType="submit", to reset use htmlType="reset".

With href a native a is created: opening in a new tab, copying the address, target and rel work. The type="link" design itself without href remains an action button. Example states shows the transition and click handler.

The onclick handler receives a native event. To cancel the link transition, call event.preventDefault(). bind:nativeElement provides a button or link after mount; with SSR there is no DOM element yet.

The loading state belongs to the application: enable it before the request and disable it after completion. The loading object allows you to delay the appearance of the indicator through delay so that the short operation does not flash. The component does not fire the request and does not track the Promise handler. Loading shows switching and delay.

On a button without an icon, the default loading indicator animates in and out. Both theme.token.motion=false and prefers-reduced-motion disable that transition. Buttons and links keep focus through hydration.

For individual theme customization use design tokens for Button and for specific button part use supported classNames and styles from Semantic DOM. CSS strings are passed to styles; A JavaScript object with CSS properties is not used here.

Examples

Press the button to check the action.

Button types

Basic, normal, dotted, text button and link. Highlight the main action with your main style.

Colors and options

The color and variant are set independently: fill, frame, dotted line, text or link.

Icons

Pass the icon via snippet icon. For a button without text, give it an accessible name using aria-label.

Icon location

iconPlacement places an icon before or after the caption. The position of the loading indicator changes along with it.

Dimensions

Three button sizes: large, medium and small. The size can be set individually or via ConfigProvider.

Inaccessible buttons

disabled disables the action for every button type, including a link button.

Loading

Initial versions of the indicator and an additional example with a delay. Its snippet is replaced by a spinner; in v1 the button is not available during loading.

Select an action from the menu.

Multiple actions

Leave the main action visible, and combine additional ones into Dropdown. SpaceCompact connects adjacent buttons.

Transparent buttons

ghost removes fill. This option is suitable for colored or other complex backgrounds.

Dangerous actions

danger highlights actions that require attention, such as deleting data.

Full width

block stretches the button to the width of the parent container.

Gradient

Custom main button styling via ConfigProvider.button.class and CSS, including hover state.

Click the button to compare effects.

Own wave

ConfigProvider.wave disables the standard wave or replaces it with its own effect. The example takes into account reduced motion.

Background of inaccessible buttons

The defaultBgDisabled and dashedBgDisabled tokens change the background of inaccessible buttons via the ConfigProvider theme.

Semantic styles

classNames and styles style root and content. styles takes an object or function from the button's properties.

Go to review

Color action and button group

A colored action, a link and counter buttons in Flex. ConfigProvider sets the size and appearance of the counter buttons.

Actions: 0
General: suite; local: project-btn

prefixCls sets the full button prefix without appending the suffix -btn. The button container and internal variables use the shared provider prefix.

Button CSS prefixes

Common namespace ConfigProvider and local prefixCls; the state of the component is preserved when the name is changed.

API

Basic parameters for working with Button. Precise signatures are available in public types ButtonProps, ButtonLinkProps and ButtonConfig installed package @woodger/antd.

Basic Button parameters
PropertyDescriptionTypeDefault
prefixClsFull CSS prefix of the component and its parts. A non-empty local prop takes precedence; empty string and undefined inherit ConfigProvider. The namespace of the topic is set separately.ButtonProps['prefixCls']ant-btn
typeReady-made design: primary, default, dashed, text or link.ButtonProps['type']default
colorButton color: default, primary, danger or one of the preset colors.ButtonProps['color']default
variantDesign option: solid, outlined, dashed, filled, text or link.ButtonProps['variant']outlined
dangerHighlights a dangerous action. The explicit color + variant pair takes precedence.ButtonProps['danger']false
ghostTransparent background for complex substrate; Applies to buttons with a border or fill.ButtonProps['ghost']false
blockStretches the button to the width of the parent container.ButtonProps['block']false
sizesmall, medium or large. Without local size, inherits ConfigProvider.ButtonProps['size']medium
shapedefault, circle, round or square.ButtonProps['shape']default
childrenLabel or Svelte snippet with button content.ButtonProps['children']—
iconIcon as snippet or other ButtonContent.ButtonProps['icon']—
iconPlacementIcon position: start or end.ButtonProps['iconPlacement']start
loadingboolean or object with delay in milliseconds and its own icon. For v1, active loading makes the native button unavailable.ButtonProps['loading']false
disabledDisables the action. For href, removes the transition and excludes the link from the Tab order.ButtonProps['disabled']false
hrefCreates a native link instead of a button. Without href, type="link" remains an action button.ButtonLinkProps['href']—
targetLink navigation target; rel, download and referrerpolicy are also passed to the native element.ButtonProps['target']—
htmlTypeNative button type: button, submit or reset. By default button.ButtonProps['htmlType']button
onclickNative Svelte event. For a link, the handler type is determined by ButtonLinkProps.ButtonProps['onclick']—
classNamesClasses semantic slots root, icon and content: object or callback from { props }.ButtonProps['classNames']—
stylesCSS strings for semantic slots: object or callback from { props }.ButtonProps['styles']—
autoInsertSpaceAutomatic spacing between two Chinese characters; enabled by default.ButtonProps['autoInsertSpace']true
nativeElementbind:nativeElement gets the native button or link after mount; unmount clears the link.ButtonProps['nativeElement']—

onclick takes priority over compatible onClick. For href handler receives the native link event. Native attributes including form, name, value and aria-*are passed to the element.

Preset Colors

bluepurplecyangreenmagentapinkredorangeyellowvolcanogeekbluelimegold

ConfigProvider.button

General button settings accept public fields ButtonConfig:

autoInsertSpacecolorvariantshapeloadingIconclassstyleclassNamesstyles

Local Button parameters take precedence. iconPlacement sets the position of the icon; SpaceCompact combines adjacent buttons into a compact group.

Semantic DOM

Select an element to see its border on the actual button. These names are used in classNames and styles.

root: Native button or a: background, border, size and states.

classNames assigns CSS classes, and styles accepts CSS strings. Both options support an object or function from { props }. Example of semantic styles shows both options.

Registration tokens

Component settings are transmitted via ConfigProvider.theme.components.Button. Their names are verified against public 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>{("Custom Button theme")}</Button>
</ConfigProvider>
Button component tokens
TokenPurpose
fontWeightText saturation.
iconGapDistance between the icon and the label.
defaultShadowShadow of a regular button.
primaryShadowShadow of the main button.
dangerShadowShadow of a dangerous button.
primaryColorMain button text color.
defaultColorText color of a regular button.
defaultBgBackground of a regular button.
defaultBorderColorFrame of a regular button.
dangerColorDanger button text color.
defaultHoverBgBackground when hovering.
defaultHoverColorText color when hovering.
defaultHoverBorderColorHover frame.
defaultActiveBgBackground when pressed.
defaultActiveColorText color when pressed.
defaultActiveBorderColorFrame when pressed.
defaultGhostColorTransparent button text color.
ghostBgTransparent button background.
defaultGhostBorderColorTransparent button frame.
solidTextColorButton text color with fill.
textTextColorText color of the text button.
textTextHoverColorText color of the text button when hovered.
textTextActiveColorThe text color of the text button when pressed.
paddingInlineNormal size horizontal space.
paddingInlineLGLarge horizontal space.
paddingInlineSMSmall horizontal indent.
onlyIconSizeIcon size in a button without a label.
onlyIconSizeLGIcon size in a large button without a label.
onlyIconSizeSMIcon size in a small button without a label.
linkHoverBglink background on hover.
textHoverBgText background when hovering.
contentFontSizeThe font size of a regular button.
contentFontSizeLGLarge button font size.
contentFontSizeSMSmall button font size.
defaultBgDisabledBackground of an inaccessible regular button.
dashedBgDisabledBackground of the inaccessible dotted button.

Common tokens of the current topic

Values below received via public theme.useToken(). Switch color, dark theme and compactness in the page panel - the table will update with them.

Common tokens of the current topic
TokenPurposeCurrent value
controlHeightHeight of a regular button, px. 32
controlHeightLGHeight of the large button, px. 40
controlHeightSMSmall button height, px. 24
fontSizeMain font size, px. 14
fontSizeLGLarge font size, px. 16
borderRadiusBasic fillet, px. 6
colorPrimaryPrimary theme color. #2f54eb
colorBgContainerContainer background. #ffffff
colorBgContainerDisabledBackground of inaccessible control. rgba(0,0,0,0.04)
colorTextDisabledColor of inaccessible text. rgba(0,0,0,0.25)
opacityLoadingLoading opacity. 0.65

FAQ

Why does loading make the button unavailable?

In the current @woodger/antd native button receives disabled and aria-busy when the loading indicator is turned on. It stops accepting clicks and is excluded from the Tab order. For the link with href, loading cancels the transition. This is a feature of the behavior of our implementation; Don't use downloading as a substitute for checking if you're allowed to perform an action.

How to properly disable the action?

Pass disabled if the action is not available. For a button with href, the library blocks the transition and excludes the link from the Tab order. Color and appearance alone do not prohibit clicking. Unavailable states can be compared to ordinary buttons.

Why doesn't the main color change via type?

Check whether color and variant are set at the same time, including the ConfigProvider.button settings. For non-standard design, use an explicit property pair and example of color and option. Individual button tokens and the general theme palette solve different problems.

How to remove the space between two Chinese characters?

Set autoInsertSpace={false} to Button or ConfigProvider.button. This setting refers to the automatic spacing in a short Chinese signature; It does not remove spaces within your text.

How to disable or replace the wave when pressed?

Pass wave={{ disabled: true }} to ConfigProvider. Its effect is set via wave.showEffect; animations and created DOM nodes are released by the owner of the callback. Example of your own wave shows both options and takes reduced motion into account.

The buttons below allow you to compare the equivalent type and color + variant, as well as pressing without a wave.