Skip to content

Commit f1c3c53

Browse files
committed
Refactor OlStarRating component by removing the disabled property and related CSS. Update event handling to simplify interactions, ensuring readonly mode is the only state affecting user input. Adjust hover and click behavior accordingly.
1 parent c82523e commit f1c3c53

File tree

1 file changed

+9
-41
lines changed

1 file changed

+9
-41
lines changed

openlibrary/components/lit/OlStarRating.js

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { LitElement, html, css } from 'lit';
22

33
/**
4-
* STATUS: EXPERIMENTAL - Minimal implementation for single scenario production testing.
5-
*
6-
* A star rating web component built with Lit
7-
*
84
* @element ol-star-rating
95
*
106
* @prop {number} value - Current rating value (0-5, supports decimals) (default: 0)
117
* @prop {string} size - Star size: 'small', 'medium', 'large' (default: 'medium')
128
* @prop {boolean} readonly - Read-only mode, no interactions (default: false)
13-
* @prop {boolean} disabled - Disabled state (default: false)
149
* @prop {string} clearButtonLabel - Label for the clear button (default: 'Clear my rating')
1510
* @prop {string} ratingText - Text to display next to stars (e.g., "4.2 (1,234 ratings)") - shown automatically when set
1611
*
@@ -27,7 +22,6 @@ export class OlStarRating extends LitElement {
2722
value: { type: Number, reflect: true },
2823
size: { type: String, reflect: true },
2924
readonly: { type: Boolean, reflect: true },
30-
disabled: { type: Boolean, reflect: true },
3125
clearButtonLabel: { type: String, reflect: true, attribute: 'clear-button-label' },
3226
ratingText: { type: String, reflect: true, attribute: 'rating-text' },
3327
_hoverValue: { type: Number, state: true },
@@ -80,11 +74,6 @@ export class OlStarRating extends LitElement {
8074
white-space: nowrap;
8175
}
8276
83-
:host([disabled]) .stars-wrapper {
84-
opacity: 0.5;
85-
cursor: not-allowed;
86-
}
87-
8877
.star-button {
8978
background: none;
9079
border: none;
@@ -101,18 +90,10 @@ export class OlStarRating extends LitElement {
10190
outline-offset: 2px;
10291
}
10392
104-
.star-button:disabled {
105-
cursor: not-allowed;
106-
}
107-
10893
:host([readonly]) .star-button {
10994
cursor: default;
11095
}
11196
112-
:host([readonly]) .star-button:hover {
113-
transform: none;
114-
}
115-
11697
/* Star sizes */
11798
.star-button.small svg {
11899
width: 16px;
@@ -210,15 +191,6 @@ export class OlStarRating extends LitElement {
210191
align-self: center;
211192
}
212193
213-
.clear-button:hover:not(:disabled) {
214-
background-color: #f5f5f5;
215-
}
216-
217-
.clear-button:disabled {
218-
opacity: 0.6;
219-
cursor: not-allowed;
220-
}
221-
222194
.clear-button:focus-visible {
223195
outline: var(--focus-ring-width) solid var(--color-border-focused);
224196
}
@@ -229,7 +201,6 @@ export class OlStarRating extends LitElement {
229201
this.value = 0;
230202
this.size = 'medium';
231203
this.readonly = false;
232-
this.disabled = false;
233204
this.clearButtonLabel = 'Clear my rating';
234205
this.ratingText = '';
235206
this._hoverValue = null;
@@ -241,7 +212,7 @@ export class OlStarRating extends LitElement {
241212
* Handle star click
242213
*/
243214
_handleStarClick(index) {
244-
if (this.readonly || this.disabled) return;
215+
if (this.readonly) return;
245216

246217
const newValue = index + 1;
247218
this.value = newValue;
@@ -258,23 +229,23 @@ export class OlStarRating extends LitElement {
258229
* Handle star hover
259230
*/
260231
_handleStarHover(index) {
261-
if (this.readonly || this.disabled) return;
232+
if (this.readonly) return;
262233
this._hoverValue = index + 1;
263234
}
264235

265236
/**
266237
* Handle mouse leave from stars wrapper
267238
*/
268239
_handleMouseLeave() {
269-
if (this.readonly || this.disabled) return;
240+
if (this.readonly) return;
270241
this._hoverValue = null;
271242
}
272243

273244
/**
274245
* Handle keyboard navigation
275246
*/
276247
_handleKeyDown(event, index) {
277-
if (this.readonly || this.disabled) return;
248+
if (this.readonly) return;
278249

279250
let newIndex = index;
280251

@@ -342,7 +313,7 @@ export class OlStarRating extends LitElement {
342313
* Handle clear button click
343314
*/
344315
_handleClear() {
345-
if (this.readonly || this.disabled) return;
316+
if (this.readonly) return;
346317

347318
this.value = 0;
348319

@@ -373,8 +344,8 @@ export class OlStarRating extends LitElement {
373344

374345
if (displayValue >= starThreshold) {
375346
return 'full';
376-
} else if (displayValue > previousThreshold && displayValue >= previousThreshold + 0.25) {
377-
// Show half star if value is at least 0.25 into this star
347+
} else if (displayValue > previousThreshold && displayValue >= previousThreshold + 0.5) {
348+
// Show half star if value is at least 0.5 into this star
378349
return 'half';
379350
}
380351

@@ -418,8 +389,7 @@ export class OlStarRating extends LitElement {
418389
class=${classes}
419390
type="button"
420391
aria-label="Rate ${index + 1} out of ${this._totalStars} stars"
421-
tabindex=${this.readonly || this.disabled ? -1 : tabIndex}
422-
?disabled=${this.disabled}
392+
tabindex=${this.readonly ? -1 : tabIndex}
423393
@click=${() => this._handleStarClick(index)}
424394
@mouseenter=${() => this._handleStarHover(index)}
425395
@keydown=${(e) => this._handleKeyDown(e, index)}
@@ -465,12 +435,11 @@ export class OlStarRating extends LitElement {
465435
</div>
466436
` : starsDisplay}
467437
468-
${this.value > 0 && !this.readonly && !this.disabled ? html`
438+
${this.value > 0 && !this.readonly ? html`
469439
<button
470440
class="clear-button"
471441
type="button"
472442
@click=${this._handleClear}
473-
?disabled=${this.disabled}
474443
>
475444
${this.clearButtonLabel}
476445
</button>
@@ -480,6 +449,5 @@ export class OlStarRating extends LitElement {
480449
}
481450
}
482451

483-
// Register the custom element
484452
customElements.define('ol-star-rating', OlStarRating);
485453

0 commit comments

Comments
 (0)