UIPackage React Native
Search

Rating

Whole-star selection and fractional read-only display with themed icons and controlled or default values.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add rating

Source files

Expand a file to read the source the installer copies into your app.

components/ui/rating/Rating.tsx
import * as React from 'react'
import { Pressable, View, type ViewProps } from 'react-native'
import { Star } from 'lucide-react-native'
import { useThemeColors } from '@/lib/theme'
import { cn } from '@/lib/utils'

export interface RatingProps extends Omit<ViewProps, 'children'> {
  value?: number
  defaultValue?: number
  onValueChange?: (value: number) => void
  max?: number
  disabled?: boolean
  readOnly?: boolean
  className?: string
  /** Localize the name of each selectable whole-star value. */
  getValueLabel?: (value: number, max: number) => string
}

export const Rating = React.forwardRef<React.ComponentRef<typeof View>, RatingProps>(
  (
    {
      value,
      defaultValue = 0,
      onValueChange,
      max = 5,
      disabled = false,
      readOnly = false,
      className,
      accessibilityLabel = 'Rating',
      getValueLabel = (rating, total) => `${rating} of ${total} stars`,
      ...props
    },
    ref,
  ) => {
    if (!Number.isInteger(max) || max < 1) throw new RangeError('Rating max must be a positive integer')
    const [internal, setInternal] = React.useState(defaultValue)
    const raw = value ?? internal
    const resolved = Math.max(0, Math.min(max, Number.isFinite(raw) ? raw : 0))
    const colors = useThemeColors()
    function star(index: number) {
      const filled = Math.max(0, Math.min(1, resolved - index))
      return (
        <View pointerEvents="none" aria-hidden style={{ width: 24, height: 24 }}>
          <Star size={24} color={colors.mutedForeground} fill="transparent" aria-hidden />
          {filled > 0 ? (
            <View className="absolute left-0 top-0 overflow-hidden" style={{ width: 24 * filled, height: 24 }}>
              <Star size={24} color={colors.primary} fill={colors.primary} aria-hidden />
            </View>
          ) : null}
        </View>
      )
    }
    return (
      <View
        ref={ref}
        {...props}
        accessibilityRole={readOnly ? 'image' : 'radiogroup'}
        accessibilityLabel={accessibilityLabel}
        // Fabric's numeric accessibility range uses integers; informational fractional ratings use text.
        accessibilityValue={readOnly ? { text: getValueLabel(resolved, max) } : undefined}
        className={cn('flex-row flex-wrap items-center gap-[4px]', disabled && 'opacity-50', className)}
      >
        {Array.from({ length: max }, (_, index) =>
          readOnly ? (
            <View key={index} className="p-[4px]">
              {star(index)}
            </View>
          ) : (
            <Pressable
              key={index}
              accessibilityRole="radio"
              accessibilityLabel={getValueLabel(index + 1, max)}
              accessibilityState={{ checked: resolved === index + 1, disabled }}
              disabled={disabled}
              className="min-h-[48px] min-w-[48px] items-center justify-center rounded-md active:bg-muted"
              onPress={() => {
                const next = index + 1
                if (disabled || next === resolved) return
                if (value === undefined) setInternal(next)
                onValueChange?.(next)
              }}
            >
              {star(index)}
            </Pressable>
          ),
        )}
      </View>
    )
  },
)
Rating.displayName = 'Rating'
components/ui/rating/index.ts
export { Rating } from './Rating'
export type { RatingProps } from './Rating'

Dependencies

npm packages

  • lucide-react-native
Browse controls and interactions