Introducing PrimeReact v11-alpha 🎉Discover Now

Rating

An unstyled, accessible rating component with compound composition for star-based selection input.

Build fully custom rating controls with complete control over layout and styling.

basic-demo

Pre-styled Versions

Choose a pre-styled library to use Rating with ready-made designs.
For hook-based usage without components, see the Headless API.

Features#

  • Compound component API with four sub-components: Root, Option, On, Off
  • Half-star support with allowHalf prop
  • Horizontal and vertical orientation
  • Controlled and uncontrolled value management

Usage#

import { Rating } from 'primereact/rating';
<Rating.Root defaultValue={3}>
    {Array(5)
        .fill(null)
        .map((_, i) => (
            <Rating.Option key={i} index={i}>
                <Rating.On>
                    <StarFill />
                </Rating.On>
                <Rating.Off>
                    <Star />
                </Rating.Off>
            </Rating.Option>
        ))}
</Rating.Root>

Behavior#

Polymorphic Rendering#

Use as on any sub-component to change the rendered HTML element.

<Rating.Root as="div">
    <Rating.Option as="div" index={0}>
        <Rating.On as="div" />
        <Rating.Off as="div" />
    </Rating.Option>
</Rating.Root>

Default element: span for all sub-components.

Render Function Children#

Rating.On and Rating.Off accept a render function as children, providing access to the component instance.

<Rating.On>{(instance) => <span>{instance.ratingOption?.state.highlighted ? 'filled' : ''}</span>}</Rating.On>

API#

RatingRoot#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RatingRootInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: RatingRootInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRatingRootInstance—
The instance to pass to the component.
ptSafeRecord<RatingRootPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: RatingRootInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
valuenumber—
Value of the rating.
defaultValuenumber0
The default value of the rating.
allowHalfbooleantrue
Whether to allow half stars.
namestring—
Name used for the hidden radio inputs. Auto-generated if not provided.
orientation"horizontal" | "vertical"'horizontal'
The orientation of the rating.
disabledbooleanfalse
When present, it specifies that the element should be disabled.
readOnlybooleanfalse
When present, it specifies that component is read-only.
invalidbooleanfalse
When present, it specifies that the component should have invalid state style.
onValueChange(event: useRatingValueChangeEvent) => void—
Callback fired when the value changes.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"rating"
data-part"root"
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-readonlyPresent when read-only
data-invalidPresent when invalid

RatingOption#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RatingOptionInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: RatingOptionInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRatingOptionInstance—
The instance to pass to the component.
ptSafeRecord<RatingOptionPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: RatingOptionInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
indexnumber—
The zero-based index of this option within the rating.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"rating"
data-part"option"
data-orientation"horizontal" or "vertical"
data-indexZero-based option index
data-highlightedPresent when filled
data-halfPresent when half-filled
data-checkedPresent when active
data-disabledPresent when disabled
data-readonlyPresent when read-only

RatingOn#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RatingOnInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: RatingOnInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRatingOnInstance—
The instance to pass to the component.
ptSafeRecord<RatingOnPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: RatingOnInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"rating"
data-part"option-on"
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-readonlyPresent when read-only

RatingOff#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RatingOffInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: RatingOffInstance) => string)—
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>—
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRatingOffInstance—
The instance to pass to the component.
ptSafeRecord<RatingOffPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: RatingOffInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any—
pt-{optionName}-*-—
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"rating"
data-part"option-off"
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-readonlyPresent when read-only

Accessibility#

Screen Reader#

Rating.Option renders hidden native <input type="radio"> elements that form a radio group. Screen readers announce each option as a radio button with its value. When allowHalf is enabled, both half and full value radios are rendered per option.

Keyboard Support#

KeyFunction
tabMoves focus to the star representing the value, if there is none then first star receives the focus.
left arrow up arrowMoves focus to the previous star.
right arrow down arrowMoves focus to the next star.
spaceIf the focused star does not represent the value, changes the value to the star value.