Ant Design Svelte

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

0 out of 5

Basic rating

Score from 0 to 5. value is stored in the Svelte state, onChange accepts the new selection.

Dimensions

large, medium and small. Without local size, the component inherits the size from the ConfigProvider.

2.5

Half stars

allowHalf allows half rating. The left side of the star selects half, the arrows change the rating in increments of 0.5.

Normal

Labels and tips

tooltips sets a tooltip for each star. The adjacent explanation reflects the selected value and the unrated status.

Read only

disabled shows a full or half rating already given and blocks interaction.

allowClear=true

allowClear=false

3; 3

Clear rating

Clicking on the selected star again resets the rating to 0. allowClear=false saves it.

Other symbols

character replaces the star with a heart, letter or hieroglyph. Color and size can be set in the usual style.

2; 3

Custom symbols by index

Svelte snippet receives RateCharacterInfo. index specifies a number or facial expression for each of the five positions.

Rating: 2; guidance: —; key: —

Pointing and keyboard

onHoverChange reports a preliminary estimate and undefined when the pointer leaves. keyboard controls arrow processing; onKeyDown receives the event.

Rating: 2.5 out of 5

Applied assessment

Review example: half choice with hints, result and separate read-only score. The model is reused.

3 out of 5
General: suite; local: project-rate

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.

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.

Main parameters Rate
PropertyDescriptionTypeDefault
prefixClsFull 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.stringConfigProvider.prefixCls + "-rate"
valueGuided assessment. Accept the new number via onChange; bind:value is not supported.numberundefined
defaultValueInitial local estimate in the absence of value.number0
countNumber of characters and upper limit of keyboard selection.number5
allowHalfSelect half a character and keyboard step 0.5 instead of 1.booleanfalse
allowClearClicking the selected item again clears the score to 0.booleantrue
disabledRead-only mode. Inherits accessibility from Form and ConfigProvider.booleanFrom context
keyboardLeft and right arrows change the rating; RTL reverses the direction.booleantrue
sizesmall, medium and large.ComponentSizeFrom ConfigProvider
characterOne symbol for all positions or snippet with index, count, value, allowHalf, disabled and focused.string | number | Snippet<[RateCharacterInfo]>Star
characterRenderExtended wrapper around the original star snippet. Takes precedence over automatic tooltips wrapper.Snippet<[Snippet, RateCharacterInfo]>—
tooltipsSymbol index hints. Objects allow you to set Tooltip parameters.readonly (string | Omit<TooltipProps, 'children'>)[]—
onChangeReceives a new rating when selected, cleared or keyed.(value: number) => void—
onHoverChangePreliminary assessment when hovering. Pointer departure and selection completion pass undefined.(value: number | undefined) => void—
onKeyDownKeyboard event, including when keyboard=false.(event: KeyboardEvent) => void—
onFocusReports receiving focus. onBlur reports loss of focus; native onfocusin and onfocusout are also available.() => void—
autoFocusFocuses the container after mount if the component is available.booleanfalse
tabIndexTab index of the container. When disabled, the container and buttons are excluded from the Tab order.number0
nativeElementbind: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.

Component Tokens Rate
TokenPurpose
starBgColor of the empty symbol.
starColorColor of the filled symbol.
starHoverScaleCSS transform of the symbol on hover.
starSizeAverage symbol size.
starSizeLGLarge symbol size.
starSizeSMSmall symbol size.

Common tokens of the current topic

The values are read through theme.useToken() and react to the settings in the header.

Common tokens of the current topic
TokenPurposeCurrent value
colorTextPrimary text color.rgba(0,0,0,0.88)
fontFamilyFont 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'
fontSizeBasic text size.14
lineHeightBase line height.1.5714285714285714
marginXSIndentation between characters.8
motionDurationMidTransition 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.