Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rubenvara/react-star-rating

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rubenvara/react-star-rating

A simple React component that shows a rating from 0 to 5 with svg stars when passed a number

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

React Star Rating

What is this

A simple React component that shows a rating from 0 to 5 with svg stars. Just need to pass a number. No dependencies required.

Installation

As with any other node package in React, Gatsby, Next.js, etc.:

npm i @rubenvara/react-star-rating

Usage

The package exports a React component.

The component takes a number of props:

PropTypeDefaultRequiredDescription
ratingNumber-YesThe rating. Any number from 0 to 5.0
configObject{}NoConfig options for the component. See below
styleObject{}NoCSS styles passed directly to the container component

About the config object:

OptionTypeDefaultDescription
fullColorString'#ffcf00'The color for the filling of the stars
emptyColorString'#7f7f7f'The color for the empty parts of the stars
sizeNumber20The size of the stars. Pass a number of pixels.
showTextBooleanfalseShow the rating number next to the stars or not

About the style prop: It accepts an object of css styles, written in camelCase to inline in JSX components. It is passed directly, unchecked, to the main div. Use it cautiously. Check the example below.

Be aware

  • Any css-valid color is accepted (hsl, hex, rgb, string, etc.).
  • Stars are 1:1 proportionate (width equals height, so both equal the size property).
  • If the rating number is shown (when config.showText = true), font size is half the star size or 16px, whichever is higher.

Example

Use it as follows:

Simple
import StarRating from '@rubenvara/react-star-rating';

// ...

return <StarRating rating={3.35} />;

// ...

Output:

Simple example of React Star Rating

More advanced
import StarRating from '@rubenvara/react-star-rating';

// ...

const rating = 3.35;
const config = {
  fullColor: '#f05',
  emptyColor: 'hsl(240, 80%, 85%)',
  size: 42,
  showText: true,
};
const style = {border: `1px solid`, borderColor: `firebrick`, padding: 12};

// ...

return <StarRating rating={rating} config={config} style={style} />;

//...

Output:

Advanced example of React Star Rating

Be careful

  • Passing a rating higher than 5.0 or lower than 0.0 will break the component.

Future?

  • Write tests.
  • Check the user input rating and return an error or something more usefull than just breaking the thing.
  • Improve styling of the rating number.

Keywords

FAQs

Package last updated on 10 Dec 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc