本文へ移動
日本語

仕組み

Each star has two stacked layers, an opaque grey and the lit colour; lighting only changes the top layer. Stars lit only by the hover preview use a lighter tint, and the star that carries the value gets a soft radial glow. Hover preview ignores touch. After a click, the lit stars remount with a new key so a CSS keyframe pop replays, staggered by index. Semantics are a standard radio group with one Tab stop. Each star sits in a 44×44 cell pulled back with negative margins, so the layout size is just the star.

向いている場面

  • Rating an order, a trip or a purchase after it finishes.
  • Showing an average score in a card or list, using readOnly with a fractional value.
  • Short feedback forms where one tap is enough.

向かない場面

  • Fine-grained scores such as 0 to 100: use a slider or a number field.
  • Yes or no feedback: use two buttons.
  • Comparing many items by rating: use a sortable number column.

使い方

import { useState } from 'react'
import { RatingStars } from './RatingStars.jsx'

export function OrderReview() {
  const [score, setScore] = useState(0)
  return (
    <RatingStars
      aria-label="How was your order?"
      value={score}
      onChange={setScore}
      labels={['Poor', 'Fair', 'Okay', 'Good', 'Excellent']}
      placeholder="Tap a star to rate"
    />
  )
}

// Read-only display (fractions supported): <RatingStars readOnly value={4.5} size={14} />

プロパティ

プロパティ型既定値説明
valuenumber—Controlled: current score, 0 means not rated yet. Can be fractional when read-only.
defaultValuenumber0Uncontrolled: initial score.
onChange(value: number) => void—Called when the score changes (click or keyboard); 0 when cleared.
onHoverChange(value: number) => void—Called when the hover preview changes: the star pointed at, or 0 when the pointer leaves.
countnumber5Number of stars.
sizenumber32Star side length in px. When clickable, each star's click area is at least 44×44.
readOnlybooleanfalseRead-only: only shows the score (fractions allowed), is read as one image by screen readers, and tightens the star spacing.
disabledbooleanfalseDisabled: dimmed, not clickable, not focusable.
allowClearbooleantrueClicking the current score again clears it; Backspace or Delete clears from the keyboard.
labelsReactNode[]—Text for each score, labels[0] for 1 star. The text area reserves the width of the longest one.
placeholderReactNode—Hint shown in the text area before anything is rated, such as “Tap a star to rate” (in text-3).
labelPlacement'end' | 'bottom''end'Where the text goes: to the right of the stars, or centred below.
precisionnumber0.5A fractional value is drawn to the nearest step: halves by default (4.3 draws 4½ stars, the label still says 4.3); 0 or 1 draws it exactly. A half is cut through the star's top point, so it reads as a clean half at any size.
starLabel(value: number, count: number) => string(value, count) => `${value} of ${count} stars`Screen-reader name of each star. String entries in labels are appended after it.
namestring—Form field name: renders a hidden input that is submitted with the form.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root element, such as className, style and aria-label / aria-labelledby (the group's name). Without a name the group is called “Rating”, and read-only is “Rating 4.5 of 5 stars”; pass aria-label to localize.

アクセシビリティ

  • The root is a radiogroup and each star a radio named “3 of 5 stars”, with the label text appended.
  • The group has one Tab stop; arrow keys change the score and focus follows, Home and End jump, Backspace and Delete clear.
  • Read-only is a single image named, for example, “Rating 4.5 of 5 stars”, and is not focusable.
  • Every star click area is at least 44×44, and the focus ring hugs the star.
  • Reduced motion drops the pop animation and keeps the colour change.

モーション

Lighting up grows from 0.6× with a 240ms spring, going out is a quick 100ms ease-in. Hover lifts a star to 1.15× with a spring; press dips it to 0.88×. After a click each lit star presses to 0.72× and springs back, 40ms apart by index, and the glow behind the chosen star swells in over 360ms.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

RatingStars.jsxrating-stars.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。