# Button

[component-header:sl-button]

Buttons represent actions that are available to the user.

```html preview
<sl-button>Button</sl-button>
```

## Examples

### Types

Use the `type` attribute to set the button's type.

```html preview
<sl-button type="default">Default</sl-button>
<sl-button type="primary">Primary</sl-button>
<sl-button type="danger">Danger</sl-button>
```

### Sizes

Use the `size` prop to change a button's size.

```html preview
<sl-button size="small">Small</sl-button>
<sl-button size="medium">Medium</sl-button>
<sl-button size="large">Large</sl-button>
```

### Pill Buttons

Use the shape value 'pill' to give buttons rounded edges.

```html preview
<sl-button size="small" shape="pill">Small</sl-button>
<sl-button size="medium" shape="pill">Medium</sl-button>
<sl-button size="large" shape="pill">Large</sl-button>
```

### Circle Buttons

Use the shape value `circle` to create circular icon buttons.

```html preview
<sl-button type="default" size="small" shape="circle"><sl-icon name="gear"></sl-icon></sl-button>
<sl-button type="default" size="medium" shape="circle"><sl-icon name="gear"></sl-icon></sl-button>
<sl-button type="default" size="large" shape="circle"><sl-icon name="gear"></sl-icon></sl-button>
```

### Group Buttons

Use the shape values `block-first`, `block-middle` and `block-last` to create buttons that are prepared for a button group.
(A button group will apply these attributes automatically)

```html preview
<sl-button type="default" shape="block-first"><sl-icon name="gear"></sl-icon></sl-button>
<sl-button type="default" shape="block-middle"><sl-icon name="gear"></sl-icon></sl-button>
<sl-button type="default" shape="block-last"><sl-icon name="gear"></sl-icon></sl-button>
```

### Text Buttons

Use the `text` type to create text buttons that share the same size as regular buttons but don't have backgrounds or borders.

```html preview
<sl-button type="text" size="small">Text</sl-button>
<sl-button type="text" size="medium">Text</sl-button>
<sl-button type="text" size="large">Text</sl-button>
```

### Link Buttons

It's often helpful to have a button that works like a link. This is possible by setting the `href` attribute, which will make the component render an `<a>` under the hood. This gives you all the default link behavior the browser provides (e.g. <kbd>CMD/CTRL/SHIFT + CLICK</kbd>) and exposes the `target` and `download` attributes.

```html preview
<sl-button href="https://example.com/">Link</sl-button>
<sl-button href="https://example.com/" target="_blank">New Window</sl-button>
<sl-button href="/assets/images/wordmark.svg" download="stallion.svg">Download</sl-button>
<sl-button href="https://example.com/" disabled>Disabled</sl-button>
```

?> When a `target` is set, the link will receive `rel="noreferrer noopener"` for [security reasons](https://mathiasbynens.github.io/rel-noopener/).

### Setting a Custom Width

As expected, buttons can be given a custom width by setting its `width`. This is useful for making buttons span the full width of their container on smaller screens.

```html preview
<sl-button type="default" size="small" style="width: 100%; margin-bottom: 1rem;">Small</sl-button>
<sl-button type="default" size="medium" style="width: 100%; margin-bottom: 1rem;">Medium</sl-button>
<sl-button type="default" size="large" style="width: 100%;">Large</sl-button>
```

### Prefix and Suffix Icons

Use the `prefix` and `suffix` slots to add icons.

```html preview
<sl-button type="default" size="small">
  <sl-icon slot="prefix" name="gear"></sl-icon>
  Settings
</sl-button>

<sl-button type="default" size="small">
  <sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
  Refresh
</sl-button>

<sl-button type="default" size="small">
  <sl-icon slot="prefix" name="link-45deg"></sl-icon>
  <sl-icon slot="suffix" name="box-arrow-up-right"></sl-icon>
  Open
</sl-button>

<br><br>

<sl-button type="default">
  <sl-icon slot="prefix" name="gear"></sl-icon>
  Settings
</sl-button>

<sl-button type="default">
  <sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
  Refresh
</sl-button>

<sl-button type="default">
  <sl-icon slot="prefix" name="link-45deg"></sl-icon>
  <sl-icon slot="suffix" name="box-arrow-up-right"></sl-icon>
  Open
</sl-button>

<br><br>

<sl-button type="default" size="large">
  <sl-icon slot="prefix" name="gear"></sl-icon>
  Settings
</sl-button>

<sl-button type="default" size="large">
  <sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
  Refresh
</sl-button>

<sl-button type="default" size="large">
  <sl-icon slot="prefix" name="link-45deg"></sl-icon>
  <sl-icon slot="suffix" name="box-arrow-up-right"></sl-icon>
  Open
</sl-button>
```

### Caret

Use the `caret` prop to add a dropdown indicator when a button will trigger a dropdown, menu, or popover.

```html preview
<sl-button size="small" caret>Small</sl-button>
<sl-button size="medium" caret>Medium</sl-button>
<sl-button size="large" caret>Large</sl-button>
```

### Loading

Use the `loading` prop to make a button busy. The width will remain the same as before, preventing adjacent elements from moving around. Clicks will be suppressed until the loading state is removed.

```html preview
<sl-button type="default" loading>Default</sl-button>
<sl-button type="primary" loading>Primary</sl-button>
<sl-button type="danger" loading>Danger</sl-button>
```

### Disabled

Use the `disabled` prop to disable a button. Clicks will be suppressed until the disabled state is removed.

```html preview
<sl-button type="default" disabled>Default</sl-button>
<sl-button type="primary" disabled>Primary</sl-button>
<sl-button type="danger" disabled>Danger</sl-button>
```

### Checked

Use the `checked` prop to render a button in a 'pushed' state.

```html preview
<sl-button type="default">Default</sl-button>
<sl-button type="default" checked>Default checked</sl-button><br><br>
<sl-button type="primary">Primary</sl-button>
<sl-button type="primary" checked>Primary checked</sl-button><br><br>
<sl-button type="danger">Danger</sl-button>
<sl-button type="danger" checked>Danger checked</sl-button>
```

### Theme

Use `sl-theme` for theming the buttons, fx. dark mode as shown here.

```html preview
<div style="background:black; padding: var(--sl-spacing-medium)" >
<sl-theme name="dark">
<sl-button type="default">Default</sl-button>
<sl-button type="primary">Primary</sl-button>
<sl-button type="danger">Danger</sl-button>
</sl-theme>
</div>
```
[component-metadata:sl-button]
