Files
webawesome/docs/pages/components/button.md

491 lines
12 KiB
Markdown
Raw Normal View History

2023-06-06 08:22:18 -04:00
---
meta:
title: Button
description: Buttons represent actions that are available to the user.
layout: component
---
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button>Button</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
const App = () => <WaButton>Button</WaButton>;
2023-06-06 08:22:18 -04:00
```
## Examples
### Variants
Use the `variant` attribute to set the button's variant.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button variant="brand">Brand</wa-button>
<wa-button variant="success">Success</wa-button>
<wa-button variant="neutral">Neutral</wa-button>
<wa-button variant="warning">Warning</wa-button>
<wa-button variant="danger">Danger</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton variant="brand">Brand</WaButton>
<WaButton variant="success">Success</WaButton>
<WaButton variant="neutral">Neutral</WaButton>
<WaButton variant="warning">Warning</WaButton>
<WaButton variant="danger">Danger</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Sizes
Use the `size` attribute to change a button's size.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button size="small">Small</wa-button>
<wa-button size="medium">Medium</wa-button>
<wa-button size="large">Large</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton size="small">Small</WaButton>
<WaButton size="medium">Medium</WaButton>
<WaButton size="large">Large</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Outline Buttons
Use the `outline` attribute to draw outlined buttons with transparent backgrounds.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button variant="brand" outline>Brand</wa-button>
<wa-button variant="success" outline>Success</wa-button>
<wa-button variant="neutral" outline>Neutral</wa-button>
<wa-button variant="warning" outline>Warning</wa-button>
<wa-button variant="danger" outline>Danger</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton variant="brand" outline>
2023-09-05 12:01:19 -04:00
Brand
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="success" outline>
2023-06-06 08:22:18 -04:00
Success
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="neutral" outline>
2023-06-06 08:22:18 -04:00
Neutral
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="warning" outline>
2023-06-06 08:22:18 -04:00
Warning
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="danger" outline>
2023-06-06 08:22:18 -04:00
Danger
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Pill Buttons
Use the `pill` attribute to give buttons rounded edges.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button size="small" pill>Small</wa-button>
<wa-button size="medium" pill>Medium</wa-button>
<wa-button size="large" pill>Large</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton size="small" pill>
2023-06-06 08:22:18 -04:00
Small
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton size="medium" pill>
2023-06-06 08:22:18 -04:00
Medium
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton size="large" pill>
2023-06-06 08:22:18 -04:00
Large
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Text Buttons
Use the `text` variant to create text buttons that share the same size as regular buttons but don't have backgrounds or borders.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button variant="text" size="small">Text</wa-button>
<wa-button variant="text" size="medium">Text</wa-button>
<wa-button variant="text" size="large">Text</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton variant="text" size="small">
2023-06-06 08:22:18 -04:00
Text
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="text" size="medium">
2023-06-06 08:22:18 -04:00
Text
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="text" size="large">
2023-06-06 08:22:18 -04:00
Text
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### 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. [[CMD/CTRL/SHIFT]] + [[CLICK]]) and exposes the `target` and `download` attributes.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button href="https://example.com/">Link</wa-button>
<wa-button href="https://example.com/" target="_blank">New Window</wa-button>
2023-09-27 16:05:44 -04:00
<wa-button href="/assets/images/logo.svg" download="shoelace.svg">Download</wa-button>
2023-09-08 13:45:49 -04:00
<wa-button href="https://example.com/" disabled>Disabled</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton href="https://example.com/">Link</WaButton>
<WaButton href="https://example.com/" target="_blank">
2023-06-06 08:22:18 -04:00
New Window
2023-09-08 13:45:49 -04:00
</WaButton>
2023-09-27 16:05:44 -04:00
<WaButton href="/assets/images/logo.svg" download="shoelace.svg">
2023-06-06 08:22:18 -04:00
Download
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton href="https://example.com/" disabled>
2023-06-06 08:22:18 -04:00
Disabled
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
:::tip
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 the `width` attribute. This is useful for making buttons span the full width of their container on smaller screens.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button size="small" style="width: 100%; margin-bottom: 1rem;">Small</wa-button>
<wa-button size="medium" style="width: 100%; margin-bottom: 1rem;">Medium</wa-button>
<wa-button size="large" style="width: 100%;">Large</wa-button>
2023-06-06 08:22:18 -04:00
```
{% raw %}
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton size="small" style={{ width: '100%', marginBottom: '1rem' }}>
2023-06-06 08:22:18 -04:00
Small
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton size="medium" style={{ width: '100%', marginBottom: '1rem' }}>
2023-06-06 08:22:18 -04:00
Medium
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton size="large" style={{ width: '100%' }}>
2023-06-06 08:22:18 -04:00
Large
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
{% endraw %}
### Prefix and Suffix Icons
Use the `prefix` and `suffix` slots to add icons.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button size="small">
2023-12-05 17:37:06 -05:00
<wa-icon slot="prefix" name="gear" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Settings
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<wa-button size="small">
2023-12-05 17:37:06 -05:00
<wa-icon slot="suffix" name="undo" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Refresh
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<wa-button size="small">
2023-12-05 17:37:06 -05:00
<wa-icon slot="prefix" name="link" variant="solid"></wa-icon>
<wa-icon slot="suffix" name="arrow-up-right-from-square" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Open
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
<br /><br />
2023-09-08 13:45:49 -04:00
<wa-button>
2023-12-05 17:37:06 -05:00
<wa-icon slot="prefix" name="gear" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Settings
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<wa-button>
2023-12-05 17:37:06 -05:00
<wa-icon slot="suffix" name="undo" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Refresh
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<wa-button>
2023-12-05 17:37:06 -05:00
<wa-icon slot="prefix" name="link" variant="solid"></wa-icon>
<wa-icon slot="suffix" name="arrow-up-right-from-square" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Open
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
<br /><br />
2023-09-08 13:45:49 -04:00
<wa-button size="large">
2023-12-05 17:37:06 -05:00
<wa-icon slot="prefix" name="gear" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Settings
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<wa-button size="large">
2023-12-05 17:37:06 -05:00
<wa-icon slot="suffix" name="undo" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Refresh
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<wa-button size="large">
2023-12-05 17:37:06 -05:00
<wa-icon slot="prefix" name="link" variant="solid"></wa-icon>
<wa-icon slot="suffix" name="arrow-up-right-from-square" variant="solid"></wa-icon>
2023-06-06 08:22:18 -04:00
Open
2023-09-08 13:45:49 -04:00
</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
import WaIcon from '@shoelace-style/shoelace/dist/react/icon';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton size="small">
2023-12-05 17:37:06 -05:00
<WaIcon slot="prefix" name="gear" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Settings
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton size="small">
2023-12-05 17:37:06 -05:00
<WaIcon slot="suffix" name="undo" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Refresh
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton size="small">
2023-12-05 17:37:06 -05:00
<WaIcon slot="prefix" name="link" variant="solid"></WaIcon>
<WaIcon slot="suffix" name="arrow-up-right-from-square" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Open
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
<br />
<br />
2023-09-08 13:45:49 -04:00
<WaButton>
2023-12-05 17:37:06 -05:00
<WaIcon slot="prefix" name="gear" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Settings
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton>
2023-12-05 17:37:06 -05:00
<WaIcon slot="suffix" name="undo" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Refresh
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton>
2023-12-05 17:37:06 -05:00
<WaIcon slot="prefix" name="link" variant="solid"></WaIcon>
<WaIcon slot="suffix" name="arrow-up-right-from-square" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Open
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
<br />
<br />
2023-09-08 13:45:49 -04:00
<WaButton size="large">
2023-12-05 17:37:06 -05:00
<WaIcon slot="prefix" name="gear" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Settings
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton size="large">
2023-12-05 17:37:06 -05:00
<WaIcon slot="suffix" name="undo" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Refresh
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton size="large">
2023-12-05 17:37:06 -05:00
<WaIcon slot="prefix" name="link" variant="solid"></WaIcon>
<WaIcon slot="suffix" name="arrow-up-right-from-square" variant="solid"></WaIcon>
2023-06-06 08:22:18 -04:00
Open
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Caret
Use the `caret` attribute to add a dropdown indicator when a button will trigger a dropdown, menu, or popover.
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button size="small" caret>Small</wa-button>
<wa-button size="medium" caret>Medium</wa-button>
<wa-button size="large" caret>Large</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton size="small" caret>
2023-06-06 08:22:18 -04:00
Small
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton size="medium" caret>
2023-06-06 08:22:18 -04:00
Medium
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton size="large" caret>
2023-06-06 08:22:18 -04:00
Large
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Loading
2024-01-24 13:21:27 -05:00
Use the `loading` attribute to make a button busy. The width will remain the same as before, preventing adjacent elements from moving around.
2023-06-06 08:22:18 -04:00
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button variant="brand" loading>Brand</wa-button>
<wa-button variant="success" loading>Success</wa-button>
<wa-button variant="neutral" loading>Neutral</wa-button>
<wa-button variant="warning" loading>Warning</wa-button>
<wa-button variant="danger" loading>Danger</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton variant="brand" loading>
2023-09-05 12:01:19 -04:00
Brand
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="success" loading>
2023-06-06 08:22:18 -04:00
Success
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="neutral" loading>
2023-06-06 08:22:18 -04:00
Neutral
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="warning" loading>
2023-06-06 08:22:18 -04:00
Warning
2023-09-08 13:45:49 -04:00
</WaButton>
<WaButton variant="danger" loading>
2023-06-06 08:22:18 -04:00
Danger
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Disabled
Use the `disabled` attribute to disable a button.
2023-06-06 08:22:18 -04:00
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button variant="brand" disabled>Brand</wa-button>
<wa-button variant="success" disabled>Success</wa-button>
<wa-button variant="neutral" disabled>Neutral</wa-button>
<wa-button variant="warning" disabled>Warning</wa-button>
<wa-button variant="danger" disabled>Danger</wa-button>
2023-06-06 08:22:18 -04:00
```
```jsx:react
2023-09-08 13:45:49 -04:00
import WaButton from '@shoelace-style/shoelace/dist/react/button';
2023-06-06 08:22:18 -04:00
const App = () => (
<>
2023-09-08 13:45:49 -04:00
<WaButton variant="brand" disabled>
2023-09-05 12:01:19 -04:00
Brand
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton variant="success" disabled>
2023-06-06 08:22:18 -04:00
Success
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton variant="neutral" disabled>
2023-06-06 08:22:18 -04:00
Neutral
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton variant="warning" disabled>
2023-06-06 08:22:18 -04:00
Warning
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
2023-09-08 13:45:49 -04:00
<WaButton variant="danger" disabled>
2023-06-06 08:22:18 -04:00
Danger
2023-09-08 13:45:49 -04:00
</WaButton>
2023-06-06 08:22:18 -04:00
</>
);
```
### Styling Buttons
2023-09-08 13:45:49 -04:00
This example demonstrates how to style buttons using a custom class. This is the recommended approach if you need to add additional variations. To customize an existing variation, modify the selector to target the button's `variant` attribute instead of a class (e.g. `wa-button[variant="brand"]`).
2023-06-06 08:22:18 -04:00
```html:preview
2023-09-08 13:45:49 -04:00
<wa-button class="pink">Pink Button</wa-button>
2023-06-06 08:22:18 -04:00
<style>
2023-09-08 13:45:49 -04:00
wa-button.pink::part(base) {
2023-09-05 12:01:19 -04:00
border-radius: 6px;
border: solid 2px;
background: #ff1493;
2023-06-06 08:22:18 -04:00
border-top-color: #ff7ac1;
border-left-color: #ff7ac1;
border-bottom-color: #ad005c;
border-right-color: #ad005c;
color: white;
font-size: 1.125rem;
box-shadow: 0 2px 10px #0002;
2023-09-05 12:01:19 -04:00
transition: var(--wa-transition-normal) all;
2023-06-06 08:22:18 -04:00
}
2023-09-08 13:45:49 -04:00
wa-button.pink::part(base):hover {
2023-09-05 12:01:19 -04:00
transform: scale(1.05);
2023-06-06 08:22:18 -04:00
}
2023-09-08 13:45:49 -04:00
wa-button.pink::part(base):active {
2023-06-06 08:22:18 -04:00
border-top-color: #ad005c;
border-right-color: #ff7ac1;
border-bottom-color: #ff7ac1;
border-left-color: #ad005c;
2023-09-05 12:01:19 -04:00
transform: translateY(1px);
2023-06-06 08:22:18 -04:00
}
2023-09-08 13:45:49 -04:00
wa-button.pink::part(base):focus-visible {
2023-06-06 08:22:18 -04:00
outline: dashed 2px deeppink;
outline-offset: 4px;
}
</style>
```