Skip to contents

Button

A button component that can be rendered as another tag or focusable when disabled.

Usage guidelines

  • Submit buttons: Unlike the native button element, type="submit" must be specified on Button for it to act as a submit button.
  • Links: The Button component enforces button semantics (role="button", keyboard interaction, disabled state). It should not be used for links. See Rendering links as buttons below.

Anatomy

Import the component:

Examples

Rendering as another tag

The button can remain keyboard accessible while being rendered as another tag, such as a <div>, by specifying nativeButton={false}. In Svelte, render is a snippet. Spread props onto the element and render children inside it.

The Button component enforces button semantics. nativeButton={false} signals that the rendered tag is not a <button>, but it must still be a tag that can receive button semantics (role="button", keyboard interaction handlers). Links (<a>) have their own semantics and should not be rendered as buttons through the render prop.

If a link needs to look like a button visually, style the <a> element directly with CSS rather than using the Button component.

Loading states

For buttons that enter a loading state after activation, specify focusableWhenDisabled so focus remains on the button while it is disabled. Because some browser and screen reader combinations do not reliably announce changes to a focused button's descendant text, use aria-labelledby to make the changing text the button's explicit accessible name.

API reference

disabled boolean | null false
Description

Whether the button should ignore user interaction.

Type
boolean | null
Default
false
focusableWhenDisabled boolean false
Description

Whether a disabled button stays in the tab order. Native buttons then use aria-disabled instead of the disabled attribute.

Type
boolean
Default
false
nativeButton boolean true
Description

Whether the host is a native <button>. Set false when render supplies a non-button element.

Type
boolean
Default
true
onclick MouseEventHandler<HTMLButtonElement> | null โ€”
Name
Description

Runs for a click the button does not ignore.

Type
MouseEventHandler<HTMLButtonElement> | null
render Snippet<[props: ButtonHostProps, state: ButtonState, children: Snippet | undefined]> โ€”
Name
Description

Replace the default <button>. Spread props onto the host element.

Type
Snippet<[props: ButtonHostProps, state: ButtonState, children: Snippet | undefined]>
disabled boolean
Name
disabled
Description

Whether the button should ignore user interaction.

Type
boolean
data-disabled
Present when the button is disabled.