2022-02-26 15:48:56 +00:00
|
|
|
import { html, LitElement } from 'lit';
|
2021-05-27 21:00:43 +00:00
|
|
|
import { customElement, property, query, state } from 'lit/decorators.js';
|
2021-09-29 12:40:26 +00:00
|
|
|
import { classMap } from 'lit/directives/class-map.js';
|
|
|
|
|
import { styleMap } from 'lit/directives/style-map.js';
|
|
|
|
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
2022-03-24 12:01:09 +00:00
|
|
|
import '../../components/icon/icon';
|
|
|
|
|
import { emit } from '../../internal/event';
|
|
|
|
|
import { clamp } from '../../internal/math';
|
|
|
|
|
import { watch } from '../../internal/watch';
|
2022-06-07 17:09:14 +00:00
|
|
|
import { LocalizeController } from '../../utilities/localize';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './rating.styles';
|
2022-07-19 12:27:39 +00:00
|
|
|
import type { CSSResultGroup } from 'lit';
|
2021-07-12 14:36:06 +00:00
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @since 2.0
|
|
|
|
|
* @status stable
|
|
|
|
|
*
|
|
|
|
|
* @dependency sl-icon
|
|
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-change - Emitted when the rating's value changes.
|
2021-05-17 22:04:28 +00:00
|
|
|
*
|
2022-03-09 20:54:18 +00:00
|
|
|
* @csspart base - The component's internal wrapper.
|
2021-06-24 22:24:54 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @cssproperty --symbol-color - The inactive color for symbols.
|
|
|
|
|
* @cssproperty --symbol-color-active - The active color for symbols.
|
|
|
|
|
* @cssproperty --symbol-size - The size of symbols.
|
|
|
|
|
* @cssproperty --symbol-spacing - The spacing to use around symbols.
|
2021-02-26 14:09:13 +00:00
|
|
|
*/
|
2021-03-18 13:04:23 +00:00
|
|
|
@customElement('sl-rating')
|
2021-03-09 00:14:32 +00:00
|
|
|
export default class SlRating extends LitElement {
|
2022-07-19 12:27:39 +00:00
|
|
|
static styles: CSSResultGroup = styles;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
|
|
|
|
@query('.rating') rating: HTMLElement;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
2022-06-07 17:09:14 +00:00
|
|
|
private readonly localize = new LocalizeController(this);
|
|
|
|
|
|
2021-03-24 14:21:21 +00:00
|
|
|
@state() private hoverValue = 0;
|
|
|
|
|
@state() private isHovering = false;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
|
/** The current rating. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Number }) value = 0;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
|
/** The highest rating to show. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Number }) max = 5;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
|
/** The minimum increment value allowed by the control. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Number }) precision = 1;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
|
/** Makes the rating readonly. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) readonly = false;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
|
/** Disables the rating. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
|
/** The name of the icon to display as the symbol. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() getSymbol: (value: number) => string = () => '<sl-icon name="star-fill" library="system"></sl-icon>';
|
2021-03-06 17:01:39 +00:00
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
/** Sets focus on the rating. */
|
2021-03-29 11:16:40 +00:00
|
|
|
focus(options?: FocusOptions) {
|
2021-02-26 14:09:13 +00:00
|
|
|
this.rating.focus(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Removes focus from the rating. */
|
2021-03-29 11:16:40 +00:00
|
|
|
blur() {
|
2021-02-26 14:09:13 +00:00
|
|
|
this.rating.blur();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getValueFromMousePosition(event: MouseEvent) {
|
2021-03-09 14:17:27 +00:00
|
|
|
return this.getValueFromXCoordinate(event.clientX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getValueFromTouchPosition(event: TouchEvent) {
|
|
|
|
|
return this.getValueFromXCoordinate(event.touches[0].clientX);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 14:22:41 +00:00
|
|
|
getValueFromXCoordinate(coordinate: number) {
|
2022-06-07 17:09:14 +00:00
|
|
|
const isRtl = this.localize.dir() === 'rtl';
|
|
|
|
|
const { left, right, width } = this.rating.getBoundingClientRect();
|
|
|
|
|
const value = isRtl
|
|
|
|
|
? this.roundToPrecision(((right - coordinate) / width) * this.max, this.precision)
|
|
|
|
|
: this.roundToPrecision(((coordinate - left) / width) * this.max, this.precision);
|
|
|
|
|
|
|
|
|
|
return clamp(value, 0, this.max);
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleClick(event: MouseEvent) {
|
2021-03-09 14:17:27 +00:00
|
|
|
this.setValue(this.getValueFromMousePosition(event));
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 14:22:41 +00:00
|
|
|
setValue(newValue: number) {
|
2021-02-26 14:09:13 +00:00
|
|
|
if (this.disabled || this.readonly) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.value = newValue === this.value ? 0 : newValue;
|
|
|
|
|
this.isHovering = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleKeyDown(event: KeyboardEvent) {
|
2022-06-07 17:09:14 +00:00
|
|
|
const isLtr = this.localize.dir() === 'ltr';
|
|
|
|
|
const isRtl = this.localize.dir() === 'rtl';
|
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
if (this.disabled || this.readonly) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 17:09:14 +00:00
|
|
|
if ((isLtr && event.key === 'ArrowLeft') || (isRtl && event.key === 'ArrowRight')) {
|
2021-02-26 14:09:13 +00:00
|
|
|
const decrement = event.shiftKey ? 1 : this.precision;
|
|
|
|
|
this.value = Math.max(0, this.value - decrement);
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 17:09:14 +00:00
|
|
|
if ((isLtr && event.key === 'ArrowRight') || (isRtl && event.key === 'ArrowLeft')) {
|
2021-02-26 14:09:13 +00:00
|
|
|
const increment = event.shiftKey ? 1 : this.precision;
|
|
|
|
|
this.value = Math.min(this.max, this.value + increment);
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.key === 'Home') {
|
|
|
|
|
this.value = 0;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.key === 'End') {
|
|
|
|
|
this.value = this.max;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleMouseEnter() {
|
|
|
|
|
this.isHovering = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 14:22:41 +00:00
|
|
|
handleMouseMove(event: MouseEvent) {
|
|
|
|
|
this.hoverValue = this.getValueFromMousePosition(event);
|
2021-03-09 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
handleMouseLeave() {
|
|
|
|
|
this.isHovering = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 14:22:41 +00:00
|
|
|
handleTouchStart(event: TouchEvent) {
|
|
|
|
|
this.hoverValue = this.getValueFromTouchPosition(event);
|
2021-03-09 14:17:27 +00:00
|
|
|
|
2021-03-09 14:22:41 +00:00
|
|
|
// Prevent scrolling when touch is initiated
|
|
|
|
|
event.preventDefault();
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-09 14:17:27 +00:00
|
|
|
handleTouchMove(event: TouchEvent) {
|
|
|
|
|
this.isHovering = true;
|
|
|
|
|
this.hoverValue = this.getValueFromTouchPosition(event);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 14:22:41 +00:00
|
|
|
handleTouchEnd(event: TouchEvent) {
|
|
|
|
|
this.isHovering = false;
|
|
|
|
|
this.setValue(this.hoverValue);
|
|
|
|
|
|
|
|
|
|
// Prevent click on mobile devices
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 13:26:35 +00:00
|
|
|
@watch('value', { waitUntilFirstUpdate: true })
|
2021-03-06 19:39:48 +00:00
|
|
|
handleValueChange() {
|
2021-06-24 22:24:54 +00:00
|
|
|
emit(this, 'sl-change');
|
2021-03-06 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
roundToPrecision(numberToRound: number, precision = 0.5) {
|
|
|
|
|
const multiplier = 1 / precision;
|
|
|
|
|
return Math.ceil(numberToRound * multiplier) / multiplier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2022-06-07 17:09:14 +00:00
|
|
|
const isRtl = this.localize.dir() === 'rtl';
|
2021-02-26 14:09:13 +00:00
|
|
|
const counter = Array.from(Array(this.max).keys());
|
|
|
|
|
let displayValue = 0;
|
|
|
|
|
|
|
|
|
|
if (this.disabled || this.readonly) {
|
|
|
|
|
displayValue = this.value;
|
|
|
|
|
} else {
|
|
|
|
|
displayValue = this.isHovering ? this.hoverValue : this.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return html`
|
|
|
|
|
<div
|
|
|
|
|
part="base"
|
|
|
|
|
class=${classMap({
|
|
|
|
|
rating: true,
|
|
|
|
|
'rating--readonly': this.readonly,
|
2022-06-07 17:09:14 +00:00
|
|
|
'rating--disabled': this.disabled,
|
|
|
|
|
'rating--rtl': isRtl
|
2021-02-26 14:09:13 +00:00
|
|
|
})}
|
|
|
|
|
aria-disabled=${this.disabled ? 'true' : 'false'}
|
|
|
|
|
aria-readonly=${this.readonly ? 'true' : 'false'}
|
2022-01-16 05:47:14 +00:00
|
|
|
aria-valuenow=${this.value}
|
2021-02-26 14:09:13 +00:00
|
|
|
aria-valuemin=${0}
|
|
|
|
|
aria-valuemax=${this.max}
|
|
|
|
|
tabindex=${this.disabled ? '-1' : '0'}
|
2021-03-06 17:01:39 +00:00
|
|
|
@click=${this.handleClick}
|
|
|
|
|
@keydown=${this.handleKeyDown}
|
|
|
|
|
@mouseenter=${this.handleMouseEnter}
|
2021-03-09 14:17:27 +00:00
|
|
|
@touchstart=${this.handleTouchStart}
|
2021-03-06 17:01:39 +00:00
|
|
|
@mouseleave=${this.handleMouseLeave}
|
2021-03-09 14:17:27 +00:00
|
|
|
@touchend=${this.handleTouchEnd}
|
2021-03-06 17:01:39 +00:00
|
|
|
@mousemove=${this.handleMouseMove}
|
2021-03-09 14:17:27 +00:00
|
|
|
@touchmove=${this.handleTouchMove}
|
2021-02-26 14:09:13 +00:00
|
|
|
>
|
|
|
|
|
<span class="rating__symbols rating__symbols--inactive">
|
|
|
|
|
${counter.map(index => {
|
|
|
|
|
// Users can click the current value to clear the rating. When this happens, we set this.isHovering to
|
|
|
|
|
// false to prevent the hover state from confusing them as they move the mouse out of the control. This
|
|
|
|
|
// extra mouseenter will reinstate it if they happen to mouse over an adjacent symbol.
|
|
|
|
|
return html`
|
|
|
|
|
<span
|
|
|
|
|
class=${classMap({
|
|
|
|
|
rating__symbol: true,
|
|
|
|
|
'rating__symbol--hover': this.isHovering && Math.ceil(displayValue) === index + 1
|
|
|
|
|
})}
|
|
|
|
|
role="presentation"
|
2021-07-07 12:13:07 +00:00
|
|
|
@mouseenter=${this.handleMouseEnter}
|
2021-02-26 14:09:13 +00:00
|
|
|
>
|
2021-03-06 17:01:39 +00:00
|
|
|
${unsafeHTML(this.getSymbol(index + 1))}
|
2021-02-26 14:09:13 +00:00
|
|
|
</span>
|
|
|
|
|
`;
|
|
|
|
|
})}
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
<span class="rating__symbols rating__symbols--indicator">
|
|
|
|
|
${counter.map(index => {
|
|
|
|
|
return html`
|
|
|
|
|
<span
|
|
|
|
|
class=${classMap({
|
|
|
|
|
rating__symbol: true,
|
|
|
|
|
'rating__symbol--hover': this.isHovering && Math.ceil(displayValue) === index + 1
|
|
|
|
|
})}
|
|
|
|
|
style=${styleMap({
|
|
|
|
|
clipPath:
|
2022-06-07 17:09:14 +00:00
|
|
|
displayValue > index + 1
|
|
|
|
|
? 'none'
|
|
|
|
|
: isRtl
|
|
|
|
|
? `inset(0 0 0 ${100 - ((displayValue - index) / 1) * 100}%)`
|
|
|
|
|
: `inset(0 ${100 - ((displayValue - index) / 1) * 100}% 0 0)`
|
2021-02-26 14:09:13 +00:00
|
|
|
})}
|
|
|
|
|
role="presentation"
|
|
|
|
|
>
|
2021-03-06 17:01:39 +00:00
|
|
|
${unsafeHTML(this.getSymbol(index + 1))}
|
2021-02-26 14:09:13 +00:00
|
|
|
</span>
|
|
|
|
|
`;
|
|
|
|
|
})}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
|
'sl-rating': SlRating;
|
|
|
|
|
}
|
|
|
|
|
}
|