A simple, zero-dependency star rating component for Svelte 5. It supports partial fills, full keyboard and mouse interaction, a readonly display mode, and per-instance customization of colors, size, and stroke.
Install the package with your preferred package manager:
npm install @devekkx/svelte-star-rating
# or
pnpm add @devekkx/svelte-star-rating
# or
bun add @devekkx/svelte-star-rating
This package requires Svelte 5 as a peer dependency.
Here is a basic interactive rating input:
<script lang="ts">
import { StarRating } from '@devekkx/svelte-star-rating';
let value = $state(4.4);
const config = {
readonly: false,
maxVal: 5,
minVal: 0,
step: 0.1,
numOfStars: 5,
starConfig: {
size: 26,
filledColor: '#F98416',
unfilledColor: '#5D5D5D'
}
};
</script>
<StarRating bind:value {config} /><p>Current rating: {value}</p>
And here is a read-only display, useful for showing an existing score:
<script lang="ts">
import { StarRating } from '@devekkx/svelte-star-rating';
let value = 3.7;
const config = {
readonly: true,
minVal: 0,
maxVal: 5,
step: 0.1,
numOfStars: 5,
starConfig: {
size: 20,
filledColor: '#ffc107',
unfilledColor: '#e0e0e0'
}
};
</script>
<StarRating {config} bind:value />
When readonly is true, the slider is disabled and the displayed rating cannot be changed by the user.
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| config | ConfigI | Yes | - | Controls behavior and appearance |
| value | number | No | 4.54 | The current rating. Use bind:value to read and update it. |
export interface StarConfigI {
size: number; // Pixel size of each star SVG
filledColor: string; // Fill color for filled stars
unfilledColor: string; // Fill color for unfilled stars
strokeColor?: string; // Outline color for each star. Defaults to filledColor when not set.
}
export interface ConfigI {
name?: string; // Name attribute on the hidden range input. Defaults to "stars".
readonly: boolean; // When true, the rating cannot be changed by the user.
numOfStars: number; // How many stars to render.
minVal: number; // Minimum selectable value.
maxVal: number; // Maximum selectable value.
step: number; // Granularity of the rating (e.g. 0.5 for half-stars, 1 for whole stars).
starConfig: StarConfigI;
styles?: {
containerStyles?: string; // Inline CSS for the outer container element.
starStyles?: string; // Inline CSS for the row of stars (useful for controlling gap).
};
}
Each star has an outline that you can control with strokeColor inside starConfig. If you do not set it, the outline defaults to the same color as filledColor, making it visible on unfilled stars but seamless on filled ones.
A darker shade of your fill color is a common choice for the stroke:
starConfig: {
size: 26,
filledColor: '#F98416',
unfilledColor: '#5D5D5D',
strokeColor: '#C8690F' // a darker orange outline
}
Or use a completely different color to create a distinct border effect:
starConfig: {
size: 26,
filledColor: '#ffd700',
unfilledColor: '#e0e0e0',
strokeColor: '#999999'
}
The value prop can be fractional. Stars at indices below Math.floor(value) are fully filled. The star at index Math.floor(value) is partially filled to show the fractional part. Stars beyond that are empty.
For example, with value = 3.7 and 5 stars:
The step field controls how finely the user can adjust the rating. Use step: 1 for whole stars, step: 0.5 for half stars, or step: 0.1 for tenth-star precision.
The component uses inline SVG fills, so star colors work without any external CSS.
To control layout and spacing, pass a styles object:
containerStyles is applied to the outer section wrapper. Use it to set margins, borders, padding, width, and alignment.starStyles is applied to the row that holds the stars. Use it to control the gap between stars.Example:
<script lang="ts">
import { StarRating, type ConfigI } from '@devekkx/svelte-star-rating';
let value = $state(4.4);
const config: ConfigI = {
readonly: false,
maxVal: 5,
minVal: 0,
step: 0.1,
numOfStars: 5,
styles: {
containerStyles:
'border: 1px solid #ddd; padding: 8px; border-radius: 8px; width: max-content;',
starStyles: 'gap: 0.2rem;'
},
starConfig: {
size: 26,
filledColor: '#F98416',
unfilledColor: '#5D5D5D',
strokeColor: '#C8690F'
}
};
</script>
<StarRating bind:value {config} />
Five stars, whole-number steps:
<script lang="ts">
import { StarRating } from '@devekkx/svelte-star-rating';
let value = $state(0);
const config = {
readonly: false,
minVal: 0,
maxVal: 5,
step: 1,
numOfStars: 5,
starConfig: { size: 32, filledColor: 'gold', unfilledColor: '#ccc' }
};
</script>
<StarRating bind:value {config} /><p>Your rating: {value}</p>
Ten stars, quarter-step precision:
<script lang="ts">
import { StarRating } from '@devekkx/svelte-star-rating';
let value = $state(7.5);
const config = {
readonly: false,
minVal: 0,
maxVal: 10,
step: 0.25,
numOfStars: 10,
starConfig: { size: 18, filledColor: '#4f46e5', unfilledColor: '#e5e7eb' }
};
</script>
<StarRating bind:value {config} />
When readonly is false, the underlying range input is keyboard-focusable and operable with arrow keys. Consider adding a visible label or aria-label attribute in your application to describe what the rating is for.
The component is exported as a named export:
import { StarRating } from '@devekkx/svelte-star-rating';
// types are also available
import type { ConfigI, StarConfigI } from '@devekkx/svelte-star-rating';
It requires Svelte 5. It renders correctly in server-side rendering environments since it is purely presentational. The range input interaction is client-only.
MIT License. Copyright 2025 Emmanuel Komla Kpendo.