Rating

Lets the user give a score with stars or another icon.

Usage

Code
import { RatingGroup } from '@leverui/components';
Code
<RatingGroup.Root>
  <RatingGroup.Label />
  <RatingGroup.HiddenInput />
  <RatingGroup.Control>
    <RatingGroup.Item>
      <RatingGroup.ItemIndicator />
    </RatingGroup.Item>
  </RatingGroup.Control>
</RatingGroup.Root>

For one ready-made component, see the snippet below.

Shortcuts

RatingControl

RatingGroup.Control renders as many rating items as count says.

Code
<RatingGroup.Control>
  <For each={Array.from({ length: 5 })} keyed={false}>
    {(_, index) => (
      <RatingGroup.Item index={index + 1}>
        <RatingGroup.ItemIndicator />
      </RatingGroup.Item>
    )}
  </For>
</RatingGroup.Control>
Code
<RatingGroup.Control />

Examples

Basic

Show five stars with an initial three-star value.

Sizes

Use the size prop to change the size of the rating component.

Controlled

Use the value and onValueChange prop to control the rating value.

Store

An alternative way to control the rating is to use the RootProvider component and the useRatingGroup store hook.

This way you can access the rating state and methods from outside the component.

ReadOnly

Use the readOnly prop to make the rating component read-only.

Hook Form

Here's an example of how to use rating with form state held in a Solid signal.

Custom Icon

Use the icon prop to pass a custom icon to the rating component. This will override the default star icon.

Label

Render the RatingGroup.Label component to provide a human-readable label for the rating component.

Half Star

Use the allowHalf prop to allow half-star ratings.

Emoji

Compose the rating component with emojis.

😡😠😐😊😍

Colors

Use the colorPalette prop to change the color of the rating

gray

red

green

blue

teal

pink

purple

cyan

orange

yellow

Testimonial

Use the rating component to show testimonials.

Rafe rebuilt our checkout in two weeks. He is careful and easy to work with.

OB

Oskar Brandt

CTO, Fernhill Labs

Closed Component

Code
import { For, Show, omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { RatingGroup, type RatingGroupIcon, type RatingGroupRootProps } from '@leverui/components';

export type RatingProps = RatingGroupRootProps & {
  icon?: RatingGroupIcon | undefined;
  count?: number | undefined;
  label?: JSX.Element;
};

export function Rating(props: RatingProps) {
  const count = () => props.count ?? 5;
  const root = omit(props, 'icon', 'count', 'label');
  return (
    <RatingGroup.Root count={count()} {...root}>
      <Show when={props.label}>
        <RatingGroup.Label>{props.label}</RatingGroup.Label>
      </Show>
      <RatingGroup.HiddenInput />
      <RatingGroup.Control>
        <For each={Array.from({ length: count() })} keyed={false}>
          {(_, index) => (
            <RatingGroup.Item index={index + 1}>
              <RatingGroup.ItemIndicator {...(props.icon ? { icon: props.icon } : {})} />
            </RatingGroup.Item>
          )}
        </For>
      </RatingGroup.Control>
    </RatingGroup.Root>
  );
}
Code
<Rating defaultValue={3} size="sm" />

Guides

Forms

In a form, give the root a name and include HiddenInput. readOnly: the score cannot change but is still sent in FormData. disabled: no score can be picked and the value is left out. required marks the hidden input as required. An untouched rating has the value -1, which the form counts as filled in. Check the score yourself when one is required.

Hover and keyboard

onHoverChange reports the score under the pointer in hoveredValue, and -1 when the pointer leaves. Hovering selects nothing. Arrow keys move the selected score. The label names the group of radio items. The default item names count stars. Change them with translations.ratingValueText.

Custom icons

Pass an icon component to ItemIndicator with icon={MyIcon}. It receives SVG attributes, including data-bg or data-fg, and must spread them onto its SVG. The icon renders twice: a background layer and a foreground layer.

Right-to-left

Half ratings follow the text direction: which half is filled, and how the pointer and arrow keys work. Wrap the group in LocaleProvider locale="ar-EG" so the rating logic and the CSS use the same direction.

Props

Root

PropDefaultTypeDescription
count5numberNumber of rating items.
colorPalettegraystringColor palette, built-in or your own.
sizemd'xs' | 'sm' | 'md' | 'lg'Rating size. Responsive.
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the rating's props.
unstyledfalsebooleanTurn off the Lever UI styles.
allowHalf—booleanAllow half stars.
autoFocus—booleanFocus the rating on first render.
defaultValue—numberInitial rating. Uncontrolled.
disabled—booleanDisable the rating.
form—stringID of the form the input belongs to.
id—stringUnique ID for the state machine.
ids—Element ID mapYour own IDs for the rating elements.
name—stringInput name in the submitted form.
onHoverChange—(details) => voidCalled when the hovered rating changes.
onValueChange—(details) => voidCalled when the rating value changes.
readOnly—booleanMake the rating read-only.
required—booleanMark the rating as required.
translations—IntlTranslationsTranslated text for accessible labels and states.
value—numberControlled rating value.

Item

PropDefaultTypeDescription
index (required)—numberRating value of this item, from 1.
asspanNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the item's props.

Customize with CSS

Classes: .leverui-rating-group__root, __label, __control, __item, __itemIndicator. The root has data-size, a data-size-* attribute for each breakpoint, and attributes for selection, hover, disabled, and focus. The indicator has data-highlighted, data-checked, and data-half.

Per-group variables: --leverui-rating-group-item-font-size, --leverui-rating-group-item-line-height, --leverui-rating-group-background-color, --leverui-rating-group-foreground-color.

Code
.product-rating {
  --leverui-rating-group-foreground-color: #ef4444;
  --leverui-rating-group-background-color: #e5e7eb;
  --leverui-rating-group-item-font-size: 1.75rem;
}

class and style still work. unstyled turns off the default styles but keeps the behavior and the part classes.