Button
Shoelace의 Button 컴포넌트(sl-button)는 사용자가 사용할 수 있는 동작(actions)을 나타내는 요소예요. variant·size·outline·pill·circle·loading 등 다양한 모양으로 버튼을 만들 수 있고, href를 설정하면 링크처럼 동작하게 할 수도 있어요.
출처: 문서
본문
Button은 사용자에게 제공되는 동작을 나타내요.
<sl-button>Button</sl-button>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => <SlButton>Button</SlButton>;
Examples
Variants
variant 속성(attribute)을 사용해 버튼의 변형(variant)을 설정할 수 있어요.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<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>
<SlButton variant="danger">Danger</SlButton>
</>
);
Sizes
size 속성(attribute)을 사용해 버튼 크기를 바꿀 수 있어요.
<sl-button size="small">Small</sl-button>
<sl-button size="medium">Medium</sl-button>
<sl-button size="large">Large</sl-button>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<SlButton size="small">Small</SlButton>
<SlButton size="medium">Medium</SlButton>
<SlButton size="large">Large</SlButton>
</>
);
Outline Buttons
outline 속성(attribute)을 사용하면 투명한 배경에 윤곽선만 있는 버튼을 그릴 수 있어요.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<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>
</>
);
Pill Buttons
pill 속성(attribute)을 사용하면 버튼에 둥근 모서리를 줄 수 있어요.
<sl-button size="small" pill>Small</sl-button>
<sl-button size="medium" pill>Medium</sl-button>
<sl-button size="large" pill>Large</sl-button>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<SlButton size="small" pill>
Small
</SlButton>
<SlButton size="medium" pill>
Medium
</SlButton>
<SlButton size="large" pill>
Large
</SlButton>
</>
);
Circle Buttons
circle 속성(attribute)을 사용하면 원형의 아이콘 버튼을 만들 수 있어요. 이 속성이 설정되면 버튼은 기본 슬롯(default slot)에 단 하나의 sl-icon을 기대해요.
<sl-button variant="default" size="small" circle>
<sl-icon name="gear" label="Settings"></sl-icon>
</sl-button>
<sl-button variant="default" size="medium" circle>
<sl-icon name="gear" label="Settings"></sl-icon>
</sl-button>
<sl-button variant="default" size="large" circle>
<sl-icon name="gear" label="Settings"></sl-icon>
</sl-button>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
const App = () => (
<>
<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>
</>
);
Text Buttons
text 변형(variant)을 사용하면 일반 버튼과 같은 크기이지만 배경이나 테두리가 없는 텍스트 버튼을 만들 수 있어요.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<SlButton variant="text" size="small">
Text
</SlButton>
<SlButton variant="text" size="medium">
Text
</SlButton>
<SlButton variant="text" size="large">
Text
</SlButton>
</>
);
Link Buttons
링크처럼 동작하는 버튼이 있으면 종종 유용해요. href 속성(attribute)을 설정하면 컴포넌트가 내부적으로 <a>를 렌더링해서 가능해져요. 이렇게 하면 브라우저가 제공하는 기본 링크 동작(예: CMD/CTRL/SHIFT + CLICK)을 모두 얻게 되고, target와 download 속성도 사용할 수 있게 돼요.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<SlButton href="https://example.com/">Link</SlButton>
<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>
</>
);
target이 설정되면 보안상의 이유로 링크에 rel="noreferrer noopener"가 적용돼요.
Setting a Custom Width
예상한 대로, 인라인 스타일(또는 클래스)을 컴포넌트에 전달하면 버튼에 사용자 지정 너비를 줄 수 있어요. 작은 화면에서 버튼이 컨테이너의 전체 너비에 걸치게 만들 때 특히 유용하답니다.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<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>
</>
);
Prefix and Suffix Icons
prefix와 suffix 슬롯을 사용해 아이콘을 추가할 수 있어요.
<sl-button variant="default" size="small">
<sl-icon slot="prefix" name="gear"></sl-icon>
Settings
</sl-button>
<sl-button variant="default" size="small">
<sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
Refresh
</sl-button>
<sl-button variant="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 variant="default">
<sl-icon slot="prefix" name="gear"></sl-icon>
Settings
</sl-button>
<sl-button variant="default">
<sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
Refresh
</sl-button>
<sl-button variant="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 variant="default" size="large">
<sl-icon slot="prefix" name="gear"></sl-icon>
Settings
</sl-button>
<sl-button variant="default" size="large">
<sl-icon slot="suffix" name="arrow-counterclockwise"></sl-icon>
Refresh
</sl-button>
<sl-button variant="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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
const App = () => (
<>
<SlButton variant="default" size="small">
<SlIcon slot="prefix" name="gear"></SlIcon>
Settings
</SlButton>
<SlButton variant="default" size="small">
<SlIcon slot="suffix" name="arrow-counterclockwise"></SlIcon>
Refresh
</SlButton>
<SlButton variant="default" size="small">
<SlIcon slot="prefix" name="link-45deg"></SlIcon>
<SlIcon slot="suffix" name="box-arrow-up-right"></SlIcon>
Open
</SlButton>
<br />
<br />
<SlButton variant="default">
<SlIcon slot="prefix" name="gear"></SlIcon>
Settings
</SlButton>
<SlButton variant="default">
<SlIcon slot="suffix" name="arrow-counterclockwise"></SlIcon>
Refresh
</SlButton>
<SlButton variant="default">
<SlIcon slot="prefix" name="link-45deg"></SlIcon>
<SlIcon slot="suffix" name="box-arrow-up-right"></SlIcon>
Open
</SlButton>
<br />
<br />
<SlButton variant="default" size="large">
<SlIcon slot="prefix" name="gear"></SlIcon>
Settings
</SlButton>
<SlButton variant="default" size="large">
<SlIcon slot="suffix" name="arrow-counterclockwise"></SlIcon>
Refresh
</SlButton>
<SlButton variant="default" size="large">
<SlIcon slot="prefix" name="link-45deg"></SlIcon>
<SlIcon slot="suffix" name="box-arrow-up-right"></SlIcon>
Open
</SlButton>
</>
);
Caret
caret 속성(attribute)을 사용하면 버튼이 드롭다운(dropdown), 메뉴, 팝오버(popover)를 트리거할 때 드롭다운 표시자를 추가할 수 있어요.
<sl-button size="small" caret>Small</sl-button>
<sl-button size="medium" caret>Medium</sl-button>
<sl-button size="large" caret>Large</sl-button>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<SlButton size="small" caret>
Small
</SlButton>
<SlButton size="medium" caret>
Medium
</SlButton>
<SlButton size="large" caret>
Large
</SlButton>
</>
);
Loading
loading 속성(attribute)을 사용하면 버튼을 "작업 중(busy)" 상태로 만들 수 있어요. 너비는 이전과 동일하게 유지되어 주변 요소가 움직이는 것을 막아줘요.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<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>
</>
);
Disabled
disabled 속성(attribute)을 사용하면 버튼을 비활성화할 수 있어요.
<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>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
const App = () => (
<>
<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>
</>
);
Styling Buttons
이 예시는 커스텀 클래스를 사용해 버튼을 스타일링하는 방법을 보여줘요. 추가 변형(variation)이 필요할 때 권장되는 방법이에요. 기존 변형을 커스터마이즈하려면 클래스 대신 버튼의 variant 속성(attribute)을 대상으로 지정하도록 셀렉터를 수정하면 돼요 (예: sl-button[variant="primary"]).
<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;
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>
Importing
autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 무시해도 돼요. 그 외의 경우 아래 스니펫 중 하나를 골라 이 컴포넌트만 골라(cherry pick) 가져올 수 있답니다.
Script / Import / Bundler / React
CDN에서 script 태그로 이 컴포넌트를 가져오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/button/button.js"></script>
CDN에서 JavaScript import로 이 컴포넌트를 가져오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/button/button.js';
bundler를 사용해 이 컴포넌트를 가져오려면:
import '@shoelace-style/shoelace/dist/components/button/button.js';
이 컴포넌트를 React 컴포넌트로 가져오려면:
import SlButton from '@shoelace-style/shoelace/dist/react/button';
Slots
| Name | Description |
|---|---|
| (default) | The button’s label. |
prefix |
A presentational prefix icon or similar element. |
suffix |
A presentational suffix icon or similar element. |
슬롯(slots) 사용에 대해 더 알아보기.
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
variant |
The button’s theme variant. | 'default' | 'primary' | 'success' | 'neutral' | 'warning' | 'danger' | 'text' |
'default' |
|
size |
The button’s size. | 'small' | 'medium' | 'large' |
'medium' |
|
caret |
Draws the button with a caret. Used to indicate that the button triggers a dropdown menu or similar behavior. | boolean |
false |
|
disabled |
Disables the button. | boolean |
false |
|
loading |
Draws the button in a loading state. | boolean |
false |
|
outline |
Draws an outlined button. | boolean |
false |
|
pill |
Draws a pill-style button with rounded edges. | boolean |
false |
|
circle |
Draws a circular icon button. When this attribute is present, the button expects a single sl-icon in the default slot. |
boolean |
false |
|
type |
The type of button. Note that the default value is button instead of submit, which is opposite of how native <button> elements behave. When the type is submit, the button will submit the surrounding form. |
'button' | 'submit' | 'reset' |
'button' |
|
name |
The name of the button, submitted as a name/value pair with form data, but only when this button is the submitter. This attribute is ignored when href is present. |
string |
'' |
|
value |
The value of the button, submitted as a pair with the button’s name as part of the form data, but only when this button is the submitter. This attribute is ignored when href is present. |
string |
'' |
|
href |
When set, the underlying button will be rendered as an <a> with this href instead of a <button>. |
string |
'' |
|
target |
Tells the browser where to open the link. Only used when href is present. |
'_blank' | '_parent' | '_self' | '_top' |
- | |
rel |
When using href, this attribute will map to the underlying link’s rel attribute. Unlike regular links, the default is noreferrer noopener to prevent security exploits. However, if you’re using target to point to a specific tab/window, this will prevent that from working correctly. You can remove or change the default value by setting the attribute to an empty string or a value of your choice, respectively. |
string |
'noreferrer noopener' |
|
download |
Tells the browser to download the linked file as this filename. Only used when href is present. |
string | undefined |
- | |
form |
The “form owner” to associate the button with. If omitted, the closest containing form will be used instead. The value of this attribute must be an id of a form in the same document or shadow root as the button. | string |
- | |
formAction / formaction |
Used to override the form owner’s action attribute. |
string |
- | |
formEnctype / formenctype |
Used to override the form owner’s enctype attribute. |
'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text/plain' |
- | |
formMethod / formmethod |
Used to override the form owner’s method attribute. |
'post' | 'get' |
- | |
formNoValidate / formnovalidate |
Used to override the form owner’s novalidate attribute. |
boolean |
- | |
formTarget / formtarget |
Used to override the form owner’s target attribute. |
'_self' | '_blank' | '_parent' | '_top' | string |
- | |
validity |
Gets the validity state object | - | - | |
validationMessage |
Gets the validation message | - | - | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
속성과 프로퍼티(attributes and properties)에 대해 더 알아보기.
Events
| Name | React Event | Description | Event Detail |
|---|---|---|---|
sl-blur |
onSlBlur |
Emitted when the button loses focus. | - |
sl-focus |
onSlFocus |
Emitted when the button gains focus. | - |
sl-invalid |
onSlInvalid |
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | - |
이벤트(events)에 대해 더 알아보기.
Methods
| Name | Description | Arguments |
|---|---|---|
click() |
Simulates a click on the button. | - |
focus() |
Sets focus on the button. | options: FocusOptions |
blur() |
Removes focus from the button. | - |
checkValidity() |
Checks for validity but does not show a validation message. Returns true when valid and false when invalid. |
- |
getForm() |
Gets the associated form, if one exists. | - |
reportValidity() |
Checks for validity and shows the browser’s validation message if the control is invalid. | - |
setCustomValidity() |
Sets a custom validation message. Pass an empty string to restore validity. | message: string |
메서드(methods)에 대해 더 알아보기.
Parts
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
prefix |
The container that wraps the prefix. |
label |
The button’s label. |
suffix |
The container that wraps the suffix. |
caret |
The button’s caret icon, an sl-icon element. |
spinner |
The spinner that shows when the button is in the loading state. |
CSS 파트(parts) 커스터마이징에 대해 더 알아보기.
Dependencies
이 컴포넌트는 다음 의존성들을 자동으로 가져와요.
sl-iconsl-spinner
더 알아보기 (Learn more)
- Shoelace Button Group 컴포넌트 — 버튼을 그룹으로 묶는 방법
- Shoelace Icon 컴포넌트 — 버튼 아이콘으로 쓰는 아이콘 문서
- Shoelace 시작하기(getting started) — 컴포넌트 설치와 로딩 방법