Modus UI

Star Rating

React Native (Expo)

A React Native star rating you can tap or scrub, with half stars, popping fills and adjustable screen reader support.

A star rating you can tap or scrub. Drag across the row and the rating follows the pointer, snapping to whole stars (or halves); dragging past the left edge clears it. Each star pops as it fills. Screen readers get an adjustable control that says "3 of 5", and a read-only rating is announced as an image with the same text. Built for React Native and Expo with react-native only; this page previews it in the browser through react-native-web.

Installation

CLI

npx @modus-ui/cli add star-rating

Manual

  1. Copy the code from the component tab.
  2. Create a file at components/ui/star-rating.tsx.
  3. Paste the code.

It needs no packages beyond react-native, which your Expo or React Native app already has.

Usage

tsx
import { useState } from "react";
import { StarRating } from "@/components/ui/star-rating";

export default function Example() {
  const [rating, setRating] = useState(4);

  return <StarRating value={rating} onChange={setRating} allowHalf />;
}

Props

PropTypeDefaultDescription
valuenumber-The current rating, from 0 to max.
onChange(value: number) => void-Called with the new rating whenever a tap or drag changes it.
maxnumber5How many stars there are.
sizenumber34Width and height of each star.
allowHalfbooleanfalseLets the rating land on halves: the left half of a star counts as half.
readOnlybooleanfalseShows the rating without letting anyone change it.
labelstring"Rating"Spoken name of the control.
colorsPartial<StarRatingColors>-Overrides filled and empty.
styleStyleProp<ViewStyle>-Style for the row.

Comments

  • No comments yet.