Rating Group
Star rating input with keyboard-accessible items across four sizes.
123456789101112import { RatingGroup } from "#components/ui/ui-sdk/rating-group.tsx";import { indigoPalette } from "@stylexblocks/ui/themes/palettes.ts";
export function Example() { // UI SDK's Rating: a radio per star, arrow keys, pointer preview return ( <RatingGroup count={5} defaultValue={3} size="md" palette={indigoPalette} aria-label="Rating" /> );}
// controlled: <RatingGroup value={value} onValueChange={setValue} />// half stars: <RatingGroup step={0.5} />Sizes
Install
1npx stylexblocks add rating-groupSource
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198import * as stylex from "@stylexjs/stylex";import { mergeProps } from "@ui-sdk/react/primitive";import { Rating, RatingItem } from "@ui-sdk/react/rating";import { colorPalette } from "@stylexblocks/ui/color-palette.stylex.ts";import { semanticColors } from "@stylexblocks/ui/semantic-tokens/colors.stylex.ts";import type { StyleOverrideProps } from "#lib/recipe-props.ts";import { ratingGroupItemIndicatorIconVars } from '@stylexblocks/ui/slot-recipes/rating-group.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
const ratingGroupRoot = stylex.create({ base: { display: 'inline-flex', },})
const ratingGroupControl = stylex.create({ base: { display: 'inline-flex', alignItems: 'center', },})
const ratingGroupItem = stylex.create({ base: { display: 'inline-flex', alignItems: 'center', justifyContent: 'center', userSelect: 'none', },})
const ratingGroupItemSizes = stylex.create({ xs: { fontSize: fontSizes.sm, lineHeight: lineHeights.sm, }, sm: { fontSize: fontSizes.md, lineHeight: lineHeights.md, }, md: { fontSize: fontSizes.xl, lineHeight: lineHeights.xl, }, lg: { fontSize: fontSizes._2xl, lineHeight: lineHeights._2xl, },})
const ratingGroupItemIndicator = stylex.create({ base: { display: 'inline-flex', alignItems: 'center', justifyContent: 'center', width: '1em', height: '1em', position: 'relative', [ratingGroupItemIndicatorIconVars.stroke]: 'currentColor', [ratingGroupItemIndicatorIconVars.width]: '100%', [ratingGroupItemIndicatorIconVars.height]: '100%', [ratingGroupItemIndicatorIconVars.display]: 'inline-block', [ratingGroupItemIndicatorIconVars.flexShrink]: 0, [ratingGroupItemIndicatorIconVars.position]: 'absolute', [ratingGroupItemIndicatorIconVars.left]: 0, [ratingGroupItemIndicatorIconVars.top]: 0, },})
const ratingGroupItemIndicatorIcon = stylex.create({ base: { stroke: ratingGroupItemIndicatorIconVars.stroke, width: ratingGroupItemIndicatorIconVars.width, height: ratingGroupItemIndicatorIconVars.height, display: ratingGroupItemIndicatorIconVars.display, flexShrink: ratingGroupItemIndicatorIconVars.flexShrink, position: ratingGroupItemIndicatorIconVars.position, left: ratingGroupItemIndicatorIconVars.left, top: ratingGroupItemIndicatorIconVars.top, },})
export type RatingGroupSize = keyof typeof ratingGroupItemSizes
// UI SDK's Rating renders its children once per star and stamps each star// with `data-filled` for the score it shows: the value, or the score a// pointer previews. The star's colour and its icon's fill key off that, so// hovering previews the score like the Ark UI version. Each star's radio is// a visually hidden input inside it, so the focus ring keys off// `:has(:focus-visible)`.const ratingLocal = stylex.create({ item: { cursor: { default: "pointer", ":is([data-disabled], [data-readonly])": "default", }, opacity: { default: null, ":is([data-disabled])": 0.5, }, outline: "none", color: { default: semanticColors.borderEmphasized, ":is([data-filled])": colorPalette.solid, }, "--rating-star-fill": { default: "none", ":is([data-filled])": "currentColor", }, borderRadius: "2px", boxShadow: { default: null, ":has(:focus-visible)": `0 0 0 2px ${colorPalette.focusRing}`, }, }, icon: { fill: "var(--rating-star-fill)" },});
function StarIcon() { return ( <svg viewBox="0 0 24 24" strokeWidth="1.5" aria-hidden="true" {...stylex.props(ratingGroupItemIndicatorIcon.base, ratingLocal.icon)} > <path d="M12 3.2 14.7 8.8l6.1.9-4.4 4.3 1 6.1-5.4-2.9-5.4 2.9 1-6.1L3.2 9.7l6.1-.9L12 3.2Z" /> </svg> );}
export interface RatingGroupProps extends Omit< React.ComponentProps<typeof Rating>, | "style" | "className" | "children" | "max" | "value" | "defaultValue" | "onValueChange" >, StyleOverrideProps { size?: RatingGroupSize; /** Number of rating items. */ count?: number; value?: number; defaultValue?: number; onValueChange?: (value: number) => void;}
/** * Rating Group — UI SDK's Rating wearing the rating-group slot recipe. * One element is both the recipe's root and control (UI SDK repeats its * children once per star, so nothing can sit between them). Behavior is * UI SDK's: a radio per star, roving focus with arrow keys, pointer * preview, `step={0.5}` half stars, `readOnly`, `disabled`, and a hidden * input under `name`. Composition via `render`. */export function RatingGroup({ size = "md", count = 5, value, defaultValue = 0, onValueChange, palette, style, ...props}: RatingGroupProps) { return ( <Rating {...mergeProps( stylex.props(palette, ratingGroupRoot.base, ratingGroupControl.base, style), props, )} max={count} value={value} defaultValue={defaultValue || null} onValueChange={(next) => onValueChange?.(next ?? 0)} > <RatingItem {...stylex.props( ratingGroupItem.base, ratingGroupItemSizes[size], ratingLocal.item, )} > <span {...stylex.props(ratingGroupItemIndicator.base)}> <StarIcon /> </span> </RatingItem> </Rating> );}
Examples
Installable blocks built with Rating Group — real use cases, delivered as source you own.
Interactive course overview page featuring breadcrumb navigation, tabbed Syllabus, Outcomes, Instructor, and Reviews views, a checkable 4-module curriculum accordion synced to live progress bars, and a sticky enrollment rail with a photographic course cover, self-paced vs. cohort planning, and toast feedback.
Two-column storefront product detail layout pairing a multi-angle interactive photo gallery with a sticky buy box, live anodized finish swatches, color-temperature edition controls, quantity stepper, collapsible specifications accordion, and filterable customer reviews.
Interactive design-system and token workbench featuring scoped accent, density, and corner-radius live preview controls over a responsive 3-column bento of cohesive UI SDK cards.
