@molecule/app-rating-display-vue

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-vue

npm · Source on GitHub

How it works

@molecule/app-rating-display-vue 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 { createApp, defineComponent, h, ref } from 'vue'
import { RatingDisplay } from '@molecule/app-rating-display-vue'

const scrollToReviews = (): void => {
  document.getElementById('reviews')?.scrollIntoView()
}

const ProductRating = defineComponent({
  setup() {
    const rating = ref(0)
    return () =>
      h('div', [
        // First: read-only with review count. Second: interactive — user picks a rating.
        h(RatingDisplay, { value: 4.5, reviewCount: 128, onReviewsClick: scrollToReviews }),
        h(RatingDisplay, {
          value: rating.value,
          size: 'lg',
          onChange: (v: number) => {
            rating.value = v
          },
        }),
      ])
  },
})

createApp(ProductRating).mount('#app')

Works with: @molecule/app-ui, @molecule/app-ui-vue, @molecule/app-vue

Reference

Vue star-rating display.

Exports RatingDisplay — fractional star rendering (inline SVG), optional review count tail, optional interactive mode via onChange. It is the Vue 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.ts JSDoc, not this file.

Quick Start

import { createApp, defineComponent, h, ref } from 'vue'
import { RatingDisplay } from '@molecule/app-rating-display-vue'

const scrollToReviews = (): void => {
  document.getElementById('reviews')?.scrollIntoView()
}

const ProductRating = defineComponent({
  setup() {
    const rating = ref(0)
    return () =>
      h('div', [
        // First: read-only with review count. Second: interactive — user picks a rating.
        h(RatingDisplay, { value: 4.5, reviewCount: 128, onReviewsClick: scrollToReviews }),
        h(RatingDisplay, {
          value: rating.value,
          size: 'lg',
          onChange: (v: number) => {
            rating.value = v
          },
        }),
      ])
  },
})

createApp(ProductRating).mount('#app')

Type

feature

Installation

npm install @molecule/app-rating-display-vue @molecule/app-ui @molecule/app-ui-vue @molecule/app-vue vue

API

Interfaces

RatingDisplayProps

Props for RatingDisplay.

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
}

Constants

RatingDisplay

Star-rating display supporting fractional values and an optional review-count tail. When onChange is provided each star becomes an interactive button.

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 is a plain flex span, 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.

const RatingDisplay: DefineComponent<
  ExtractPropTypes<{
    value: { type: NumberConstructor; required: true }
    max: { type: NumberConstructor; default: number }
    reviewCount: { type: NumberConstructor; default: undefined }
    onReviewsClick: { type: PropType<RatingDisplayProps['onReviewsClick']>; default: undefined }
    size: { type: PropType<'sm' | 'md' | 'lg'>; default: string }
    onChange: { type: PropType<RatingDisplayProps['onChange']>; default: undefined }
    ariaLabel: { type: StringConstructor; default: undefined }
    className: { type: StringConstructor; default: undefined }
  }>,
  () => VNode,
  {},
  {},
  {},
  ComponentOptionsMixin,
  ComponentOptionsMixin,
  {},
  string,
  PublicProps,
  Readonly<
    ExtractPropTypes<{
      value: { type: NumberConstructor; required: true }
      max: { type: NumberConstructor; default: number }
      reviewCount: { type: NumberConstructor; default: undefined }
      onReviewsClick: { type: PropType<RatingDisplayProps['onReviewsClick']>; default: undefined }
      size: { type: PropType<'sm' | 'md' | 'lg'>; default: string }
      onChange: { type: PropType<RatingDisplayProps['onChange']>; default: undefined }
      ariaLabel: { type: StringConstructor; default: undefined }
      className: { type: StringConstructor; default: undefined }
    }>
  > &
    Readonly<{}>,
  {
    onReviewsClick: ((event?: MouseEvent) => void) | undefined
    onChange: ((value: number) => void) | undefined
    max: number
    reviewCount: number
    size: 'sm' | 'md' | 'lg'
    ariaLabel: string
    className: string
  },
  {},
  {},
  {},
  string,
  ComponentProvideOptions,
  true,
  {},
  any
>
  • props — Component props (see RatingDisplayProps).

Returns: The rating display component.

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-vue ^1.1.0
  • @molecule/app-vue ^3.5.0 || ^4.0.0
  • vue ^3.4.0

Runtime Dependencies

  • @molecule/app-ui
  • @molecule/app-ui-vue
  • @molecule/app-vue
  • vue

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.