@mshafiqyajid/react-rating

v0.1.0 ~18 KB 0 deps react

Headless star-rating hook and styled component for React. Half-step support, hover preview, custom icons (hearts, thumbs, anything), full keyboard nav, CSS clip-path half-fill.

$ npm install @mshafiqyajid/react-rating

Playground #

Rate this
Props
5
TSX
import { RatingStyled } from "@mshafiqyajid/react-rating/styled";
import "@mshafiqyajid/react-rating/styles.css";

<RatingStyled />

Install #

npm install @mshafiqyajid/react-rating

Quick start #

import { RatingStyled } from "@mshafiqyajid/react-rating/styled";
import "@mshafiqyajid/react-rating/styles.css";

const [value, setValue] = useState(0);

<RatingStyled
  count={5}
  value={value}
  onChange={setValue}
  showValue
/>

Recipes #

Read-only display (e.g. average rating)

<RatingStyled
  count={5}
  value={4.5}
  readOnly
  showValue
  label="Average rating"
/>

Heart icon

<RatingStyled
  count={5}
  tone="danger"
  icon={<HeartIcon />}
  defaultValue={3}
/>

Per-star labels with onHover

const labels = ["Terrible", "Poor", "OK", "Good", "Great"];
const [hovered, setHovered] = useState<number | null>(null);

<>
  <RatingStyled
    count={5}
    value={value}
    onChange={setValue}
    onHover={setHovered}
  />
  <span>{labels[Math.ceil((hovered ?? value) - 1)] ?? ""}</span>
</>

API #

RatingStyled

PropTypeDefaultDescription
countnumber5Number of stars.
valuenumberControlled value (0..count).
defaultValuenumber0Uncontrolled initial.
onChange(value: number) => voidFires on commit.
onHover(value: number | null) => voidFires during hover.
allowHalfbooleantrueEnable half-step values.
clearablebooleantrueRe-clicking active value clears it.
readOnlybooleanfalseDisplay only, no interaction.
sizesm | md | lgmdSize.
toneneutral | primary | success | warning | dangerwarningFill color.
iconReactNodestar SVGCustom icon for both layers.
showValuebooleanfalseShow numeric value badge.
labelReactNodeLabel above.
hintReactNodeHelper text below.
Edit this page on GitHub