New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

star-meter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

star-meter

A highly customizable React star rating component with support for half-stars, animations, and custom styling

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

Star Rating Component Documentation

The StarRating component is a customizable React component that allows users to rate items using stars. It supports features like half-star ratings, custom colors, animations, and more.

Installation

npm install star-meter

Make sure to include the CSS file for animations:

import "./StarRating.css";

Basic Usage

import StarRating from "star-meter";

function App() {
  const handleRating = (rating) => {
    console.log(`User rated: ${rating}`);
  };

  return <StarRating onSetRating={handleRating} />;
}

Props

PropTypeDefaultDescription
maxRatingnumber5Maximum number of stars to display
colorstring"#fcc419"Color of the stars
textcolorstring"#0d0d0d"Color of the rating text
sizenumber48Size of stars in pixels
messagesarray[]Array of messages to display for each rating
classNamestring""Additional CSS class names
defaultRatingnumber0Initial rating value
onSetRatingfunction() => {}Callback function when rating changes
allowHalfStarsbooleantrueEnable half-star ratings
animationstring"none"Animation style: "scale", "rotate", "bounce", or "none"
allowResetbooleantrueAllow resetting the rating by clicking the same star again

Features

Half-Star Ratings

The component supports half-star ratings by default. Users can hover over the left or right side of a star to select a half or full star rating.

<StarRating allowHalfStars={true} />

To disable half-star ratings:

<StarRating allowHalfStars={false} />

Custom Colors

You can customize the color of the stars and the rating text:

<StarRating color="#ff6b6b" textcolor="#339af0" />

Animations

The component supports several animation styles:

<StarRating animation="scale" /> // Options: "scale", "rotate", "bounce", "none"

Make sure you've imported the CSS file for animations to work properly.

Custom Rating Messages

You can display custom messages for each rating value:

<StarRating
  maxRating={5}
  messages={["Terrible", "Bad", "Okay", "Good", "Excellent"]}
/>

The message corresponding to the current rating will be displayed next to the stars.

Reset Functionality

Users can reset the rating by clicking on the same star again:

<StarRating allowReset={true} />

To disable this feature:

<StarRating allowReset={false} />

Event Handling

The component provides a callback function that is triggered when the rating changes:

const handleRating = (value) => {
  console.log(`New rating: ${value}`);
  // Perform actions based on the new rating
};

<StarRating onSetRating={handleRating} />;

Accessibility

The component is built with accessibility in mind:

  • Stars are keyboard-navigable using Tab and can be activated with Enter or Space
  • Proper role attributes are applied
  • Interactive elements have appropriate tabIndex values

CSS Customization

You can further customize the component with CSS by targeting the provided class names:

/* Example custom styling */
.star {
  margin: 0 2px;
}

.star.scale:hover {
  transform: scale(1.2);
}

.star.rotate:hover {
  transform: rotate(15deg);
}

.star.bounce:hover {
  animation: bounceEffect 0.3s ease;
}

@keyframes bounceEffect {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

Example

import React, { useState } from "react";
import StarRating from "star-rating-component";

function MovieRating() {
  const [userRating, setUserRating] = useState(0);

  return (
    <div className="movie-rating">
      <h3>Rate this movie:</h3>
      <StarRating
        maxRating={5}
        size={32}
        color="#ffd700"
        defaultRating={userRating}
        onSetRating={setUserRating}
        messages={["Poor", "Fair", "Good", "Very good", "Excellent"]}
        animation="scale"
      />
      {userRating > 0 && <p>You rated this movie: {userRating} stars</p>}
    </div>
  );
}

Internal Structure

The component consists of two main parts:

  • The StarRating container component that manages state and handles events
  • The Star component that renders individual stars

Browser Support

The component should work in all modern browsers that support React and SVG.

License

MIT

Keywords

react

FAQs

Package last updated on 22 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts