@molecule/app-rating-display-angular
Feature · rating-display · App (browser) · v1.1.2 · Apache-2.0
Read-only and interactive star rating display with half-star support
npm install @molecule/app-rating-display-angularHow it works
@molecule/app-rating-display-angular is a ready-made rating-display feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { Component, signal } from '@angular/core'
import { RatingDisplay } from '@molecule/app-rating-display-angular'
@Component({
selector: 'app-product-rating',
standalone: true,
imports: [RatingDisplay],
template: `
<span mol-rating-display [value]="4.5" [reviewCount]="128" (click)="scrollToReviews()"></span>
<span mol-rating-display [value]="rating()" size="lg" [onChange]="setRating"></span>
`,
})
export class ProductRating {
readonly rating = signal(0)
readonly setRating = (value: number): void => this.rating.set(value)
scrollToReviews(): void {
document.getElementById('reviews')?.scrollIntoView()
}
}Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular star-rating display.
Exports RatingDisplay — fractional star rendering (inline SVG), optional
review count tail, optional interactive mode via onChange. It is the
Angular counterpart of @molecule/app-rating-display-react: same props
(React names), same DOM for the same input.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { Component, signal } from '@angular/core'
import { RatingDisplay } from '@molecule/app-rating-display-angular'
@Component({
selector: 'app-product-rating',
standalone: true,
imports: [RatingDisplay],
template: `
<span mol-rating-display [value]="4.5" [reviewCount]="128" (click)="scrollToReviews()"></span>
<span mol-rating-display [value]="rating()" size="lg" [onChange]="setRating"></span>
`,
})
export class ProductRating {
readonly rating = signal(0)
readonly setRating = (value: number): void => this.rating.set(value)
scrollToReviews(): void {
document.getElementById('reviews')?.scrollIntoView()
}
}
Type
feature
Installation
npm install @molecule/app-rating-display-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
API
Interfaces
RatingDisplayProps
Props accepted by RatingDisplay — the same props
@molecule/app-rating-display-react accepts. Every one is an input:
onReviewsClick and onChange are inputs holding the callback functions
React takes as props.
interface RatingDisplayProps {
/** Rating value (0–max). Fractional supported. */
value: number
/** Maximum rating. Defaults to 5. */
max?: number
/** Optional review count shown after the stars. */
reviewCount?: number
/**
* Called when the review-count is clicked (renders it as a button). React
* passes the click event, so this receives it too.
*/
onReviewsClick?: (event?: MouseEvent) => void
/** Size preset. */
size?: 'sm' | 'md' | 'lg'
/** Interactive mode — star clicks call `onChange(starIndex + 1)`. */
onChange?: (value: number) => void
/** Accessible label override; defaults to the translated `Rating: {value} of {max}`. */
ariaLabel?: string
/** Extra classes. */
className?: string
}
Classes
RatingDisplay
RatingDisplay component.
Star-rating display supporting fractional values and an optional
review-count tail. When onChange is provided each star becomes an
interactive button. The host IS the outer <span>
(span[mol-rating-display]), matching React's root exactly, so the root's
class is a host binding; the stars sit in an inner span that carries the
rating label — role="img" when read-only, role="group" (holding the
rate buttons) in interactive mode — and the root carries no role, exactly
as React's does.
Uses inline SVG stars so the component works without icon-font fallback.
Same props (React names) and same DOM as
@molecule/app-rating-display-react's RatingDisplay.
@Component({
selector: 'span[mol-rating-display]',
standalone: true,
inputs: [
'value',
'max',
'reviewCount',
'onReviewsClick',
'size',
'onChange',
'ariaLabel',
'className',
],
host: {
'[class]': 'classes()',
},
template: `<span
[class]="starsClasses()"
[attr.role]="onChange ? 'group' : 'img'"
[attr.aria-label]="label()"
>
@for (star of stars(); track star.i) {
@if (onChange) {
<button
type="button"
(click)="select(star.i)"
[attr.aria-pressed]="pressed(star.i) ? 'true' : 'false'"
[attr.aria-label]="rateLabel(star.i)"
>
<svg [attr.width]="px()" [attr.height]="px()" viewBox="0 0 24 24" aria-hidden="true">
<defs>
<linearGradient [attr.id]="star.id">
<stop [attr.offset]="offset(star.fill)" stop-color="currentColor"></stop>
<stop
[attr.offset]="offset(star.fill)"
stop-color="currentColor"
stop-opacity="0.25"
></stop>
</linearGradient>
</defs>
<path [attr.d]="path" [attr.fill]="fillUrl(star.id)"></path>
</svg>
</button>
} @else {
<svg [attr.width]="px()" [attr.height]="px()" viewBox="0 0 24 24" aria-hidden="true">
<defs>
<linearGradient [attr.id]="star.id">
<stop [attr.offset]="offset(star.fill)" stop-color="currentColor"></stop>
<stop
[attr.offset]="offset(star.fill)"
stop-color="currentColor"
stop-opacity="0.25"
></stop>
</linearGradient>
</defs>
<path [attr.d]="path" [attr.fill]="fillUrl(star.id)"></path>
</svg>
}
}
</span>
@if (reviewCount !== undefined) {
@if (onReviewsClick) {
<button type="button" (click)="onReviewsClick($event)" [class]="reviewClasses()">
({{ reviewCount }})
</button>
} @else {
<span [class]="textClasses()">({{ reviewCount }})</span>
}
}`,
})
export class RatingDisplay {
/** Rating value (0–max). Fractional supported. */
value!: number
/** Maximum rating (defaults to 5 at read time). */
max?: number
/** Optional review count shown after the stars. */
reviewCount?: number
/** Called when the review-count is clicked; receives the DOM click event. */
onReviewsClick?: (event?: MouseEvent) => void
/** Size preset (defaults to `'md'` at read time). */
size?: 'sm' | 'md' | 'lg'
/** Interactive mode — star clicks call `onChange(starIndex + 1)`. */
onChange?: (value: number) => void
/** Accessible label override; defaults to the translated `Rating: {value} of {max}`. */
ariaLabel?: string
/** Extra classes. */
className?: string
/** The star's single SVG path. */
readonly path = STAR_PATH
/** The i18n handle (locale-reactive, so the labels follow a switch). */
private readonly i18n = useTranslation()
/**
* The current class map.
* @returns The UIClassMap.
*/
cm(): UIClassMap {
return getClassMap()
}
/**
* The effective maximum (React's default parameter: a later-bound write of
* `undefined` must not erase the `5` default).
* @returns The maximum rating.
*/
maxValue(): number {
return this.max ?? 5
}
/**
* The star size in pixels (React's `SIZE_PX[size]`, which is `undefined` for
* an unknown size so both axes are omitted).
* @returns The pixel size.
*/
px(): number | undefined {
return SIZE_PX[this.size ?? 'md']
}
/**
* One render state per star: its index, its fractional fill and its gradient
* id (minted per render, as React mints one per star per render).
* @returns The star states.
*/
stars(): StarState[] {
return Array.from({ length: this.maxValue() }, (_value, i) => ({
i,
fill: Math.max(0, Math.min(1, this.value - i)),
id: starId(),
}))
}
/**
* The gradient stop offset for a fill fraction.
* @param fill - Star fill fraction (0–1).
* @returns The percentage offset.
*/
offset(fill: number): string {
return `${fill * 100}%`
}
/**
* The `<path>` fill referencing the star's gradient.
* @param id - The star's gradient id.
* @returns The `url(#id)` fill.
*/
fillUrl(id: string): string {
return `url(#${id})`
}
/**
* The per-star button label in interactive mode.
* @param i - Star index (0-based).
* @returns The accessible label.
*/
rateLabel(i: number): string {
return this.i18n.t(
'ratingDisplay.aria.rate',
{ value: i + 1, max: this.maxValue() },
{ defaultValue: 'Rate {{value}} of {{max}}' },
)
}
/**
* Calls `onChange` with the rating a clicked star selects.
* @param i - Star index (0-based).
*/
select(i: number): void {
this.onChange?.(i + 1)
}
/**
* Whether the star at index `i` is pressed: it is within the rounded rating.
* @param i - Star index (0-based).
* @returns The button's `aria-pressed` state.
*/
pressed(i: number): boolean {
return i + 1 <= Math.round(this.value)
}
/**
* The root `<span>` class list (`cm.flex({ align: 'center', gap: 'xs' })` +
* `className`).
* @returns The class string.
*/
classes(): string {
return this.cm().cn(this.cm().flex({ align: 'center', gap: 'xs' }), this.className)
}
/**
* The inner stars span class (`cm.flex({ align: 'center', gap: 'xs' })`, the
* same list as the root so wrapping the stars does not move the layout).
* @returns The class string.
*/
starsClasses(): string {
return this.cm().flex({ align: 'center', gap: 'xs' })
}
/**
* The root `aria-label` (`ariaLabel`, or the translated default).
* @returns The accessible label.
*/
label(): string {
return (
this.ariaLabel ??
this.i18n.t(
'ratingDisplay.aria.rating',
{ value: this.value, max: this.maxValue() },
{ defaultValue: 'Rating: {{value}} of {{max}}' },
)
)
}
/**
* The review-count button class (`cm.textSize('sm')` + `cm.link`).
* @returns The class string.
*/
reviewClasses(): string {
return this.cm().cn(this.cm().textSize('sm'), this.cm().link)
}
/**
* The review-count text span class (`cm.textSize('sm')`).
* @returns The class string.
*/
textClasses(): string {
return this.cm().textSize('sm')
}
}
Injection Notes
Requirements
Peer dependencies:
@angular/common^22.0.0@angular/core^22.0.0@angular/platform-browser^22.0.0@molecule/app-angular^3.5.0 || ^4.0.0@molecule/app-ui^1.0.1@molecule/app-ui-angular^1.1.0
Runtime Dependencies
@angular/common@angular/core@angular/platform-browser@molecule/app-angular@molecule/app-ui@molecule/app-ui-angular
The host element IS the outer <span> (span[mol-rating-display]), exactly
as in React. Stars are inline SVG filled with currentColor — set a text
color on the wrapper (e.g. a warning/amber token) to tint them; unfilled
portions render at 25% opacity of the same color. Accessible labels are
translated through useTranslation(), matching the React package and
following a locale switch: the container label (ratingDisplay.aria.rating,
overridable via ariaLabel) and the per-star "Rate N of M" button labels in
interactive mode (ratingDisplay.aria.rate). Requires a wired ClassMap bond.
Translations
Translation strings are provided by @molecule/app-locales-rating-display.