Vue Star Rating Component
`vue-star-rating` is a highly customizable star rating component for Vue.js applications, offering extensive control over its appearance and behavior. It maintains separate versions to support both Vue 2.x (primarily version 1.6.x, which remains the default `npm install`) and Vue 3 (via version 2.x, installed using `npm install vue-star-rating@next`). The library features a wide array of customization options, including custom star shapes (`star-points`), rounded corners, individual star coloring, glow effects, and full support for RTL (right-to-left) layouts. It integrates seamlessly with Vue's `v-model` for bidirectional data binding and provides fine-grained control over initial rating rounding and increment steps. The project actively develops the Vue 3 compatible version, ensuring continued relevance and compatibility within the evolving Vue ecosystem.
Common errors
-
Rating not being set to 0 when clicking outside the star or to the left of the first star.
cause A 'dead' column of pixels prevented resetting to zero or selecting the lowest increment. This was a known issue (issue #22).fixThis issue was resolved in `vue-star-rating@1.5.0`. Upgrade to this version or newer for Vue 2 projects. -
Glow effect is always on, even when not intended, particularly prominent on dark backgrounds.
cause An issue where the `glow` effect was permanently active due to a bug in the component's styling or logic.fixThis specific glow issue was fixed in `vue-star-rating@2.0.3`. Upgrade to this version or newer for Vue 3 projects. -
Initial rating values are being rounded despite `round-start-rating` being set to `false`, especially with async data.
cause A bug in versions prior to 1.6.1 caused `round-start-rating` to not correctly apply when the initial rating was loaded asynchronously (issue #36).fixUpgrade to `vue-star-rating@1.6.1` or higher for Vue 2 projects to correctly apply `round-start-rating` with async data. -
Styling of the star rating component is incorrect or clashing with other CSS in my application.
cause Older versions of the component (prior to v1.4.0) used generic CSS class names (`.star-rating`) that could easily conflict with other project styles (issue #12).fixUpgrade to `vue-star-rating@1.4.0` or newer for Vue 2 projects. This version introduced specific class names (e.g., `.vue-star-rating`) to avoid clashes. Review and update any custom CSS targeting the old class names.
Warnings
- breaking Version 2.x of `vue-star-rating` is specifically for Vue 3, while version 1.6.x and below are for Vue 2. Installing via `npm install vue-star-rating` defaults to the Vue 2 compatible version. For Vue 3, you MUST install with `npm install vue-star-rating@next`.
- breaking Starting with Vue 3 compatible versions (v2.0.2+), the `rating` argument must now be explicitly passed for the component to function correctly.
- breaking Internal CSS class names were changed in v1.4.0 to prevent clashes. Custom CSS targeting old class names will no longer work.
- gotcha Prior to v2.1.0, enabling the `glow` prop required also explicitly setting a `glow-color`. In v2.1.0+, `glow-color` is optional as it will default if not provided.
- gotcha The `round-start-rating` prop, intended to prevent initial rating rounding, had an issue where it would still round when the rating was set asynchronously. This was fixed in v1.6.1.
- gotcha The `active-color` and `active-border-color` props can accept an array of color values in v2.1.0+ to color each star individually. If a single string is provided, all stars will share that color.
Install
-
npm install vue-star-rating -
yarn add vue-star-rating -
pnpm add vue-star-rating
Imports
- StarRating
import { StarRating } from 'vue-star-rating';import StarRating from 'vue-star-rating';
- StarRating (Vue 3)
import StarRating from 'vue-star-rating';
- StarRating (CommonJS Vue 2)
const StarRating = require('vue-star-rating');
Quickstart
import { createApp, ref } from 'vue';
import StarRating from 'vue-star-rating';
const app = createApp({
components: {
StarRating
},
setup() {
const currentRating = ref(3.5);
const readOnlyRating = ref(4.2);
const setRating = (rating) => {
currentRating.value = rating;
console.log('New rating:', rating);
};
return {
currentRating,
readOnlyRating,
setRating
};
},
template: `
<div>
<h1>Interactive Star Rating</h1>
<star-rating
v-model:rating="currentRating"
:increment="0.5"
:star-size="30"
:show-rating="false"
:glow="5"
glow-color="#ffd055"
active-color="#FFD700"
inactive-color="#D8D8D8"
border-color="#ccc"
:border-width="2"
:rounded-corners="true"
:padding="5"
@update:rating="setRating"
></star-rating>
<p>Your current rating: {{ currentRating }}</p>
<h2>Read-only Rating</h2>
<star-rating
:rating="readOnlyRating"
:read-only="true"
:increment="0.01"
:star-size="25"
:fixed-points="2"
:show-rating="true"
active-color="#42b883"
inactive-color="#f0f0f0"
></star-rating>
<p>Average rating: {{ readOnlyRating }}</p>
</div>
`
});
app.mount('#app');