Select or display a score with whole stars, half stars, or custom icons.
Default
Review Form
Display, Custom Icons & States
Purpose
A rating control lets people choose a score, such as a star rating, or see a score that’s already been given.
Parts & Composition
Place RatingItem inside Rating to provide the item template. UI SDK repeats it to match the configured maximum. Give the root an accessible name.
API & Props
| Part / Prop | Purpose |
|---|---|
Rating | Root group (value, defaultValue, onValueChange, max, step, deselectable, disabled, readOnly, required, name, form, loop, getValueLabel, controller, debug, size) |
RatingItem | Repeated item (icon default <DvStar />, JSX or state-based ({ fill, checked, highlighted, disabled, readOnly }) => ReactNode children, and SDK render props) |
size | xs (26px), sm (32px), md (36px), lg (40px) — sets --rating-size |
createRatingController() | Programmatic getValue(), setValue(value, details?), and focus() |
Use step={0.5} for half-star selection. For display-only averages, pass readOnly and value.
Common props in detail
| Prop | Purpose |
|---|---|
max / step | Set the scale and allowed increments. For example, a step of 0.5 enables half stars. |
getValueLabel | Provide a spoken label when the bare number is ambiguous. |
readOnly / disabled | Use readOnly to display a rating without editing, or disabled to mark the control unavailable. |
State & forms
name and form submit the numeric score through hidden native radios. required blocks form submission until a score is chosen; form reset restores defaultValue for uncontrolled ratings (for controlled ratings, reset in onReset). Set deselectable so clicking or pressing Space on the checked star clears the score to null.
Interaction states
| State | Behavior |
|---|---|
| Rest | Unfilled icons render in text-muted-foreground/50; filled overlay clips to --rating-fill in text-amber-500 dark:text-amber-400 with a bevel top highlight and drop shadow. |
| Hover / preview | On @media(hover:hover) devices (when not disabled or read-only), pointer movement previews --rating-fill, dims trailing unchecked/unhighlighted icons to opacity-70, scales highlighted items (scale-105), and lifts the hovered item (scale-110, brightness-105) with a 300ms spring transition. |
| Active / press | Pressing an interactive item compresses and depresses the icon (scale-90, translate-y-px, brightness-95), and selecting (data-checked) triggers a 420ms rating-spring scale-in/scale-out pop animation across the checked item and preceding active items (motion-reduce:transition-none respects reduced motion). |
| Focus | Keyboard focus on the inner radio applies focusRing (ring-3 ring-border/50) on RatingItem. |
| Disabled | data-disabled dims the group (opacity-50), blocks focus/input/submission, and sets cursor-not-allowed. |
| Read-only | data-readonly renders as role="img", hides inner radios from AT, and keeps cursor-default. |
| Invalid | aria-invalid="true" tints unfilled icons with text-destructive/50 via group-aria-invalid/rating. |
Theming, Layout & Responsiveness
Uses shadcn semantic tokens (muted-foreground, destructive, border, ring) plus amber-500 / amber-400 for filled stars, compatible with light, dark, and custom themes.
| Attribute / CSS Var | Meaning |
|---|---|
data-slot="rating" / data-slot="rating-item" | Stable slot selectors on root and each item. |
data-size | Reflects xs | sm | md | lg. |
data-filled / data-partial / data-checked / data-highlighted | Set by UI SDK on RatingItem for whole, partial, selected, and previewed stars. |
data-disabled / data-readonly / data-required | Propagated on Rating and RatingItem. |
--rating-fill | Fraction from 0 to 1 driving LTR and RTL clip-path fill. |
Rating uses inline-flex max-w-full flex-wrap items-center gap-0.5 so large max counts wrap cleanly on narrow viewports. Interactive items set touch-manipulation and expand hit targets to at least 44×44px (min-h-11 min-w-11) on @media(pointer:coarse) touch screens, while readOnly ratings stay compact.
Motion & Accessibility
Follows the WAI-ARIA radio group pattern (Tab enters the checked star or first star, Arrow keys step through scores, Space selects or clears when deselectable, Enter submits forms). Name interactive ratings with aria-label or a Field label; name readOnly ratings with getValueLabel={(value, max) => ...} so screen readers announce the score.
Guidance for Coding Agents
Install using the command in the Install tab. Read this Info page before implementing.
Set the scale and permitted step at the root. Give the group a label and provide meaningful value text when a bare number is unclear. Use readOnly to display an average and keep review submission separate.
Put exactly one <RatingItem /> inside <Rating>; use step={0.5} for half stars and readOnly for static averages. Choose controlled value or an initial defaultValue and supply an accessible name.
Start from the closest Preview → Usage example and use this page’s API table for component parts and options. For inherited props, callbacks, keyboard behavior, and rendering options, consult UI SDK Rating.
CLI installation
bunx --bun shadcn@latest add https://ui.devvarena.com/api/r/rating'use client';
import type {
RatingChangeOptions,
RatingController,
RatingItemState,
RatingOptions,
RatingState,
RatingValueChangeReason,
} from '@ui-sdk/core/rating';
import {
createRatingController,
Rating as SDKRating,
RatingItem as SDKRatingItem,
} from '@ui-sdk/react/rating';
import { cva } from 'class-variance-authority';
import { Star } from 'lucide-react';
import type { ComponentProps, ReactNode } from 'react';
import { focusRing, partClass, type StandardControlSize } from '@/lib/devvarena-ui';
const defaultIcon = <Star />;
const ratingSizes = cva(null, {
variants: {
size: {
xs: '[--rating-size:1.625rem]',
sm: '[--rating-size:2rem]',
md: '[--rating-size:2.25rem]',
lg: '[--rating-size:2.5rem]',
},
},
defaultVariants: { size: 'md' },
});
type RatingProps = ComponentProps<typeof SDKRating> & {
size?: StandardControlSize;
};
function Rating({ size = 'md', children = defaultRatingItem, className, ...props }: RatingProps) {
return (
<>
<style href="rating-spring" precedence="components">
{'@keyframes rating-spring{0%{scale:.82}45%{scale:1.24}75%{scale:.94}100%{scale:1}}'}
</style>
<SDKRating
{...props}
data-size={size}
className={partClass(
'group/rating inline-flex max-w-full flex-wrap items-center gap-0.5 data-disabled:opacity-50 [@media(hover:hover)]:[&:not([data-disabled],[data-readonly]):hover_[data-slot=rating-item]:not([data-checked],[data-highlighted],:has(~[data-slot=rating-item][data-checked]))_[data-rating-icon]]:opacity-70 motion-safe:[&:not([data-disabled],[data-readonly])_[data-slot=rating-item]:is([data-checked],:has(~[data-slot=rating-item][data-checked]))_[data-rating-fill]]:animate-[rating-spring_420ms_cubic-bezier(0.22,1.4,0.36,1)]',
ratingSizes({ size }),
className,
)}
>
{children}
</SDKRating>
</>
);
}
type RatingItemProps = ComponentProps<typeof SDKRatingItem> & {
icon?: ReactNode;
};
function RatingItem({ icon = defaultIcon, children, className, ...props }: RatingItemProps) {
return (
<SDKRatingItem
{...props}
className={partClass(
'group/rating-item relative inline-grid size-[var(--rating-size,2.25rem)] shrink-0 cursor-pointer touch-manipulation place-items-center rounded-lg text-muted-foreground/50 group-aria-invalid/rating:text-destructive/50 data-disabled:cursor-not-allowed data-readonly:cursor-default [@media(pointer:coarse)]:not-data-readonly:min-h-11 [@media(pointer:coarse)]:not-data-readonly:min-w-11',
focusRing,
className,
)}
>
{children ?? (
<span
aria-hidden="true"
data-rating-icon=""
className="pointer-events-none relative grid size-[var(--rating-size,2.25rem)] place-items-center motion-safe:transition-[opacity,scale,translate,filter] motion-safe:duration-300 motion-safe:ease-[cubic-bezier(0.2,1.45,0.45,1)] motion-safe:[@media(hover:hover)]:group-data-highlighted/rating-item:group-not-data-disabled/rating-item:group-not-data-readonly/rating-item:scale-105 motion-safe:[@media(hover:hover)]:group-hover/rating-item:group-not-data-disabled/rating-item:group-not-data-readonly/rating-item:scale-110 motion-safe:[@media(hover:hover)]:group-hover/rating-item:group-not-data-disabled/rating-item:group-not-data-readonly/rating-item:brightness-105 motion-safe:group-active/rating-item:group-not-data-disabled/rating-item:group-not-data-readonly/rating-item:translate-y-px motion-safe:group-active/rating-item:group-not-data-disabled/rating-item:group-not-data-readonly/rating-item:scale-90 motion-safe:group-active/rating-item:group-not-data-disabled/rating-item:group-not-data-readonly/rating-item:brightness-95 motion-reduce:transition-none [&>svg]:size-full"
>
{icon}
<span
data-rating-fill=""
className="absolute inset-0 text-[var(--status-warning,var(--color-amber-500))] transition-[clip-path] duration-150 [clip-path:inset(0_calc((1_-_var(--rating-fill,0))*100%)_0_0)] rtl:[clip-path:inset(0_0_0_calc((1_-_var(--rating-fill,0))*100%))] motion-reduce:transition-none dark:text-[var(--status-warning,var(--color-amber-400))] [&>svg]:size-full [&>svg]:fill-current"
>
{icon}
</span>
</span>
)}
</SDKRatingItem>
);
}
const defaultRatingItem = <RatingItem />;
export {
createRatingController,
Rating,
type RatingChangeOptions,
type RatingController,
RatingItem,
type RatingItemProps,
type RatingItemState,
type RatingOptions,
type RatingProps,
type RatingState,
type RatingValueChangeReason,
};
import { cn } from '@/lib/utils';
type ClassValue = string | false | null | undefined;
type PartClassValue<State> = ClassValue | ((state: State) => ClassValue);
/** Composes static class names and UI-SDK state class functions into a single `className` prop. */
export function partClass<State>(...parts: PartClassValue<State>[]) {
if (parts.some((part) => typeof part === 'function')) {
return (state: State) =>
cn(...parts.map((part) => (typeof part === 'function' ? part(state) : part)));
}
return cn(...(parts as ClassValue[]));
}
export const surfacePadding = 'p-1 [--surface-padding:1.5rem] [--concentric-inset:0.25rem]';
export const surfaceRegionInset = 'p-[calc(var(--surface-padding,1rem)-0.5rem)]';
export const surfaceHeaderRadius = 'rounded-t-[max(0px,calc(var(--radius-lg,0.5rem)-0.25rem))]';
export const surfaceFooterRadius = 'rounded-b-[max(0px,calc(var(--radius-lg,0.5rem)-0.25rem))]';
/** Concentric inner radius (`outer - inset`); set `[--concentric-inset:0.125rem]` on `p-0.5` rails (defaults to `0.25rem` for `p-1`). */
export const concentricRadius =
'rounded-[max(0px,calc(var(--radius-lg,0.5rem)-var(--concentric-inset,0.25rem)))]';
export const surfaceBodyPadding =
'py-[max(0px,calc(var(--surface-padding,1rem)-1rem))] has-[>[data-slot=list]:not([data-variant=plain])]:p-0 has-[>[data-slot=list-items-container]]:p-0';
export type StandardControlSize = 'xs' | 'sm' | 'md' | 'lg';
/** Standard control heights (`28px / 32px / 40px / 48px`) for buttons, inputs, selects, tabs, and sliders. */
export const controlHeights = {
xs: 'h-7',
sm: 'h-8',
md: 'h-10',
lg: 'h-12',
} as const satisfies Record<StandardControlSize, string>;
/** Standard minimum control heights (`28px / 32px / 40px / 48px`) for expandable controls and menu items. */
export const controlMinHeights = {
xs: 'min-h-7',
sm: 'min-h-8',
md: 'min-h-10',
lg: 'min-h-12',
} as const satisfies Record<StandardControlSize, string>;
/** Standard control text sizes (`xs / xs / sm / base`) paired with `StandardControlSize`. */
export const controlTextSizes = {
xs: 'text-xs',
sm: 'text-xs',
md: 'text-sm',
lg: 'text-base',
} as const satisfies Record<StandardControlSize, string>;
/** Compact control heights (`1.25rem / 1.3rem / 1.625rem / 1.75rem`), one step smaller than standard, for switches and compact controls. */
export const compactControlHeights = {
xs: 'h-[1.3rem]',
sm: 'h-[1.4rem]',
md: 'h-[1.5rem]',
lg: 'h-[1.6rem]',
} as const satisfies Record<StandardControlSize, string>;
/** Compact minimum control heights (`1rem / 1.25rem / 1.5rem / 1.75rem`) for badges and tooltips. */
export const compactControlMinHeights = {
xs: 'min-h-[1.3rem]',
sm: 'min-h-[1.4rem]',
md: 'min-h-[1.5rem]',
lg: 'min-h-[1.6rem]',
} as const satisfies Record<StandardControlSize, string>;
/** Universal keyboard focus + invalid ring/border styles shared across controls. */
export const focusRing =
'touch-manipulation outline-none focus-visible:border-ring/30 transition-ring focus-visible:ring-3 focus-visible:ring-ring/15 focus-visible:brightness-[0.97] dark:focus-visible:brightness-[1.15] has-[:focus-visible]:border-ring/30 has-[:focus-visible]:ring-3 has-[:focus-visible]:ring-ring/15 has-[:focus-visible]:brightness-[0.97] dark:has-[:focus-visible]:brightness-[1.15] data-active:border-ring/30 data-active:ring-3 data-active:ring-ring/15 data-active:brightness-[0.97] dark:data-active:brightness-[1.15] data-open:border-ring/30 data-open:ring-3 data-open:ring-ring/15 data-open:brightness-[0.97] dark:data-open:brightness-[1.15] data-disabled:cursor-not-allowed data-disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/25 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/25 has-[[data-invalid]]:border-destructive has-[[data-invalid]]:ring-3 has-[[data-invalid]]:ring-destructive/25 group-aria-invalid/control:border-destructive group-aria-invalid/control:ring-3 group-aria-invalid/control:ring-destructive/25 group-data-invalid/control:border-destructive group-data-invalid/control:ring-3 group-data-invalid/control:ring-destructive/25 group-has-[input[aria-invalid=true]]/control:border-destructive group-has-[input[aria-invalid=true]]/control:ring-3 group-has-[input[aria-invalid=true]]/control:ring-destructive/25 hover:brightness-[0.97] dark:hover:brightness-[1.15]';
/** Shared sliding highlight indicator base (pointer-safe, concentric inner radius inside rounded-lg containers, 200ms transition). */
export const slidingHighlight =
'pointer-events-none -z-10 rounded-[max(0px,calc(var(--radius-lg,0.5rem)-var(--concentric-inset,0.25rem)))] bg-primary/10 [clip-path:inset(0px_round_max(0px,calc(var(--radius-lg,0.5rem)-var(--concentric-inset,0.25rem))))] transition-[transform,width,height,opacity] duration-200 ease-out motion-reduce:transition-none';
export const textFieldSurfaces = {
filled: 'border-input bg-card text-card-foreground',
outline: 'border-input bg-transparent text-foreground dark:bg-transparent',
ghost: 'border-transparent bg-muted text-foreground shadow-none',
underline:
'rounded-none border-0 border-b-2 border-input bg-transparent px-0 text-foreground shadow-none focus-visible:border-ring focus-visible:ring-0 aria-invalid:ring-0 data-invalid:ring-0 dark:bg-transparent',
} as const;
export const textFieldUnderlineMotion =
'border-transparent! bg-origin-border bg-bottom bg-no-repeat [--underline-color:var(--primary)] [background-image:linear-gradient(var(--underline-color),var(--underline-color)),linear-gradient(var(--input),var(--input))] [background-size:0%_2px,100%_2px] focus:[background-size:100%_2px,100%_2px] aria-invalid:[--underline-color:var(--destructive)] data-invalid:[--underline-color:var(--destructive)] transition-[border-color,background-color,box-shadow,filter,background-size] duration-200 motion-reduce:transition-none';
/** Generic balanced icon & floating-label layout tokens shared across all text-field sizes. */
export const textFieldLayout = {
icon: 'size-4',
iconStart: 'start-3',
iconEnd: 'end-3',
padStart: 'ps-9',
padEnd: 'pe-9',
underlinePadStart: 'ps-7',
underlinePadEnd: 'pe-7',
floatingStart: 'start-2.5',
floatingWithIcon: 'start-8',
floatingUnderlineIcon: 'start-6',
} as const;
export const textFieldLabelSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
/** Clamps per-grapheme wave delay (ms) so total staggering stays within 300ms. */
export function clampWaveDelay(waveDelay: number, count: number): number {
return Math.max(
0,
Math.min(Number.isFinite(waveDelay) ? waveDelay : 24, 80, 300 / Math.max(1, count - 1)),
);
}
/** Shared scale/color transition for floating labels inside a `group/field` wrapper. */
export const textFieldLabelMotion =
'inline-block origin-left backdrop-blur-xs transition-[scale,color] duration-200 ease-out group-has-[input:focus]/field:scale-85 group-has-[input:not(:placeholder-shown)]/field:scale-85 rtl:origin-right motion-reduce:transition-none';
/** Resolves start/end padding for a text field when a leading or trailing icon is present. */
export function getTextFieldIconPadding(iconPosition: 'start' | 'end', underline: boolean): string {
if (iconPosition === 'start') {
return underline ? textFieldLayout.underlinePadStart : textFieldLayout.padStart;
}
return underline ? textFieldLayout.underlinePadEnd : textFieldLayout.padEnd;
}
/** Shared per-grapheme ripple transition for wave labels inside a `group/field` wrapper. */
export const textFieldWaveMotion =
'inline-block origin-left backdrop-blur-xs transition-[translate,scale,color] duration-200 ease-out group-has-[input:focus]/field:-translate-y-5 group-has-[input:focus]/field:scale-85 group-has-[input:focus]/field:text-primary group-has-[input:not(:placeholder-shown)]/field:-translate-y-5 group-has-[input:not(:placeholder-shown)]/field:scale-85 group-has-[input[aria-invalid=true]]/field:text-destructive rtl:origin-right motion-reduce:transition-none';