스위치 (Switch)
SlSwitch는 사용자가 옵션을 켜고 끄는 토글을 할 수 있게 해주는 요소예요. 설정 항목을 켜거나 끌 때 유용합니다.
출처: 문서
본문
<sl-switch>Switch</sl-switch>
React에서는 이렇게 사용할 수 있어요.
import SlSwitch from '@shoelace-style/shoelace/dist/react/switch';
const App = () => <SlSwitch>Switch</SlSwitch>;
이 컴포넌트는 표준 `` 요소와 함께 동작해요. 폼 제출(form submission)과 클라이언트 측 유효성 검사(client-side validation)에 대해 더 자세히 알고 싶다면 폼 컨트롤 섹션을 참고하세요.
예시 (Examples)
체크됨 (Checked)
checked 속성을 사용하면 스위치를 켠 상태로 만들 수 있어요.
<sl-switch checked>Checked</sl-switch>
import SlSwitch from '@shoelace-style/shoelace/dist/react/switch';
const App = () => <SlSwitch checked>Checked</SlSwitch>;
비활성화 (Disabled)
disabled 속성을 사용하면 스위치를 비활성화할 수 있어요.
<sl-switch disabled>Disabled</sl-switch>
import SlSwitch from '@shoelace-style/shoelace/dist/react/switch';
const App = () => <SlSwitch disabled>Disabled</SlSwitch>;
크기 (Sizes)
size 속성을 사용하면 스위치의 크기를 바꿀 수 있어요.
<sl-switch size="small">Small</sl-switch>
<br />
<sl-switch size="medium">Medium</sl-switch>
<br />
<sl-switch size="large">Large</sl-switch>
import SlSwitch from '@shoelace-style/shoelace/dist/react/switch';
const App = () => (
<>
<SlSwitch size="small">Small</SlSwitch>
<br />
<SlSwitch size="medium">Medium</SlSwitch>
<br />
<SlSwitch size="large">Large</SlSwitch>
</>
);
도움말 텍스트 (Help Text)
help-text 속성을 사용하면 스위치에 설명용 도움말 텍스트를 추가할 수 있어요. HTML이 포함된 도움말이 필요하다면 help-text 슬롯을 대신 사용하세요.
<sl-switch help-text="What should the user know about the switch?">Label</sl-switch>
import SlSwitch from '@shoelace-style/shoelace/dist/react/checkbox';
const App = () => <SlSwitch help-text="What should the user know about the switch?">Label</SlSwitch>;
커스텀 스타일 (Custom Styles)
제공되는 커스텀 프로퍼티를 사용해 스위치의 스타일을 바꿀 수 있어요.
<sl-switch style="--width: 80px; --height: 40px; --thumb-size: 36px;">Really big</sl-switch>
import SlSwitch from '@shoelace-style/shoelace/dist/react/switch';
const App = () => (
<SlSwitch
style={{
'--width': '80px',
'--height': '32px',
'--thumb-size': '26px'
}}
/>
);
불러오기 (Importing)
autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.
Script 태그로 CDN에서 이 컴포넌트를 불러오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/switch/switch.js"></script>
JavaScript import 구문으로 CDN에서 불러오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/switch/switch.js';
번들러(bundler)를 사용해 불러오려면:
import '@shoelace-style/shoelace/dist/components/switch/switch.js';
React 컴포넌트로 불러오려면:
import SlSwitch from '@shoelace-style/shoelace/dist/react/switch';
슬롯 (Slots)
| Name | Description |
|---|---|
| (default) | The switch’s label. |
help-text |
Text that describes how to use the switch. Alternatively, you can use the help-text attribute. |
슬롯 사용법에 대해 더 알아보기.
속성 (Properties)
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
name |
The name of the switch, submitted as a name/value pair with form data. | string |
'' |
|
value |
The current value of the switch, submitted as a name/value pair with form data. | string |
- | |
size |
The switch’s size. | 'small' | 'medium' | 'large' |
'medium' |
|
disabled |
Disables the switch. | boolean |
false |
|
checked |
Draws the switch in a checked state. | boolean |
false |
|
defaultChecked |
The default value of the form control. Primarily used for resetting the form control. | boolean |
false |
|
form |
By default, form controls are associated with the nearest containing `` element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. |
string |
'' |
|
required |
Makes the switch a required field. | boolean |
false |
|
helpText help-text |
The switch’s help text. If you need to display HTML, use the help-text slot instead. |
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. |
속성(attribute)과 프로퍼티(property)에 대해 더 알아보기.
이벤트 (Events)
| Name | React Event | Description | Event Detail |
|---|---|---|---|
sl-blur |
onSlBlur |
Emitted when the control loses focus. | - |
sl-change |
onSlChange |
Emitted when the control’s checked state changes. | - |
sl-input |
onSlInput |
Emitted when the control receives input. | - |
sl-focus |
onSlFocus |
Emitted when the control gains focus. | - |
sl-invalid |
onSlInvalid |
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | - |
이벤트에 대해 더 알아보기.
메서드 (Methods)
| Name | Description | Arguments |
|---|---|---|
click() |
Simulates a click on the switch. | - |
focus() |
Sets focus on the switch. | options: FocusOptions |
blur() |
Removes focus from the switch. | - |
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 |
메서드에 대해 더 알아보기.
커스텀 프로퍼티 (Custom Properties)
| Name | Description | Default |
|---|---|---|
--width |
The width of the switch. | |
--height |
The height of the switch. | |
--thumb-size |
The size of the thumb. |
CSS 커스텀 프로퍼티 커스터마이징에 대해 더 알아보기.
파트 (Parts)
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
control |
The control that houses the switch’s thumb. |
thumb |
The switch’s thumb. |
label |
The switch’s label. |
form-control-help-text |
The help text’s wrapper. |
CSS 파트 커스터마이징에 대해 더 알아보기.