Rating
Choose a rating with whole or fractional values.
Demos
Size
Usage
vue
<script setup lang="ts">
import { Accordion } from '~/components/ui/accordion'
const items = [
{ value: 'source', label: 'Do I own the source?', content: 'Yes. Copy and adapt the files.' },
]
</script>
<template>
<Accordion :items="items" />
</template>
Rating uses SVG stars with a clipped fill for fractional values. The native radio buttons cover the star area and retain keyboard interaction. Coarse pointers get a 44px target around each star while the icon follows the selected size. Set step="0.5" to try half ratings.
Installation
Complete the shared installation. Copy Rating.vue and index.ts into app/components/ui/rating/.
app/components/ui/rating/Rating.vue
<script setup lang="ts">
import { Meter as Primitive } from 'akaza-ui'
import { computed, useAttrs } from 'vue'
import { cn } from '../../../utils/ui'
import uiConfig from '../ui.config'
import { meterVariants, resolveMeterVariants, type MeterProps, type MeterSlots } from './index'
defineOptions({ inheritAttrs: false })
// Preserve Akaza defaults when optional props, including Boolean aliases, are omitted.
const props = withDefaults(defineProps<MeterProps>(), {
min: undefined,
max: undefined,
low: undefined,
high: undefined,
optimum: undefined,
label: undefined,
ariaLabel: undefined,
ariaLabelledby: undefined,
ariaValueText: undefined,
locale: undefined,
formatOptions: undefined,
getAriaValueText: undefined,
getValueLabel: undefined,
ui: undefined,
})
defineSlots<MeterSlots>()
const attrs = useAttrs()
const styles = computed(() => meterVariants(resolveMeterVariants(props, uiConfig)))
const mergedUi = computed(() => {
const overrides = props.ui as Record<string, string | undefined> | undefined
return Object.fromEntries(
Object.entries(styles.value).map(([part, style]) => [
part,
cn(style(), overrides?.[part], part === 'root' ? props.class : undefined),
]),
)
})
const primitiveProps = computed(() => {
const { class: _class, ui: _ui, size: _size, ...native } = props
return native
})
</script>
<template>
<component :is="Primitive" v-bind="{ ...primitiveProps, ...attrs }" :ui="mergedUi">
<template v-for="(_, name) in $slots" #[name]="scope"
><slot :name="name as keyof MeterSlots" v-bind="scope ?? {}"
/></template>
</component>
</template>
API
Variants and defaults
| Prop | Values | Default |
|---|---|---|
size | xs, sm, md, lg, xl | sm |
Props
| Prop | Type | Default | Description |
|---|---|---|---|
length | number | 5 | Number of rating items. |
step | number | 1 | Selection granularity, clamped from 0.01 through 1. |
clearable | boolean | false | Selecting the committed value clears it. |
hoverable | boolean | false | Previews pointer values before commit. |
loop | boolean | true | Loops arrow navigation at range boundaries. |
orientation | "horizontal" | "vertical" | "horizontal" | Layout and arrow-key axis. |
dir | "ltr" | "rtl" | "ltr" | Horizontal navigation direction. |
autoFocus | boolean | false | Focuses selected or first value on mount. |
id | string | field/generated id | Native validity input id. |
name | string | field name | Native form field name. |
required | boolean | false | Requires a value above 0. |
disabled | boolean | false | Disables interaction and form submission. |
readOnly | boolean | false | Blocks value changes without disabling focus. |
invalid | boolean | false | Controlled invalid state. |
ariaLabel | string | "Rating" | Accessible group label. |
ariaLabelledby | string | — | Id of an external label. |
ariaDescribedby | string | field ids | Accessible description ids. |
getValueLabel | (value, maximum) => string | "value of maximum" | Accessible label for each radio value. |
ui | RatingUi | — | Classes for structural parts. |
Slots
| Slot | Props | Description |
|---|---|---|
item | item, value, previewValue, fill, state | Replaces one full visual item. |
empty | item | Empty item visual. |
filled | item | Filled item visual. |
radio | value, label | Content inside a generated radio target. |
state | value, previewValue, items, hoveredValue, clear | Optional state-driven custom UI. |
Events
| Event | Payload | Description |
|---|---|---|
update:modelValue | value | Fired after an accepted selection. |
value-change | (value, details) | Fired before commit. details.cancel() prevents it. |
hover-change | (value | null, details) | Fired when hover preview enters, moves, or leaves. Cancelable. |
Keyboard
| Key | Behavior |
|---|---|
ArrowRight / ArrowLeft | Move by step in horizontal mode, respecting RTL. |
ArrowDown / ArrowUp | Move by step in vertical mode. |
Home | Select the first value. |
End | Select the maximum value. |
Space / Enter | Select focused value; may clear it when clearable. |