Rate
Quick evaluation and display of ratings with whole and half values.
When to use
Rate is suitable for quick assessment and display of an already assigned rating. Several familiar symbols allow you to select a value without entering a number. Label what is being assessed and explain the meaning of the extreme values if it is not obvious.
For a precise number or other numeric scale, use InputNumber or Slider. For an existing rating that cannot be changed, Rate is disabled; reading example retains the display of half stars.
CSS prefixes Rate show global ConfigProvider.prefixCls and local prefixCls without resetting state. Root, stars and character context use the allowed prefix. Tooltip separately inherits the common prefix. Score and focus are preserved when renamed.
Evaluation value
Rating - a number from zero to count, by default there are five characters. The managed value is updated via onChange; bind:value is missing from the current Rate. Without value, the component stores the selection internally and starts with defaultValue, defaulting to 0.
| Problem | Setting | Example |
|---|---|---|
| Store selected rating | value, onChange |
Basic score |
| Allow halves | allowHalf |
Half stars |
| Disable cleaning by pressing again | allowClear=false |
Clearing the score |
| Only show rating | disabled |
Read only |
Normally, selecting the same star again clears the rating to 0. The allowClear=false property disables exactly this action; it does not replace the application's validation of the value. If a strictly positive evaluation is required, specify an appropriate model rule.
Half selection changes the precision of the interaction, and count changes the number of characters. Don't mix the number of stars with the size: size selects small/medium/large and can be inherited from general settings. Example sizes stores independent local estimates.
Selection and guidance
Hovering shows a preliminary estimate without recording it. The onHoverChange handler receives a preliminary number and undefined when the pointer leaves; onChange gets the confirmed choice. Do not send the hover value as a custom rating. Example Events shows both states side by side.
Left and right arrows change the rating; with allowHalf the step is 0.5. In RTL the direction is reversed. keyboard=false disables this arrow processing, preserving the pointer selection and callback onKeyDown. disabled completely prohibits modification.
The container has the role of radiogroup, stars are radios with numeric accessible names. Give the group an aria-label and link the rating description using aria-describedby. tooltips can contain strings or Tooltip settings for each star; The explanation inside the tooltip does not replace the name of the entire field. Sample signatures associates the selected number with the text next to it.
The focus/blur and native element methods are available after mount; example methods shows software focus. When the state changes externally, update the value, not the defaultValue of the already created local control.
Symbols and design
The character property accepts text, a number, or a Svelte snippet with RateCharacterInfo. index starts at 0, and value reflects the currently displayed score, including hover. Symbols by index replace the React render function with a regular snippet.
The characterRender property gets the star's original snippet and the same context. To add a wrapper, render the original snippet: it retains the standard button, events and accessibility. It is more convenient to perform a simple image replacement using character, as in example of symbols.
There are no named classNames/styles. For the root, class, rootClassName and style are available, general class/style are set via ConfigProvider.rate. DOM section describes the actual markup. Component design tokens theme.components.Rate apply to stars and text; token directory describes the available settings. Local style and ConfigProvider.rate specify the design.
Examples
Basic rating
Score from 0 to 5. value is stored in the Svelte state, onChange accepts the new selection.
<script lang="ts">
import { Rate } from '@woodger/antd';
let value = $state(0);
</script>
<style>
.example {
display: grid;
gap: 16px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<Rate {value} onChange={next => { value = next; }} aria-label={("Base Rate")} />
<output aria-label={("Basic rating")}>{value}{(" out of 5")}</output>
</div>
Dimensions
large, medium and small. Without local size, the component inherits the size from the ConfigProvider.
<script lang="ts">
import { Rate } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<Rate size="large" defaultValue={3} aria-label={("Size Rate large")} />
<Rate size="medium" defaultValue={3} aria-label={("Size Rate medium")} />
<Rate size="small" defaultValue={3} aria-label={("Size Rate small")} />
</div>
Half stars
allowHalf allows half rating. The left side of the star selects half, the arrows change the rating in increments of 0.5.
<script lang="ts">
import { Rate } from '@woodger/antd';
let value = $state(2.5);
</script>
<style>
.example {
display: grid;
gap: 16px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<Rate allowHalf {value} onChange={next => { value = next; }} aria-label={("Half Rate")} />
<output aria-label={("Half mark value")}>{value}</output>
</div>
- Very bad Poor Normal Good Excellent
Labels and tips
tooltips sets a tooltip for each star. The adjacent explanation reflects the selected value and the unrated status.
<script lang="ts">
import { Rate } from '@woodger/antd';
let value = $state(3);
const descriptions = ["Very bad", "Poor", "Normal", "Good", "Excellent"];
</script>
<style>
.example {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<Rate {value} tooltips={(descriptions)} onChange={next => { value = next; }}
aria-label={("Rating with explanation")} />
<output aria-label={("Explanation of rating")}>{(value ? descriptions[value - 1] : "No rating selected")}</output>
</div>
Read only
disabled shows a full or half rating already given and blocks interaction.
<script lang="ts">
import { Rate } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<Rate disabled value={2} aria-label={("Ready whole assessment")} />
<Rate disabled allowHalf value={2.5} aria-label={("Ready half grade")} />
</div>
allowClear=true
allowClear=false
Clear rating
Clicking on the selected star again resets the rating to 0. allowClear=false saves it.
<script lang="ts">
import { Rate } from '@woodger/antd';
let clearable = $state(3);
let required = $state(3);
</script>
<style>
.example {
display: grid;
gap: 20px;
}
p {
margin: 0 0 12px;
color: var(--ant-color-text-secondary);
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<div><p>allowClear=true</p><Rate value={clearable} onChange={next => { clearable = next; }}
aria-label={("Evaluation with cleaning")} /></div>
<div><p>allowClear=false</p><Rate
value={required}
allowClear={false}
onChange={next => { required = next; }}
aria-label={("Evaluation without cleaning")}
/></div>
<output aria-label={("Cleaning results Rate")}>{clearable}; {required}</output>
</div>
Other symbols
character replaces the star with a heart, letter or hieroglyph. Color and size can be set in the usual style.
<script lang="ts">
import { Rate } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 24px;
}
</style>
<div class="example">
<Rate character="♥" allowHalf defaultValue={2.5} style="color:var(--ant-color-error)"
aria-label={("Rating with hearts")} />
<Rate character="A" allowHalf defaultValue={2.5} style="font-size:36px"
aria-label={("Letter rating")} />
<Rate
character="好"
allowHalf
defaultValue={2.5}
aria-label={("Rating in hieroglyphs")}
/>
</div>
Custom symbols by index
Svelte snippet receives RateCharacterInfo. index specifies a number or facial expression for each of the five positions.
<script lang="ts">
import { Rate, type RateCharacterInfo } from '@woodger/antd';
let numbers = $state(2);
let faces = $state(3);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 24px;
}
output { color: var(--ant-color-text-secondary); }
</style>
{#snippet number(info: RateCharacterInfo)}{(info.index + 1)}{/snippet}
{#snippet face(info: RateCharacterInfo)}
<svg
viewBox="0 0 24 24"
width="1em"
height="1em"
fill="none"
stroke="currentColor"
stroke-width="1.7"
>
<circle cx="12" cy="12" r="9" />
<path d="M8 9h1m6 0h1" stroke-linecap="round" />
<path d={info.index < 2 ? 'M7.5 17q4.5-6 9 0' : info.index === 2
? 'M7.5 15h9'
: 'M7.5 14q4.5 6 9 0'} />
</svg>
{/snippet}
<div class="example">
<Rate value={numbers} character={number} onChange={next => { numbers = next; }}
aria-label={("Numbers instead of stars")} />
<Rate value={faces} character={face} onChange={next => { faces = next; }}
aria-label={("Mood instead of stars")} />
<output
aria-label={("Custom character values")}
>{numbers}; {faces}</output>
</div>
Pointing and keyboard
onHoverChange reports a preliminary estimate and undefined when the pointer leaves. keyboard controls arrow processing; onKeyDown receives the event.
<script lang="ts">
import { Checkbox, Rate } from '@woodger/antd';
let value = $state(2);
let hovered = $state<number>();
let keyboard = $state(true);
let key = $state('—');
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<Checkbox bind:checked={keyboard}>{("Keyboard Rate")}</Checkbox>
<Rate {value} {keyboard} allowHalf onChange={next => { value = next; }}
onHoverChange={next => { hovered = next; }} onKeyDown={event => { key = event.key; }}
aria-label={("Events Rate")} />
<output
aria-label={("Event status Rate")}
>{("Rating: ")}{value}{("; guidance: ")}{(hovered
?? '—')}{("; key: ")}{key}</output>
</div>
- Very bad Poor Normal Good Excellent
Rating: out of 5
Applied assessment
Review example: half choice with hints, result and separate read-only score. The model is reused.
<script lang="ts">
import { Rate } from '@woodger/antd';
let value = $state(2.5);
const tooltips = ["Very bad", "Poor", "Normal", "Good", "Excellent"];
</script>
<Rate
aria-label={("Rating")}
{value}
allowHalf
tooltips={(tooltips)}
onChange={next => { value = next; }}
/>
<p>{("Rating: ")}<output
aria-label={("Selected rating")}
>{value}</output>{(" out of 5")}</p>
<Rate aria-label={("Read-only rating")} value={4} disabled />
Root, stars and character context use the allowed prefix. Tooltip separately inherits the common prefix. Score and focus are preserved when renamed.
CSS prefixes Rate
Common namespace ConfigProvider and local prefixCls; the state of the component is preserved when the name is changed.
<script lang="ts">
import { Checkbox, ConfigProvider, Rate } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-rate' : '');
let value = $state(3);
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("General prefix Rate suite")}</Checkbox>
<Checkbox bind:checked={local}>{("Local prefix Rate")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'rate-theme' } }}>
<div class="prefix-stage">
<Rate prefixCls={localPrefix} {value} onChange={next => value = next}
aria-label={("Rate with prefix")} allowHalf />
<output
aria-label={("Rate with prefix")}
>{value}{(" out of 5")}</output>
</div>
</ConfigProvider>
<output aria-label={("CSS prefix Rate")}>{("General: ")}{prefix}{("; local: ")}{(localPrefix
|| ("from the provider"))}</output>
<p>
{("Root, stars and character context use the allowed prefix. Tooltip separately inherits the common prefix. Score and focus are preserved when renamed.")}
</p>
</div>
API
The main parameters are checked against the public RateProps of the package @woodger/antd. The example themes are mapped to the live Rate page on ant.design.
| Property | Description | Type | Default |
|---|---|---|---|
prefixCls | Full CSS prefix of the component and its parts. A non-empty local prop takes precedence; empty string and undefined inherit ConfigProvider. The namespace of the topic is set separately. | string | ConfigProvider.prefixCls + "-rate" |
value | Guided assessment. Accept the new number via onChange; bind:value is not supported. | number | undefined |
defaultValue | Initial local estimate in the absence of value. | number | 0 |
count | Number of characters and upper limit of keyboard selection. | number | 5 |
allowHalf | Select half a character and keyboard step 0.5 instead of 1. | boolean | false |
allowClear | Clicking the selected item again clears the score to 0. | boolean | true |
disabled | Read-only mode. Inherits accessibility from Form and ConfigProvider. | boolean | From context |
keyboard | Left and right arrows change the rating; RTL reverses the direction. | boolean | true |
size | small, medium and large. | ComponentSize | From ConfigProvider |
character | One symbol for all positions or snippet with index, count, value, allowHalf, disabled and focused. | string | number | Snippet<[RateCharacterInfo]> | Star |
characterRender | Extended wrapper around the original star snippet. Takes precedence over automatic tooltips wrapper. | Snippet<[Snippet, RateCharacterInfo]> | — |
tooltips | Symbol index hints. Objects allow you to set Tooltip parameters. | readonly (string | Omit<TooltipProps, 'children'>)[] | — |
onChange | Receives a new rating when selected, cleared or keyed. | (value: number) => void | — |
onHoverChange | Preliminary assessment when hovering. Pointer departure and selection completion pass undefined. | (value: number | undefined) => void | — |
onKeyDown | Keyboard event, including when keyboard=false. | (event: KeyboardEvent) => void | — |
onFocus | Reports receiving focus. onBlur reports loss of focus; native onfocusin and onfocusout are also available. | () => void | — |
autoFocus | Focuses the container after mount if the component is available. | boolean | false |
tabIndex | Tab index of the container. When disabled, the container and buttons are excluded from the Tab order. | number | 0 |
nativeElement | bind:nativeElement returns the root ul after mount. | HTMLUListElement | — |
ConfigProvider.rate
Supported class and style. The usual class and style components complement the provider settings; local style comes after it.
<script lang="ts">
import { ConfigProvider, Rate } from '@woodger/antd';
</script>
<ConfigProvider rate={{ style: `
color: #d4380d;
font-size: 24px;
` }}>
<Rate defaultValue={3} aria-label={("Registration of Rate from the provider")} />
</ConfigProvider>
Methods
Via bind:this available focus() and blur(). They refer to the Rate container and have no effect when disabled. bind:nativeElement returns the root ul.
<script lang="ts">
import { Button, Rate } from '@woodger/antd';
let control = $state<ReturnType<typeof Rate>>();
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
</style>
<div class="example">
<Rate bind:this={control} defaultValue={2} aria-label={("Rate Methods")} />
<div class="actions">
<Button onclick={() => control?.focus()}>{("Focus Rate")}</Button>
<Button onclick={() => control?.blur()}>{("Remove focus Rate")}</Button>
</div>
</div>
DOM and accessibility
Container — ul with the radiogroup role, each star is a native button with the radio role and a numeric accessible name. Give the group a clear aria-label; Explanation nearby link via aria-describedby.
Rate does not provide classNames and styles with named semantic slots. class, rootClassName and style are available for the root, and character for the symbol. characterRender gets the star's original snippet and RateCharacterInfo for the extended wrapper.
Left and right arrows change the number, with allowHalf the step is 0.5. In RTL the direction is reversed. keyboard=false disables arrow processing, preserving pointer selection and onKeyDown events.
Registration tokens
The six component tokens correspond to the Rate table on ant.design and are listed in ComponentTokenMap['Rate']. They are applied through theme.components.Rate. Local style and the ConfigProvider.rate shown above clarify the design.
| Token | Purpose |
|---|---|
starBg | Color of the empty symbol. |
starColor | Color of the filled symbol. |
starHoverScale | CSS transform of the symbol on hover. |
starSize | Average symbol size. |
starSizeLG | Large symbol size. |
starSizeSM | Small symbol size. |
Common tokens of the current topic
The values are read through theme.useToken() and react to the settings in the header.
| Token | Purpose | Current value |
|---|---|---|
colorText | Primary text color. | rgba(0,0,0,0.88) |
fontFamily | Font family. | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | Basic text size. | 14 |
lineHeight | Base line height. | 1.5714285714285714 |
marginXS | Indentation between characters. | 8 |
motionDurationMid | Transition duration. | 0.2s |
FAQ
Why is there no bind:value here?
In the current Rate value is a managed property. Assign the number from onChange to your $state, as in basic example. Without value, the component stores the selection internally and starts with defaultValue.
How to prohibit cleaning?
Set allowClear=false so that reselecting a star does not clear the rating. Use application rules to limit the allowed range; for display without interaction - disabled.
Why does the picture change when you hover over it, but not the number next to it?
Hovering shows a preliminary estimate. The onChange handler captures the selection, onHoverChange reports the preliminary value, and undefined when the pointer leaves. Example Events shows them separately.
How to replace the character function from React?
Pass Svelte snippet with RateCharacterInfo. Its index starts at 0; value contains the currently displayed rating, including hover. For the extended wrapper, characterRender passes the original star snippet.