범위 (Range)
SlRange은 슬라이더를 이용해 주어진 범위 안에서 하나의 값을 선택할 수 있게 해주는 요소예요. 볼륨 조절 같은 곳에 유용합니다.
출처: 문서
본문
<sl-range></sl-range>
React에서는 이렇게 사용할 수 있어요.
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange />;
이 컴포넌트는 표준 `` 요소와 함께 동작해요. 폼 제출(form submission)과 클라이언트 측 유효성 검사(client-side validation)에 대해 더 자세히 알고 싶다면 폼 컨트롤 섹션을 참고하세요.
예시 (Examples)
라벨 (Labels)
label 속성을 사용하면 범위에 접근 가능한(accessible) 라벨을 붙일 수 있어요. HTML이 포함된 라벨이 필요하다면 label 슬롯을 대신 사용하세요.
<sl-range label="Volume" min="0" max="100"></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange label="Volume" min={0} max={100} />;
도움말 텍스트 (Help Text)
help-text 속성을 사용하면 범위에 설명용 도움말 텍스트를 추가할 수 있어요. HTML이 포함된 도움말이 필요하다면 help-text 슬롯을 대신 사용하세요.
<sl-range label="Volume" help-text="Controls the volume of the current song." min="0" max="100"></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange label="Volume" help-text="Controls the volume of the current song." min={0} max={100} />;
최소, 최대, 간격 (Min, Max, and Step)
min과 max 속성으로 범위의 최솟값과 최댓값을 각각 설정할 수 있어요. step 속성은 값을 증가·감소시킬 때의 간격을 결정합니다.
<sl-range min="0" max="10" step="1"></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange min={0} max={10} step={1} />;
비활성화 (Disabled)
disabled 속성을 사용하면 슬라이더를 비활성화할 수 있어요.
<sl-range disabled></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange disabled />;
툴팁 위치 (Tooltip Placement)
기본적으로 툴팁은 위쪽에 표시돼요. tooltip을 bottom으로 설정하면 슬라이더 아래쪽에 표시됩니다.
<sl-range tooltip="bottom"></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange tooltip="bottom" />;
툴팁 비활성화 (Disable the Tooltip)
툴팁을 끄려면 tooltip을 none으로 설정하면 돼요.
<sl-range tooltip="none"></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange tooltip="none" />;
커스텀 트랙 색상 (Custom Track Colors)
--track-color-active와 --track-color-inactive 커스텀 프로퍼티를 사용하면 트랙의 활성/비활성 부분 색상을 커스터마이징할 수 있어요.
<sl-range
style="
--track-color-active: var(--sl-color-primary-600);
--track-color-inactive: var(--sl-color-primary-100);
"
></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => (
<SlRange
style={{
'--track-color-active': 'var(--sl-color-primary-600)',
'--track-color-inactive': 'var(--sl-color-primary-200)'
}}
/>
);
커스텀 트랙 오프셋 (Custom Track Offset)
--track-active-offset 커스텀 프로퍼티로 활성 트랙의 시작 오프셋을 커스터마이징할 수 있어요.
<sl-range
min="-100"
max="100"
style="
--track-color-active: var(--sl-color-primary-600);
--track-color-inactive: var(--sl-color-primary-100);
--track-active-offset: 50%;
"
></sl-range>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => (
<SlRange
min={-100}
max={100}
style={{
'--track-color-active': 'var(--sl-color-primary-600)',
'--track-color-inactive': 'var(--sl-color-primary-200)',
'--track-active-offset': '50%'
}}
/>
);
커스텀 툴팁 포맷터 (Custom Tooltip Formatter)
tooltipFormatter 프로퍼티에 범위의 값을 인자로 받는 함수를 설정하면 툴팁의 내용을 바꿀 수 있어요.
<sl-range min="0" max="100" step="1" class="range-with-custom-formatter"></sl-range>
<script>
const range = document.querySelector('.range-with-custom-formatter');
range.tooltipFormatter = value => `Total - ${value}%`;
</script>
import SlRange from '@shoelace-style/shoelace/dist/react/range';
const App = () => <SlRange min={0} max={100} step={1} tooltipFormatter={value => `Total - ${value}%`} />;
불러오기 (Importing)
autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.
Script 태그로 CDN에서 이 컴포넌트를 불러오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/range/range.js"></script>
JavaScript import 구문으로 CDN에서 불러오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/range/range.js';
번들러(bundler)를 사용해 불러오려면:
import '@shoelace-style/shoelace/dist/components/range/range.js';
React 컴포넌트로 불러오려면:
import SlRange from '@shoelace-style/shoelace/dist/react/range';
슬롯 (Slots)
| Name | Description |
|---|---|
label |
The range’s label. Alternatively, you can use the label attribute. |
help-text |
Text that describes how to use the input. Alternatively, you can use the help-text attribute. |
슬롯 사용법에 대해 더 알아보기.
속성 (Properties)
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
name |
The name of the range, submitted as a name/value pair with form data. | string |
'' |
|
value |
The current value of the range, submitted as a name/value pair with form data. | number |
0 |
|
label |
The range’s label. If you need to display HTML, use the label slot instead. |
string |
'' |
|
helpText help-text |
The range’s help text. If you need to display HTML, use the help-text slot instead. | string |
'' |
|
disabled |
Disables the range. | boolean |
false |
|
min |
The minimum acceptable value of the range. | number |
0 |
|
max |
The maximum acceptable value of the range. | number |
100 |
|
step |
The interval at which the range will increase and decrease. | number |
1 |
|
tooltip |
The preferred placement of the range’s tooltip. | 'top' | 'bottom' | 'none' |
'top' |
|
tooltipFormatter |
A function used to format the tooltip’s value. The range’s value is passed as the first and only argument. The function should return a string to display in the tooltip. | (value: number) => string |
- | |
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 |
'' |
|
defaultValue |
The default value of the form control. Primarily used for resetting the form control. | number |
0 |
|
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 an alteration to the control’s value is committed by the user. | - |
sl-focus |
onSlFocus |
Emitted when the control gains focus. | - |
sl-input |
onSlInput |
Emitted when the control receives input. | - |
sl-invalid |
onSlInvalid |
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | - |
이벤트에 대해 더 알아보기.
메서드 (Methods)
| Name | Description | Arguments |
|---|---|---|
focus() |
Sets focus on the range. | options: FocusOptions |
blur() |
Removes focus from the range. | - |
stepUp() |
Increments the value of the range by the value of the step attribute. | - |
stepDown() |
Decrements the value of the range by the value of the step attribute. | - |
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 |
|---|---|---|
--thumb-size |
The size of the thumb. | |
--tooltip-offset |
The vertical distance the tooltip is offset from the track. | |
--track-color-active |
The color of the portion of the track that represents the current value. | |
--track-color-inactive |
The of the portion of the track that represents the remaining value. | |
--track-height |
The height of the track. | |
--track-active-offset |
The point of origin of the active track. |
CSS 커스텀 프로퍼티 커스터마이징에 대해 더 알아보기.
파트 (Parts)
| Name | Description |
|---|---|
form-control |
The form control that wraps the label, input, and help text. |
form-control-label |
The label’s wrapper. |
form-control-input |
The range’s wrapper. |
form-control-help-text |
The help text’s wrapper. |
base |
The component’s base wrapper. |
input |
The internal `` element. |
tooltip |
The range’s tooltip. |
CSS 파트 커스터마이징에 대해 더 알아보기.