1.0.2 • Published 7 months ago

react-rating-star-component v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Star Rating Component

This StarRating component is a fully customizable React component that allows users to rate items using a star-based system. It supports dynamic customization of colors, size, tooltips, and accessibility.

Installation

To use this component, copy the StarRating code into your project or install it from your own library if published.

Usage

Here is an example of how to use the StarRating component in your project.

Basic Example

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

const App: React.FC = () => {
  const handleRatingChange = (rating: number) => {
    console.log('Selected Rating:', rating);
  };

  return (
    <div>
      <h1>Star Rating Example</h1>
      <StarRating onChange={handleRatingChange} />
    </div>
  );
};

export default App;

Customization Example

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

const App: React.FC = () => {
  const handleRatingChange = (rating: number) => {
    console.log('Selected Rating:', rating);
  };

  return (
    <div>
      <h1>Custom Star Rating</h1>
      <StarRating
        totalStars={10} // Display 10 stars
        initialRating={3} // Initial rating set to 3 stars
        activeColor="gold" // Selected stars color
        inactiveColor="lightgray" // Unselected stars color
        size="3rem" // Size of the stars
        gap="10px" // Gap between stars
        onChange={handleRatingChange} // Callback function on rating change
        tooltipTexts={['Poor', 'Fair', 'Good', 'Very Good', 'Excellent']} // Custom tooltips
        ariaLabel="Custom star rating" // Accessible label
      />
    </div>
  );
};

export default App;

Props

Prop NameTypeDefaultDescription
totalStarsnumber5Total number of stars to display.
initialRatingnumber0Initial selected rating.
onChange(rating: number) => voidundefinedCallback function triggered when the rating changes.
activeColorstringyellowColor for selected stars.
inactiveColorstringgrayColor for unselected stars.
sizestring or number2remFont size of the stars.
gapstring or number5pxSpace between stars.
classNamestring''Custom class name for additional styling.
tooltipTextsstring[]Auto-generatedArray of tooltips for each star. Defaults to "1 Star", "2 Stars", etc.
ariaLabelstringStar ratingAccessible label for the star rating component.

Features

  • Customizable Colors: Adjust the colors of selected and unselected stars.
  • Dynamic Star Count: Define the number of stars to display.
  • Tooltips: Provide helpful tooltips for each star.
  • Accessibility: Includes ARIA attributes for screen readers.
  • Smooth Transitions: Color changes are animated for a better user experience.

Styling

You can style the component further using the className prop or modify the inline styles directly in the component code.

Example CSS

.star-rating-container {
  display: flex;
  gap: 5px;
}

.star-button {
  background: none;
  border: none;
  cursor: pointer;
  transition: color 0.2s;
}

Contributing

We welcome contributions! If you find bugs or have feature requests, please open an issue or submit a pull request on the GitHub repository.

License

This component is open-source and can be freely used and modified.


Enjoy building with the StarRating component! If you encounter any issues or have suggestions for improvements, feel free to reach out.

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago