shoelace/docs/components/rating.md

157 wiersze
3.7 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-20 10:17:20 +00:00
# Rating
[component-header:sl-rating]
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating"></sl-rating>
2020-07-20 10:17:20 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" />;
2021-11-04 22:12:47 +00:00
```
2020-07-20 16:58:05 +00:00
## Examples
2022-08-11 13:17:34 +00:00
### Labels
2022-08-11 13:22:35 +00:00
Ratings are commonly identified contextually, so labels aren't displayed. However, you should always provide one for assistive devices using the `label` attribute.
2022-08-11 13:17:34 +00:00
```html preview
<sl-rating label="Rate this component"></sl-rating>
```
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
const App = () => <SlRating label="Rate this component" />;
```
2020-07-20 16:58:05 +00:00
### Maximum Value
Ratings are 0-5 by default. To change the maximum possible value, use the `max` attribute.
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" max="3"></sl-rating>
2020-07-20 16:58:05 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" max={3} />;
2021-11-04 22:12:47 +00:00
```
2020-07-20 16:58:05 +00:00
### Precision
Use the `precision` attribute to let users select fractional ratings.
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" precision="0.5" value="2.5"></sl-rating>
2021-11-04 22:12:47 +00:00
```
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" precision={0.5} value={2.5} />;
2020-07-20 16:58:05 +00:00
```
## Symbol Sizes
Set the `--symbol-size` custom property to adjust the size.
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" style="--symbol-size: 2rem;"></sl-rating>
2020-07-20 16:58:05 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" style={{ '--symbol-size': '2rem' }} />;
2021-11-04 22:12:47 +00:00
```
2020-07-20 16:58:05 +00:00
### Readonly
Use the `readonly` attribute to display a rating that users can't change.
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" readonly value="3"></sl-rating>
2020-07-20 16:58:05 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" readonly value={3} />;
2021-11-04 22:12:47 +00:00
```
2020-07-20 16:58:05 +00:00
### Disabled
Use the `disable` attribute to disable the rating.
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" disabled value="3"></sl-rating>
2020-07-20 16:58:05 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRating } from '@shoelace-style/shoelace/dist/react';
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" disabled value={3} />;
2021-11-04 22:12:47 +00:00
```
2020-07-20 16:58:05 +00:00
### Custom Icons
2022-02-26 15:46:25 +00:00
You can provide custom icons by passing a function to the `getSymbol` property.
2020-07-20 16:58:05 +00:00
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" class="rating-hearts" style="--symbol-color-active: #ff4136;"></sl-rating>
2021-03-06 17:01:39 +00:00
<script>
const rating = document.querySelector('.rating-hearts');
2022-03-02 15:10:41 +00:00
rating.getSymbol = () => '<sl-icon name="heart-fill"></sl-icon>';
2021-03-06 17:01:39 +00:00
</script>
2020-07-20 16:58:05 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import '@shoelace-style/shoelace/dist/components/icon/icon';
import { SlRating } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2022-08-11 13:17:34 +00:00
<SlRating
label="Rating"
getSymbol={() => '<sl-icon name="heart-fill"></sl-icon>'}
style={{ '--symbol-color-active': '#ff4136' }}
/>
2021-11-04 22:12:47 +00:00
);
```
2020-07-20 16:58:05 +00:00
### Value-based Icons
2022-02-26 15:46:25 +00:00
You can also use the `getSymbol` property to render different icons based on value.
2020-07-20 16:58:05 +00:00
```html preview
2022-08-11 13:17:34 +00:00
<sl-rating label="Rating" class="rating-emojis"></sl-rating>
2020-07-20 16:58:05 +00:00
<script>
const rating = document.querySelector('.rating-emojis');
2022-03-02 15:10:41 +00:00
rating.getSymbol = value => {
2020-07-20 16:58:05 +00:00
const icons = ['emoji-angry', 'emoji-frown', 'emoji-expressionless', 'emoji-smile', 'emoji-laughing'];
2021-08-18 21:29:59 +00:00
return `<sl-icon name="${icons[value - 1]}"></sl-icon>`;
2020-07-20 16:58:05 +00:00
};
</script>
```
2021-11-04 22:12:47 +00:00
```jsx react
import '@shoelace-style/shoelace/dist/components/icon/icon';
import { SlRating } from '@shoelace-style/shoelace/dist/react';
function getSymbol(value) {
const icons = ['emoji-angry', 'emoji-frown', 'emoji-expressionless', 'emoji-smile', 'emoji-laughing'];
return `<sl-icon name="${icons[value - 1]}"></sl-icon>`;
}
2022-08-11 13:17:34 +00:00
const App = () => <SlRating label="Rating" getSymbol={getSymbol} />;
2021-11-04 22:12:47 +00:00
```
2020-07-20 10:17:20 +00:00
[component-metadata:sl-rating]