Skip to content

Data Input

Rating

Star rating input and display, built on a native radio group.

import { Rating } from '@var-ui/react';
Quality

Rating

Star (or custom icon) rating. An input rating is a real radio group: max native <input type="radio"> elements share a name, the inputs are visually hidden and their labels draw the icons. Keyboard, form submission, required and disabled are the browser’s own. Use readOnly to display a score, including fractions such as 3.5.

Examples

Default

Seed the rating with defaultValue (0 means no rating). Hovering previews the rating up to the hovered star with CSS only.

Quality
Demo.tsxTSX
import { Rating } from '@var-ui/react';

export default function Preview() {
  return <Rating label="Quality" defaultValue={3} />;
}

Controlled

Drive the rating with value and onValueChange. clearable lets a click on the current value reset it to 0.

Your rating
4 of 5
Demo.tsxTSX
import { Rating } from '@var-ui/react';
import { useState } from 'react';

export default function Preview() {
  const [value, setValue] = useState(4);

  return (
    <div style={{ display: 'grid', gap: '0.5rem' }}>
      <Rating label="Your rating" value={value} onValueChange={setValue} clearable />
      <span>{value === 0 ? 'No rating' : `${value} of 5`}</span>
    </div>
  );
}

Read-only with a fractional value

readOnly renders a non-interactive role="img" with an accessible name such as “3.5 out of 5”. Fractions are only supported here: the next star is clipped at the fraction through the recipe’s fill custom property.

Average rating
Demo.tsxTSX
import { Rating } from '@var-ui/react';

export default function Preview() {
  return (
    <div style={{ display: 'grid', gap: '0.75rem' }}>
      <Rating readOnly value={3.5} label="Average rating" />
      <Rating readOnly value={4.2} tone="warning" size="sm" aria-label="Reviews" />
    </div>
  );
}

Custom icon

icon and emptyIcon take any node, or a semantic icon name from the IconProvider registry (the default is star).

Favourite
Demo.tsxTSX
import { Rating } from '@var-ui/react';

const heart = (
  <svg viewBox="0 0 24 24" stroke="currentColor" strokeWidth="2" aria-hidden="true">
    <path d="M12 21s-8-5.2-8-11a4.5 4.5 0 0 1 8-2.8A4.5 4.5 0 0 1 20 10c0 5.8-8 11-8 11z" />
  </svg>
);

export default function Preview() {
  return <Rating label="Favourite" defaultValue={2} max={5} icon={heart} tone="danger" />;
}

Sizes

size is the shared control size (sm, md, lg) and falls back to the surrounding control size. tone colors the filled icons.

Demo.tsxTSX
import { Rating } from '@var-ui/react';

export default function Preview() {
  return (
    <div style={{ display: 'grid', gap: '1rem' }}>
      <Rating size="sm" defaultValue={2} aria-label="Small" />
      <Rating size="md" defaultValue={3} aria-label="Medium" />
      <Rating size="lg" defaultValue={4} aria-label="Large" />
    </div>
  );
}

In a form

With a name the checked number is posted like any radio group. required blocks submission until a star is picked. Without a name the rating still works as a group but posts nothing.

Quality

Required before you can submit.

Demo.tsxTSX
import { Button, Rating } from '@var-ui/react';
import { useState } from 'react';

export default function Preview() {
  const [posted, setPosted] = useState<string>('');

  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        setPosted(String(new FormData(event.currentTarget).get('quality') ?? ''));
      }}
      style={{ display: 'grid', gap: '0.75rem' }}
    >
      <Rating
        name="quality"
        label="Quality"
        description="Required before you can submit."
        required
      />
      <Button type="submit">Submit</Button>
      {posted ? <span>Posted quality={posted}</span> : null}
    </form>
  );
}

Props

Accessibility

The input rating is a role="radiogroup" of native radios, so assistive technology announces each star as “n of max” (override with getItemLabel). Name the group with label or aria-label. description and errorMessage are wired into aria-describedby; errorMessage implies aria-invalid. The focus ring appears on the star of the focused radio.

Filled and empty icons differ by fill, not only color, so they stay distinguishable in forced-colors mode. Transitions follow the reduced-motion tokens.

The read-only display is not focusable: it is a single image named by getValueLabel (default “3 out of 5”) and keeps its visible label. With a name it still posts its value through a hidden input. Astro has a Rating for this read-only display (value, max, size, tone, label).

Keyboard

KeyBehavior
Tab / Shift+TabEnters and leaves the group once, landing on the checked star (or first).
ArrowRight / ArrowDownSelects the next star (the one visually after it; mirrored in RTL).
ArrowLeft / ArrowUpSelects the previous star.
SpaceSelects the focused star; with clearable, clears it when already chosen.