평가 (Rating)
SlRating은 사용자가 빠르게 점수를 보고 평가를 남길 수 있게 해주는 요소예요. 별점 평가 같은 곳에 유용합니다.
출처: 문서
본문
<sl-rating label="Rating"></sl-rating>
React에서는 이렇게 사용할 수 있어요.
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rating" />;
예시 (Examples)
라벨 (Labels)
Rating은 보통 문맥상 어떤 평점인지 알 수 있기 때문에 라벨을 화면에 표시하지 않아요. 하지만 접근성 도구(assistive devices)를 위해 label 속성으로 항상 하나는 제공해 주는 게 좋습니다.
<sl-rating label="Rate this component"></sl-rating>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rate this component" />;
최댓값 (Maximum Value)
Rating은 기본적으로 0–5 범위예요. 최대 가능 값을 바꾸려면 max 속성을 사용하면 됩니다.
<sl-rating label="Rating" max="3"></sl-rating>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rating" max={3} />;
정밀도 (Precision)
precision 속성을 사용하면 사용자가 소수점 단위의 평가를 할 수 있게 해줘요.
<sl-rating label="Rating" precision="0.5" value="2.5"></sl-rating>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rating" precision={0.5} value={2.5} />;
심볼 크기 (Symbol Sizes)
--symbol-size 커스텀 프로퍼티를 설정하면 크기를 조절할 수 있어요.
<sl-rating label="Rating" style="--symbol-size: 2rem;"></sl-rating>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rating" style={{ '--symbol-size': '2rem' }} />;
읽기 전용 (Readonly)
readonly 속성을 사용하면 사용자가 바꿀 수 없는 평가를 표시할 수 있어요.
<sl-rating label="Rating" readonly value="3"></sl-rating>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rating" readonly value={3} />;
비활성화 (Disabled)
disabled 속성을 사용하면 평가를 비활성화할 수 있어요.
<sl-rating label="Rating" disabled value="3"></sl-rating>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => <SlRating label="Rating" disabled value={3} />;
호버 감지 (Detecting Hover)
sl-hover 이벤트를 사용하면 사용자가 rating 위에 호버(또는 터치 후 드래그)할 때를 감지할 수 있어요. 이렇게 하면 사용자가 값을 선택하기 전에 상호작용하는 동안의 값을 잡아낼 수 있습니다. 이 이벤트는 phase와 value 프로퍼티를 담은 페이로드를 갖고 있어요. phase 프로퍼티는 호버가 시작되고, 새 값으로 이동하며, 끝나는 시점을 알려줍니다. value 프로퍼티는 사용자가 호버한 값에 확정한다면 rating의 값이 무엇이 될지를 알려줘요.
<div class="detect-hover">
<sl-rating label="Rating"></sl-rating>
<span></span>
</div>
<script>
const rating = document.querySelector('.detect-hover > sl-rating');
const span = rating.nextElementSibling;
const terms = ['No rating', 'Terrible', 'Bad', 'OK', 'Good', 'Excellent'];
rating.addEventListener('sl-hover', event => {
span.textContent = terms[event.detail.value];
// Clear feedback when hovering stops
if (event.detail.phase === 'end') {
span.textContent = '';
}
});
</script>
<style>
.detect-hover span {
position: relative;
top: -4px;
left: 8px;
border-radius: var(--sl-border-radius-small);
background: var(--sl-color-neutral-900);
color: var(--sl-color-neutral-0);
text-align: center;
padding: 4px 6px;
}
.detect-hover span:empty {
display: none;
}
</style>
import { useState } from 'react';
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const terms = ['No rating', 'Terrible', 'Bad', 'OK', 'Good', 'Excellent'];
const css = `
.detect-hover span {
position: relative;
top: -4px;
left: 8px;
border-radius: var(--sl-border-radius-small);
background: var(--sl-color-neutral-900);
color: var(--sl-color-neutral-0);
text-align: center;
padding: 4px 6px;
}
.detect-hover span:empty {
display: none;
}
`;
function handleHover(event) {
rating.addEventListener('sl-hover', event => {
setFeedback(terms[event.detail.value]);
// Clear feedback when hovering stops
if (event.detail.phase === 'end') {
setFeedback('');
}
});
}
const App = () => {
const [feedback, setFeedback] = useState(true);
return (
<>
<div class="detect-hover">
<SlRating label="Rating" onSlHover={handleHover} />
<span>{feedback}</span>
</div>
<style>{css}</style>
</>
);
};
커스텀 아이콘 (Custom Icons)
getSymbol 프로퍼티에 함수를 넘겨주면 커스텀 아이콘을 제공할 수 있어요.
<sl-rating label="Rating" class="rating-hearts" style="--symbol-color-active: #ff4136;"></sl-rating>
<script>
const rating = document.querySelector('.rating-hearts');
rating.getSymbol = () => '<sl-icon name="heart-fill"></sl-icon>';
</script>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
const App = () => (
<SlRating
label="Rating"
getSymbol={() => '<sl-icon name="heart-fill"></sl-icon>'}
style={{ '--symbol-color-active': '#ff4136' }}
/>
);
값 기반 아이콘 (Value-based Icons)
getSymbol 프로퍼티를 사용해 값에 따라 다른 아이콘을 렌더링할 수도 있어요.
<sl-rating label="Rating" class="rating-emojis"></sl-rating>
<script>
const rating = document.querySelector('.rating-emojis');
rating.getSymbol = value => {
const icons = ['emoji-angry', 'emoji-frown', 'emoji-expressionless', 'emoji-smile', 'emoji-laughing'];
return `<sl-icon name="${icons[value - 1]}"></sl-icon>`;
};
</script>
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
function getSymbol(value) {
const icons = ['emoji-angry', 'emoji-frown', 'emoji-expressionless', 'emoji-smile', 'emoji-laughing'];
return `<sl-icon name="${icons[value - 1]}"></sl-icon>`;
}
const App = () => <SlRating label="Rating" getSymbol={getSymbol} />;
불러오기 (Importing)
autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.
Script 태그로 CDN에서 이 컴포넌트를 불러오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/rating/rating.js"></script>
JavaScript import 구문으로 CDN에서 불러오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/rating/rating.js';
번들러(bundler)를 사용해 불러오려면:
import '@shoelace-style/shoelace/dist/components/rating/rating.js';
React 컴포넌트로 불러오려면:
import SlRating from '@shoelace-style/shoelace/dist/react/rating';
속성 (Properties)
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
label |
A label that describes the rating to assistive devices. | string |
'' |
|
value |
The current rating. | number |
0 |
|
max |
The highest rating to show. | number |
5 |
|
precision |
The precision at which the rating will increase and decrease. For example, to allow half-star ratings, set this attribute to 0.5. |
number |
1 |
|
readonly |
Makes the rating readonly. | boolean |
false |
|
disabled |
Disables the rating. | boolean |
false |
|
getSymbol |
A function that customizes the symbol to be rendered. The first and only argument is the rating’s current value. The function should return a string containing trusted HTML of the symbol to render at the specified value. Works well with `` elements. | (value: number) => string |
- | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
속성(attribute)과 프로퍼티(property)에 대해 더 알아보기.
이벤트 (Events)
| Name | React Event | Description | Event Detail |
|---|---|---|---|
sl-change |
onSlChange |
Emitted when the rating’s value changes. | - |
sl-hover |
onSlHover |
Emitted when the user hovers over a value. The phase property indicates when hovering starts, moves to a new value, or ends. The value property tells what the rating’s value would be if the user were to commit to the hovered value. |
{ phase: 'start' | 'move' | 'end', value: number } |
이벤트에 대해 더 알아보기.
메서드 (Methods)
| Name | Description | Arguments |
|---|---|---|
focus() |
Sets focus on the rating. | options: FocusOptions |
blur() |
Removes focus from the rating. | - |
메서드에 대해 더 알아보기.
커스텀 프로퍼티 (Custom Properties)
| Name | Description | Default |
|---|---|---|
--symbol-color |
The inactive color for symbols. | |
--symbol-color-active |
The active color for symbols. | |
--symbol-size |
The size of symbols. | |
--symbol-spacing |
The spacing to use around symbols. |
CSS 커스텀 프로퍼티 커스터마이징에 대해 더 알아보기.
파트 (Parts)
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
CSS 파트 커스터마이징에 대해 더 알아보기.
의존성 (Dependencies)
이 컴포넌트는 다음 의존성을 자동으로 불러옵니다.
- ``