Rate
A keyboard-accessible star rating with half scores, descriptive text, colors, icons, form integration, and focus methods.
Import
import { HuiRate } from '@nscode/handy-ui'
import type { RateProps, RateEmits, RateExpose, RateSize } from '@nscode/handy-ui'
Internal state and controlled updates
An omitted value starts at zero. Explicit undefined remains controlled at zero; hovering only previews a score. Omitting the prop, including when only listening for updates, uses internal state that survives unrelated parent renders. A passed prop remains controlled; rejecting an update preserves the parent value. Switching modes reports a development warning. No defaultValue prop is added.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRate, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request')
const q1Model = ref(1)
</script>
<template>
<div class="q1-state-demo rate-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>Omitted prop: internal state</p>
<div class="q1-local"><HuiRate show-text /></div>
</div>
<div>
<p>Update listener only: internal state</p>
<div class="q1-listener">
<HuiRate show-text @update:model-value="q1Last = String($event)" />
</div>
</div>
<div>
<p>Explicit undefined: controlled</p>
<div class="q1-empty"><HuiRate show-text :model-value="undefined" /></div>
</div>
<div>
<p>Fixed controlled value: parent rejects</p>
<div class="q1-fixed">
<HuiRate show-text :model-value="2" @update:model-value="q1Last = String($event)" />
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound"><HuiRate show-text v-model="q1Model" /></div>
</div>
<p class="q1-notification">Bound value: {{ q1Model }}; latest request: {{ q1Last }}</p>
</HuiSpace>
</div>
</template>
Examples
Binding and Half Scores
Bind a numeric score. With allow-half, the left half of a star represents half a point. Clicking the current score does not clear it or emit another change; set the model to zero or press Home to clear the rating.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRate, HuiSpace } from '@nscode/handy-ui'
const whole = ref(3)
const half = ref(2.5)
const ten = ref(7)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRate v-model="whole" />
<HuiRate v-model="half" allow-half />
<HuiRate v-model="ten" :max="10" show-text />
<span>Scores: {{ whole }}, {{ half }}, {{ ten }}</span>
</HuiSpace>
</template>
Disabled and Readonly
Readonly ratings remain keyboard focusable for reading, but cannot change. Disabled ratings cannot receive focus.
<script setup lang="ts">
import { HuiRate, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRate :model-value="3.5" allow-half disabled show-text />
<HuiRate :model-value="3.5" allow-half readonly show-text />
</HuiSpace>
</template>
Text, Colors, and Icons
Text and color arrays use the rounded-up score as an index; larger scores use the last entry. Empty texts suppress descriptive text. A color function also receives the rounded-up score. Only icons[0] is currently used for both selected and unselected stars.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRate, HuiSpace } from '@nscode/handy-ui'
const score = ref(3)
const texts = ['Poor', 'Fair', 'Good', 'Very good', 'Excellent']
const colors = [
'var(--hui-color-danger)',
'var(--hui-color-warning)',
'var(--hui-color-primary)',
'var(--hui-color-primary)',
'var(--hui-color-success)'
]
const color = (value: number) =>
value < 3 ? 'var(--hui-color-warning)' : 'var(--hui-color-success)'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRate v-model="score" show-text />
<HuiRate v-model="score" show-text :texts="texts" :colors="colors" />
<HuiRate v-model="score" show-text :texts="[]" :colors="color" :icons="['heart']" />
</HuiSpace>
</template>
Sizes
Stars are 24, 18, and 14px for large, default, and small; descriptive text is 16, 14, and 12px. Omitted sizes inherit form or provider settings.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRate, HuiSpace } from '@nscode/handy-ui'
import type { RateSize } from '@nscode/handy-ui'
const sizes: RateSize[] = ['large', 'default', 'small']
const value = ref(3)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace v-for="size in sizes" :key="size"
><span>{{ size }}</span
><HuiRate v-model="value" :size="size" show-text
/></HuiSpace>
</HuiSpace>
</template>
Keyboard, Events, and Focus Methods
Arrow keys adjust by one point, or half a point with allow-half. Home sets zero; End sets max. Screen readers hear the score with its level text (for example "3 Fair"); at 0, or when texts is an empty array, they hear only the number. The component emits update before change only when a user action changes the score. RateExpose exposes focus and blur without changing the value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiRate, HuiSpace } from '@nscode/handy-ui'
import type { RateExpose } from '@nscode/handy-ui'
const control = ref<RateExpose>()
const value = ref(2.5)
const event = ref('Waiting')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiRate
ref="control"
v-model="value"
allow-half
show-text
aria-label="Service rating"
@change="event = 'change: ' + $event"
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}; score: {{ value }}</span>
</HuiSpace>
</template>
Form Validation
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiRate, HuiSpace } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ score: 0 })
const rules: Record<string, FormRule[]> = {
score: [{ min: 1, message: 'Choose at least one star', trigger: 'change' }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm :model="model" :rules="rules"
><HuiFormItem label="Service" prop="score"><HuiRate v-model="model.score" /></HuiFormItem
></HuiForm>
<HuiRate :model-value="3" :validate-event="false" aria-label="No automatic validation" />
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | number | undefined | Score. Omit it for internal state starting at zero; a passed prop is controlled, with undefined displayed as zero. |
| max | number | 5 | Maximum score and number of stars |
| disabled | boolean | false | Disable interaction and focus |
| readonly | boolean | false | Display only, retaining keyboard focus |
| allowHalf | boolean | false | Allow half-star scores |
| showText | boolean | false | Display descriptive text |
| texts | string[] | Locale texts | Score descriptions; empty array hides text |
| colors | string[] | ((value: number) => string) | Theme warning color (--hui-color-warning) | Colors by rounded-up score |
| icons | string[] | ['star', 'star'] | First icon name is used for all stars |
| size | 'large' | 'default' | 'small' | — | Inherited size |
| validateEvent | boolean | true | Validate when the value changes |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | (value: number) | User interaction changes the score |
| change | (value: number) | Follows update:modelValue |
| focus | (event: FocusEvent) | Focus enters the component |
| blur | (event: FocusEvent) | Focus leaves the component |
Slots
No slots.
Methods
| Name | Type | Description |
|---|---|---|
| focus | () => void | Focus the current or first available control unless disabled or unmounted |
| blur | () => void | Blur only an active element inside this component in its owner document |
Types
import type { RateSize, RateProps, RateEmits, RateExpose } from '@nscode/handy-ui'
export type RateSize = 'large' | 'default' | 'small'
export interface RateProps {
modelValue?: number
max?: number
disabled?: boolean
readonly?: boolean
allowHalf?: boolean
showText?: boolean
texts?: string[]
colors?: string[] | ((value: number) => string)
icons?: string[]
size?: RateSize
validateEvent?: boolean
}
export interface RateEmits {
focus: [event: FocusEvent]
blur: [event: FocusEvent]
'update:modelValue': [value: number]
change: [value: number]
}
export interface RateExpose {
focus: () => void
blur: () => void
}