Color Picker
Shoelace의 Color Picker 컴포넌트(sl-color-picker)는 사용자가 색상을 선택할 수 있게 해주는 색상 선택기예요. hex, rgb, hsl, hsv 등 다양한 형식을 지원하고, 불투명도(opacity) 슬라이더와 함께 사용할 수 있답니다.
출처: 문서
본문
Color picker는 사용자가 색상을 선택할 수 있게 해줘요.
<sl-color-picker label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => <SlColorPicker label="Select a color" />;
이 컴포넌트는 표준 <form> 요소와 함께 동작해요. 폼 제출과 클라이언트 측 검증에 대해 더 알아보려면 form controls 섹션을 참고하세요.
Examples
Initial Value
value 속성(attribute)을 사용해 색상 선택기의 초기 값을 설정할 수 있어요.
<sl-color-picker value="#4a90e2" label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => <SlColorPicker value="#4a90e2" label="Select a color" />;
Opacity
opacity 속성(attribute)을 사용하면 불투명도 슬라이더를 활성화할 수 있어요. 활성화하면 값이 format에 따라 HEXA, RGBA, HSLA, 또는 HSVA로 표시돼요.
<sl-color-picker value="#f5a623ff" opacity label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => <SlColorPicker opacity label="Select a color" />;
Formats
format 속성(attribute)으로 색상 선택기의 형식을 설정할 수 있어요. 유효한 옵션은 hex, rgb, hsl, hsv예요. 참고로 색상 선택기의 입력은 이 옵션과 관계없이 모든 파싱 가능한 형식(CSS 색상 이름 포함)을 받아들여요.
사용자가 스스로 형식을 토글하지 못하게 하려면 no-format-toggle 속성(attribute)을 추가하세요.
<sl-color-picker format="hex" value="#4a90e2" label="Select a color"></sl-color-picker>
<sl-color-picker format="rgb" value="rgb(80, 227, 194)" label="Select a color"></sl-color-picker>
<sl-color-picker format="hsl" value="hsl(290, 87%, 47%)" label="Select a color"></sl-color-picker>
<sl-color-picker format="hsv" value="hsv(55, 89%, 97%)" label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => (
<>
<SlColorPicker format="hex" value="#4a90e2" />
<SlColorPicker format="rgb" value="rgb(80, 227, 194)" />
<SlColorPicker format="hsl" value="hsl(290, 87%, 47%)" />
<SlColorPicker format="hsv" value="hsv(55, 89%, 97%)" />
</>
);
Swatches
swatches 속성(attribute)을 사용하면 색상 선택기에 편리한 프리셋을 추가할 수 있어요. 색상 선택기가 파싱할 수 있는 어떤 형식(CSS 색상 이름 포함)이든 가능하지만, 각 값은 세미콜론(;)으로 구분해야 해요. 또는 JavaScript로 색상 값 배열을 이 프로퍼티에 전달할 수도 있어요.
<sl-color-picker
label="Select a color"
swatches="
#d0021b; #f5a623; #f8e71c; #8b572a; #7ed321; #417505; #bd10e0; #9013fe;
#4a90e2; #50e3c2; #b8e986; #000; #444; #888; #ccc; #fff;
"
></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => (
<SlColorPicker
label="Select a color"
swatches="
#d0021b; #f5a623; #f8e71c; #8b572a; #7ed321; #417505; #bd10e0; #9013fe;
#4a90e2; #50e3c2; #b8e986; #000; #444; #888; #ccc; #fff;
"
/>
);
Sizes
size 속성(attribute)을 사용해 색상 선택기의 트리거(trigger) 크기를 바꿀 수 있어요.
<sl-color-picker size="small" label="Select a color"></sl-color-picker>
<sl-color-picker size="medium" label="Select a color"></sl-color-picker>
<sl-color-picker size="large" label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => (
<>
<SlColorPicker size="small" label="Select a color" />
<SlColorPicker size="medium" label="Select a color" />
<SlColorPicker size="large" label="Select a color" />
</>
);
Inline
inline 속성(attribute)을 사용하면 색상 선택기를 드롭다운 대신 인라인으로 렌더링할 수 있어요.
<sl-color-picker inline label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
const App = () => <SlColorPicker inline label="Select a color" />;
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/color-picker/color-picker.js"></script>
CDN에서 JavaScript import로 이 컴포넌트를 가져오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/color-picker/color-picker.js';
bundler를 사용해 이 컴포넌트를 가져오려면:
import '@shoelace-style/shoelace/dist/components/color-picker/color-picker.js';
이 컴포넌트를 React 컴포넌트로 가져오려면:
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
Slots
| Name | Description |
|---|---|
label |
The color picker’s form label. Alternatively, you can use the label attribute. |
슬롯(slots) 사용에 대해 더 알아보기.
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
value |
The current value of the color picker. The value’s format will vary based the format attribute. To get the value in a specific format, use the getFormattedValue() method. The value is submitted as a name/value pair with form data. |
string |
'' |
|
defaultValue |
The default value of the form control. Primarily used for resetting the form control. | string |
'' |
|
label |
The color picker’s label. This will not be displayed, but it will be announced by assistive devices. If you need to display HTML, you can use the label slot` instead. |
string |
'' |
|
format |
The format to use. If opacity is enabled, these will translate to HEXA, RGBA, HSLA, and HSVA respectively. The color picker will accept user input in any format (including CSS color names) and convert it to the desired format. | 'hex' | 'rgb' | 'hsl' | 'hsv' |
'hex' |
|
inline |
Renders the color picker inline rather than in a dropdown. | boolean |
false |
|
size |
Determines the size of the color picker’s trigger. This has no effect on inline color pickers. | 'small' | 'medium' | 'large' |
'medium' |
|
noFormatToggle / no-format-toggle |
Removes the button that lets users toggle between format. | boolean |
false |
|
name |
The name of the form control, submitted as a name/value pair with form data. | string |
'' |
|
disabled |
Disables the color picker. | boolean |
false |
|
hoist |
Enable this option to prevent the panel from being clipped when the component is placed inside a container with overflow: auto|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios. |
boolean |
false |
|
opacity |
Shows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA. | boolean |
false |
|
uppercase |
By default, values are lowercase. With this attribute, values will be uppercase instead. | boolean |
false |
|
swatches |
One or more predefined color swatches to display as presets in the color picker. Can include any format the color picker can parse, including HEX(A), RGB(A), HSL(A), HSV(A), and CSS color names. Each color must be separated by a semicolon (;). Alternatively, you can pass an array of color values to this property using JavaScript. |
string | string[] |
'' |
|
form |
By default, form controls are associated with the nearest containing <form> 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 color picker a required field. | boolean |
false |
|
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 color picker loses focus. | - |
sl-change |
onSlChange |
Emitted when the color picker’s value changes. | - |
sl-focus |
onSlFocus |
Emitted when the color picker receives focus. | - |
sl-input |
onSlInput |
Emitted when the color picker receives input. | - |
sl-invalid |
onSlInvalid |
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | - |
이벤트(events)에 대해 더 알아보기.
Methods
| Name | Description | Arguments |
|---|---|---|
focus() |
Sets focus on the color picker. | options: FocusOptions |
blur() |
Removes focus from the color picker. | - |
getFormattedValue() |
Returns the current value as a string in the specified format. | format: 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hsv' | 'hsva' |
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)에 대해 더 알아보기.
Custom Properties
| Name | Description | Default |
|---|---|---|
--grid-width |
The width of the color grid. | |
--grid-height |
The height of the color grid. | |
--grid-handle-size |
The size of the color grid’s handle. | |
--slider-height |
The height of the hue and alpha sliders. | |
--slider-handle-size |
The diameter of the slider’s handle. | |
--swatch-size |
The size of each predefined color swatch. |
CSS 커스텀 프로퍼티(custom properties) 커스터마이징에 대해 더 알아보기.
Parts
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
trigger |
The color picker’s dropdown trigger. |
swatches |
The container that holds the swatches. |
swatch |
Each individual swatch. |
grid |
The color grid. |
grid-handle |
The color grid’s handle. |
slider |
Hue and opacity sliders. |
slider-handle |
Hue and opacity slider handles. |
hue-slider |
The hue slider. |
hue-slider-handle |
The hue slider’s handle. |
opacity-slider |
The opacity slider. |
opacity-slider-handle |
The opacity slider’s handle. |
preview |
The preview color. |
input |
The text input. |
eye-dropper-button |
The eye dropper button. |
eye-dropper-button__base |
The eye dropper button’s exported button part. |
eye-dropper-button__prefix |
The eye dropper button’s exported prefix part. |
eye-dropper-button__label |
The eye dropper button’s exported label part. |
eye-dropper-button__suffix |
The eye dropper button’s exported suffix part. |
eye-dropper-button__caret |
The eye dropper button’s exported caret part. |
format-button |
The format button. |
format-button__base |
The format button’s exported button part. |
format-button__prefix |
The format button’s exported prefix part. |
format-button__label |
The format button’s exported label part. |
format-button__suffix |
The format button’s exported suffix part. |
format-button__caret |
The format button’s exported caret part. |
CSS 파트(parts) 커스터마이징에 대해 더 알아보기.
Dependencies
이 컴포넌트는 다음 의존성들을 자동으로 가져와요.
sl-buttonsl-dropdownsl-iconsl-inputsl-tooltipsl-visually-hiddensl-icon-buttonsl-spinner
더 알아보기 (Learn more)
- Shoelace 시작하기(getting started) — 컴포넌트 설치와 로딩 방법
- Shoelace Form Controls — 폼 제출과 클라이언트 측 검증 방법
- Shoelace Input 컴포넌트 — 색상 값을 텍스트로 입력할 수 있는 입력 컴포넌트