shoelace/docs/components/button.md

531 wiersze
14 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Button
[component-header:sl-button]
Buttons represent actions that are available to the user.
```html preview
<sl-button>Button</sl-button>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlButton>Button</SlButton>;
2021-11-04 22:12:47 +00:00
```
2020-07-15 21:30:37 +00:00
## Examples
2021-12-13 22:38:40 +00:00
### Variants
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
Use the `variant` attribute to set the button's variant.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default">Default</sl-button>
<sl-button variant="primary">Primary</sl-button>
<sl-button variant="success">Success</sl-button>
<sl-button variant="neutral">Neutral</sl-button>
<sl-button variant="warning">Warning</sl-button>
<sl-button variant="danger">Danger</sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2021-12-13 22:38:40 +00:00
<SlButton variant="default">Default</SlButton>
<SlButton variant="primary">Primary</SlButton>
<SlButton variant="success">Success</SlButton>
<SlButton variant="neutral">Neutral</SlButton>
<SlButton variant="warning">Warning</SlButton>
2022-03-02 15:10:41 +00:00
<SlButton variant="danger">Danger</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Sizes
2021-07-08 21:41:10 +00:00
Use the `size` attribute to change a button's size.
2020-07-15 21:30:37 +00:00
```html preview
<sl-button size="small">Small</sl-button>
<sl-button size="medium">Medium</sl-button>
<sl-button size="large">Large</sl-button>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
<SlButton size="small">Small</SlButton>
<SlButton size="medium">Medium</SlButton>
<SlButton size="large">Large</SlButton>
</>
);
```
2021-09-24 20:40:03 +00:00
### Outline Buttons
Use the `outline` attribute to draw outlined buttons with transparent backgrounds.
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default" outline>Default</sl-button>
<sl-button variant="primary" outline>Primary</sl-button>
<sl-button variant="success" outline>Success</sl-button>
<sl-button variant="neutral" outline>Neutral</sl-button>
<sl-button variant="warning" outline>Warning</sl-button>
<sl-button variant="danger" outline>Danger</sl-button>
2021-09-24 20:40:03 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton variant="default" outline>
Default
</SlButton>
<SlButton variant="primary" outline>
Primary
</SlButton>
<SlButton variant="success" outline>
Success
</SlButton>
<SlButton variant="neutral" outline>
Neutral
</SlButton>
<SlButton variant="warning" outline>
Warning
</SlButton>
<SlButton variant="danger" outline>
Danger
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Pill Buttons
2021-07-08 21:41:10 +00:00
Use the `pill` attribute to give buttons rounded edges.
2020-07-15 21:30:37 +00:00
```html preview
<sl-button size="small" pill>Small</sl-button>
<sl-button size="medium" pill>Medium</sl-button>
<sl-button size="large" pill>Large</sl-button>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton size="small" pill>
Small
</SlButton>
<SlButton size="medium" pill>
Medium
</SlButton>
<SlButton size="large" pill>
Large
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Circle Buttons
2021-07-08 21:41:10 +00:00
Use the `circle` attribute to create circular icon buttons.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="small" circle><sl-icon name="gear"></sl-icon></sl-button>
<sl-button variant="default" size="medium" circle><sl-icon name="gear"></sl-icon></sl-button>
<sl-button variant="default" size="large" circle><sl-icon name="gear"></sl-icon></sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton variant="default" size="small" circle>
<SlIcon name="gear" />
</SlButton>
<SlButton variant="default" size="medium" circle>
<SlIcon name="gear" />
</SlButton>
<SlButton variant="default" size="large" circle>
<SlIcon name="gear" />
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Text Buttons
2021-12-13 22:38:40 +00:00
Use the `text` variant to create text buttons that share the same size as regular buttons but don't have backgrounds or borders.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="text" size="small">Text</sl-button>
<sl-button variant="text" size="medium">Text</sl-button>
<sl-button variant="text" size="large">Text</sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton variant="text" size="small">
Text
</SlButton>
<SlButton variant="text" size="medium">
Text
</SlButton>
<SlButton variant="text" size="large">
Text
</SlButton>
2021-11-04 22:12:47 +00: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. <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="shoelace.svg">Download</sl-button>
<sl-button href="https://example.com/" disabled>Disabled</sl-button>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
<SlButton href="https://example.com/">Link</SlButton>
2022-03-02 15:10:41 +00:00
<SlButton href="https://example.com/" target="_blank">
New Window
</SlButton>
<SlButton href="/assets/images/wordmark.svg" download="shoelace.svg">
Download
</SlButton>
<SlButton href="https://example.com/" disabled>
Disabled
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
?> When a `target` is set, the link will receive `rel="noreferrer noopener"` for [security reasons](https://mathiasbynens.github.io/rel-noopener/).
2020-07-15 21:30:37 +00:00
### Setting a Custom Width
2021-11-22 13:21:38 +00:00
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.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="small" style="width: 100%; margin-bottom: 1rem;">Small</sl-button>
<sl-button variant="default" size="medium" style="width: 100%; margin-bottom: 1rem;">Medium</sl-button>
<sl-button variant="default" size="large" style="width: 100%;">Large</sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton variant="default" size="small" style={{ width: '100%', marginBottom: '1rem' }}>
Small
</SlButton>
<SlButton variant="default" size="medium" style={{ width: '100%', marginBottom: '1rem' }}>
Medium
</SlButton>
<SlButton variant="default" size="large" style={{ width: '100%' }}>
Large
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Prefix and Suffix Icons
Use the `prefix` and `suffix` slots to add icons.
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="small">
2020-12-22 22:40:37 +00:00
<sl-icon slot="prefix" name="gear"></sl-icon>
Settings
</sl-button>
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="small">
2020-12-22 22:40:37 +00:00
<sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
Refresh
</sl-button>
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="small">
2020-12-22 22:40:37 +00:00
<sl-icon slot="prefix" name="link-45deg"></sl-icon>
<sl-icon slot="suffix" name="box-arrow-up-right"></sl-icon>
Open
</sl-button>
2022-03-02 15:10:41 +00:00
<br /><br />
2020-12-22 22:40:37 +00:00
2021-12-13 22:38:40 +00:00
<sl-button variant="default">
2020-07-15 21:30:37 +00:00
<sl-icon slot="prefix" name="gear"></sl-icon>
Settings
</sl-button>
2021-12-13 22:38:40 +00:00
<sl-button variant="default">
2020-07-15 21:30:37 +00:00
<sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
Refresh
</sl-button>
2021-12-13 22:38:40 +00:00
<sl-button variant="default">
2020-07-15 21:30:37 +00:00
<sl-icon slot="prefix" name="link-45deg"></sl-icon>
<sl-icon slot="suffix" name="box-arrow-up-right"></sl-icon>
Open
</sl-button>
2020-12-22 22:40:37 +00:00
2022-03-02 15:10:41 +00:00
<br /><br />
2020-12-22 22:40:37 +00:00
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="large">
2020-12-22 22:40:37 +00:00
<sl-icon slot="prefix" name="gear"></sl-icon>
Settings
</sl-button>
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="large">
2020-12-22 22:40:37 +00:00
<sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
Refresh
</sl-button>
2021-12-13 22:38:40 +00:00
<sl-button variant="default" size="large">
2020-12-22 22:40:37 +00:00
<sl-icon slot="prefix" name="link-45deg"></sl-icon>
<sl-icon slot="suffix" name="box-arrow-up-right"></sl-icon>
Open
</sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2021-12-13 22:38:40 +00:00
<SlButton variant="default" size="small">
2021-11-04 22:12:47 +00:00
<SlIcon slot="prefix" name="gear"></SlIcon>
Settings
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="default" size="small">
2021-11-04 22:12:47 +00:00
<SlIcon slot="suffix" name="arrow-counterclockwise"></SlIcon>
Refresh
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="default" size="small">
2021-11-04 22:12:47 +00:00
<SlIcon slot="prefix" name="link-45deg"></SlIcon>
<SlIcon slot="suffix" name="box-arrow-up-right"></SlIcon>
Open
</SlButton>
2022-03-02 15:10:41 +00:00
<br />
<br />
2021-11-04 22:12:47 +00:00
2021-12-13 22:38:40 +00:00
<SlButton variant="default">
2021-11-04 22:12:47 +00:00
<SlIcon slot="prefix" name="gear"></SlIcon>
Settings
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="default">
2021-11-04 22:12:47 +00:00
<SlIcon slot="suffix" name="arrow-counterclockwise"></SlIcon>
Refresh
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="default">
2021-11-04 22:12:47 +00:00
<SlIcon slot="prefix" name="link-45deg"></SlIcon>
<SlIcon slot="suffix" name="box-arrow-up-right"></SlIcon>
Open
</SlButton>
2022-03-02 15:10:41 +00:00
<br />
<br />
2021-11-04 22:12:47 +00:00
2021-12-13 22:38:40 +00:00
<SlButton variant="default" size="large">
2021-11-04 22:12:47 +00:00
<SlIcon slot="prefix" name="gear"></SlIcon>
Settings
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="default" size="large">
2021-11-04 22:12:47 +00:00
<SlIcon slot="suffix" name="arrow-counterclockwise"></SlIcon>
Refresh
</SlButton>
2021-12-13 22:38:40 +00:00
<SlButton variant="default" size="large">
2021-11-04 22:12:47 +00:00
<SlIcon slot="prefix" name="link-45deg"></SlIcon>
<SlIcon slot="suffix" name="box-arrow-up-right"></SlIcon>
Open
</SlButton>
</>
);
```
2020-07-15 21:30:37 +00:00
### Caret
2021-07-08 21:41:10 +00:00
Use the `caret` attribute to add a dropdown indicator when a button will trigger a dropdown, menu, or popover.
2020-07-15 21:30:37 +00:00
```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>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton size="small" caret>
Small
</SlButton>
<SlButton size="medium" caret>
Medium
</SlButton>
<SlButton size="large" caret>
Large
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Loading
2021-07-08 21:41:10 +00:00
Use the `loading` attribute 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.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default" loading>Default</sl-button>
<sl-button variant="primary" loading>Primary</sl-button>
<sl-button variant="success" loading>Success</sl-button>
<sl-button variant="neutral" loading>Neutral</sl-button>
<sl-button variant="warning" loading>Warning</sl-button>
<sl-button variant="danger" loading>Danger</sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton variant="default" loading>
Default
</SlButton>
<SlButton variant="primary" loading>
Primary
</SlButton>
<SlButton variant="success" loading>
Success
</SlButton>
<SlButton variant="neutral" loading>
Neutral
</SlButton>
<SlButton variant="warning" loading>
Warning
</SlButton>
<SlButton variant="danger" loading>
Danger
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Disabled
2021-07-08 21:41:10 +00:00
Use the `disabled` attribute to disable a button. Clicks will be suppressed until the disabled state is removed.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-13 22:38:40 +00:00
<sl-button variant="default" disabled>Default</sl-button>
<sl-button variant="primary" disabled>Primary</sl-button>
<sl-button variant="success" disabled>Success</sl-button>
<sl-button variant="neutral" disabled>Neutral</sl-button>
<sl-button variant="warning" disabled>Warning</sl-button>
<sl-button variant="danger" disabled>Danger</sl-button>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlButton } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlButton variant="default" disabled>
Default
</SlButton>
<SlButton variant="primary" disabled>
Primary
</SlButton>
<SlButton variant="success" disabled>
Success
</SlButton>
<SlButton variant="neutral" disabled>
Neutral
</SlButton>
<SlButton variant="warning" disabled>
Warning
</SlButton>
<SlButton variant="danger" disabled>
Danger
</SlButton>
2021-11-04 22:12:47 +00:00
</>
);
```
2021-04-05 20:59:11 +00:00
### Styling Buttons
2021-12-13 22:38:40 +00: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. `sl-button[variant="primary"]`).
2021-04-05 20:59:11 +00:00
```html preview
<sl-button class="pink">Pink Button</sl-button>
<style>
sl-button.pink::part(base) {
/* Set design tokens for height and border width */
--sl-input-height-medium: 48px;
--sl-input-border-width: 4px;
2022-03-02 15:10:41 +00:00
2021-04-05 20:59:11 +00:00
border-radius: 0;
background-color: #ff1493;
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;
transition: var(--sl-transition-medium) transform ease, var(--sl-transition-medium) border ease;
}
sl-button.pink::part(base):hover {
transform: scale(1.05) rotate(-1deg);
}
sl-button.pink::part(base):active {
border-top-color: #ad005c;
border-right-color: #ff7ac1;
border-bottom-color: #ff7ac1;
border-left-color: #ad005c;
transform: scale(1.05) rotate(-1deg) translateY(2px);
}
sl-button.pink::part(base):focus-visible {
outline: dashed 2px deeppink;
outline-offset: 4px;
}
</style>
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-button]