This commit is contained in:
Cory LaViska
2023-01-10 17:10:18 -05:00
parent 0df27cf730
commit 0c2f43b837
3 changed files with 122 additions and 5 deletions

View File

@@ -56,7 +56,7 @@ import { SlRating } from '@shoelace-style/shoelace/dist/react';
const App = () => <SlRating label="Rating" precision={0.5} value={2.5} />;
```
## Symbol Sizes
### Symbol Sizes
Set the `--symbol-size` custom property to adjust the size.
@@ -98,6 +98,99 @@ import { SlRating } from '@shoelace-style/shoelace/dist/react';
const App = () => <SlRating label="Rating" disabled value={3} />;
```
### Detecting Hover
Use the `sl-hover` event to detect when the user hovers over (or touch and drag) the rating. This lets you hook into values as the user interacts with the rating, but before they select a value.
The event has a payload with `phase` and `value` properties. The `phase` property tells when hovering starts, moves to a new value, and ends. The `value` property tells what the rating's value would be if the user were to commit to the hovered value.
```html preview
<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>
```
```jsx react
import { useState } from 'react';
import { SlRating } from '@shoelace-style/shoelace/dist/react';
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
You can provide custom icons by passing a function to the `getSymbol` property.
@@ -112,7 +205,6 @@ You can provide custom icons by passing a function to the `getSymbol` property.
```
```jsx react
import '@shoelace-style/shoelace/dist/components/icon/icon';
import { SlRating } from '@shoelace-style/shoelace/dist/react';
const App = () => (
@@ -142,7 +234,6 @@ You can also use the `getSymbol` property to render different icons based on val
```
```jsx react
import '@shoelace-style/shoelace/dist/components/icon/icon';
import { SlRating } from '@shoelace-style/shoelace/dist/react';
function getSymbol(value) {

View File

@@ -12,6 +12,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
- Added support for the `inert` attribute on `<sl-menu-item>` to allow hidden menu items to not accept focus [#1107](https://github.com/shoelace-style/shoelace/issues/1107)
- Added the `tag` part to `<sl-select>`
- Added `sl-hover` event to `<sl-rating>` [#1125](https://github.com/shoelace-style/shoelace/issues/1125)
- Fixed a bug in `<sl-select>` that prevented placeholders from showing when `multiple` was used [#1109](https://github.com/shoelace-style/shoelace/issues/1109)
- Fixed a bug in `<sl-select>` that caused tags to not be rounded when using the `pill` attribute [#1117](https://github.com/shoelace-style/shoelace/issues/1117)
- Fixed a bug in `<sl-select>` where the `sl-change` and `sl-input` events didn't weren't emitted when removing tags [#1119](https://github.com/shoelace-style/shoelace/issues/1119)