0.2.0 • Published 3 years ago

react-star-rate v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Features

  • 🔩 Easily Customizable
  • 🕊 Lightweight - less than 4kb including styles
  • Accessible

Table of contents

Example

You can visit the example here: View on CodeSandbox

Installation

With yarn

yarn add react-star-rate

With NPM

npm install react-star-rate

Getting Started

You can add React Stars Rating to your project using the <StarsRating /> component.

import { useState } from 'react';

import StarsRating from 'react-star-rate';

const App = () => {
  const [value, setValue] = useState(0);
  return (
    <div>
      <StarsRating
        value={value}
        onChange={value => {
          setValue(value);
        }}
      />
    </div>
  );
};

Props

NameTypeDefaultDescription
allowClearbooleantrueAllow the value to be reset when clicked again
allowHalfbooleantrueAllow half of the star to be selected
autoFocusboolean-Automatically focus the element
classNamePrefixstring"react-star-rate"The components will have classNames with the given prefix
countnumber5Number of stars
defaultValuenumber0The default value of the stars. Should be the same as the default useState value
directionstring"ltr"The direction of the selected stars. Either "ltr" or "rtl"
disabledbooleanfalseDisable the rating element
onBlurfunction-Will be triggered on blur
onChangefunction(value) => {}Will be triggered on change of value
onFocusfunction-Will be triggered on focus
onHoverChangefunction(value) => {}Will be triggered on hover
styleobject{}Custom styles. See styling
symbolstring"★"The symbol to be displayed
tabIndexnumber0The tab index of the stars container
valuenumber-Controlled value of the component

Styling

Style Object

Each component inside the StarsRating component is keyed and ships with default styles. You can apply custom styles by accessing the key on the style prop.

Style Keys

  • style.style - The main wrapper ul element.
  • style.full - Styles for when the star is fully active.
    • style.full.star - The wrapper li element every star.
    • style.full.first - The first half star, on top.
    • style.full.second - The second full star, in the background.
  • style.half - Styles for when the star is half active.
    • style.half.star - The wrapper li element every star.
    • style.half.first - The first half star, on top.
    • style.half.second - The second full star, in the background.
  • style.zero - Styles for when the star is not active.
    • style.zero.star - The wrapper li element every star.
    • style.zero.first - The first half star, on top.
    • style.zero.second - The second full star, in the background.
  • style.hover - Styles for when the element is hovered over. You can use access of the keys listed above in the hover object.

With CSS

You can also apply custom styles using CSS Stylesheets. The classnames are determined with the classNamePrefix prop (defaults to "react-star-rate").

Classnames

  • ${classNamePrefix} - The main ul element.
  • ${classNamePrefix}--ltr | - ${classNamePrefix}--rtl - If the rating component is from left-to-right or right-to-left respectively.
  • ${classNamePrefix}__star - The star li element.
  • ${classNamePrefix}__star--zero | ${classNamePrefix}__star--half | ${classNamePrefix}__star--full - When the star is inactive, half, or completely full.
  • ${classNamePrefix}__star__first - The first half star, on top.
  • ${classNamePrefix}__star__second - The second full star, in the background.

License

MIT Licensed. Copyright (c) 2021-present, Raymon Zhang.

0.2.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago