Rating
Rating component
Install
npx ui8kit add ratingPulls in: box, compose, kit-box, lucide-icon, utils
Preview
Code
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework svelte --dir src --registry-url https://registry.buildy.tw/svelte --strict-cdnnpx ui8kit add rating<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
One to five stars (a review's grade, the average of a product). The value is read as the label; the stars
themselves are decoration.
-->
<script lang="ts" module>
import type { Snippet } from "svelte";
export type RatingProps = {
value: number;
label: string;
max?: number;
size?: number;
class?: string;
};
</script>
<script lang="ts">
import Box from "../compose/Box.svelte";
import LucideIcon from "../icons/LucideIcon.svelte";
import { cn } from "../utils";
let { value, label, max = 5, size = 16, class: className }: RatingProps = $props();
const full = $derived(Math.round(Math.min(max, Math.max(0, value))));
</script>
<Box
class={cn("inline-flex items-center gap-0", className)}
role="img"
aria-label={label}
data-slot="rating"
data-value={full}
>
{#each Array.from({ length: max }, (_, i) => i) as i (i)}
<LucideIcon name="star" size={size} class={((i < full) ? "fill-foreground text-foreground" : "text-border")} />
{/each}
</Box>
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework react --dir src --registry-url https://registry.buildy.tw/react --strict-cdnnpx ui8kit add rating// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
* One to five stars (a review's grade, the average of a product). The value is read as the label; the stars
* themselves are decoration.
*/
import { type ReactNode } from "react";
import { Box } from "../compose";
import { LucideIcon } from "../icons/LucideIcon";
import { cn } from "../utils";
export type RatingProps = {
value: number;
label: string;
max?: number;
size?: number;
className?: string;
};
export function Rating({ value, label, max = 5, size = 16, className }: RatingProps) {
const full = Math.round(Math.min(max, Math.max(0, value)));
return (
<Box
className={cn("inline-flex items-center gap-0", className)}
role="img"
aria-label={label}
data-slot="rating"
data-value={full}
>
{(Array.from({ length: max }, (_, i) => i) ?? []).map((i) => (
<LucideIcon key={i}
name="star"
size={size}
className={((i < full) ? "fill-foreground text-foreground" : "text-border")}
/>
))}
</Box>
);
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework vue --dir src --registry-url https://registry.buildy.tw/vue --strict-cdnnpx ui8kit add rating<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
One to five stars (a review's grade, the average of a product). The value is read as the label; the stars
themselves are decoration.
-->
<script lang="ts">
export type RatingProps = {
value: number;
label: string;
max?: number;
size?: number;
class?: string;
};
</script>
<script setup lang="ts">
import { computed } from "vue";
import Box from "../compose/Box.vue";
import LucideIcon from "../icons/LucideIcon.vue";
import { cn } from "../utils";
const { value, label, max = 5, size = 16, class: className } = defineProps<RatingProps>();
const full = computed(() => Math.round(Math.min(max, Math.max(0, value))));
</script>
<template>
<Box
:class="cn('inline-flex items-center gap-0', className)"
role="img"
:aria-label="label"
data-slot="rating"
:data-value="full"
>
<LucideIcon
v-for="i in Array.from({ length: max }, (_, i) => i)"
:key="i"
name="star"
:size="size"
:class="((i < full) ? 'fill-foreground text-foreground' : 'text-border')"
/>
</Box>
</template>
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework solid --dir src --registry-url https://registry.buildy.tw/solid --strict-cdnnpx ui8kit add rating// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
* One to five stars (a review's grade, the average of a product). The value is read as the label; the stars
* themselves are decoration.
*/
import { For, type JSX } from "solid-js";
import { Box } from "../compose";
import { LucideIcon } from "../icons/LucideIcon";
import { cn } from "../utils";
export type RatingProps = {
value: number;
label: string;
max?: number;
size?: number;
class?: string;
};
export function Rating({ value, label, max = 5, size = 16, class: className }: RatingProps) {
const full = Math.round(Math.min(max, Math.max(0, value)));
return (
<Box
class={cn("inline-flex items-center gap-0", className)}
role="img"
aria-label={label}
data-slot="rating"
data-value={full}
>
<For each={Array.from({ length: max }, (_, i) => i) ?? []}>{(i) => (
<LucideIcon name="star" size={size} class={((i < full) ? "fill-foreground text-foreground" : "text-border")} />
)}</For>
</Box>
);
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework templ --dir src --registry-url https://registry.buildy.tw/templ --strict-cdnnpx ui8kit add rating// Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
// Source: packages/compose-gen/specs/parts/rating.json
package blocks
import (
"github.com/ui8kit/codegen/generated/templ/ui/box"
"github.com/ui8kit/codegen/generated/templ/compose"
"github.com/ui8kit/codegen/generated/templ/icons"
uiutils "github.com/ui8kit/codegen/generated/templ/orgutils"
)
// RatingProps are the props of Rating; the values are the JSON shapes of the TypeScript props.
type RatingProps struct {
Value any `json:"value"`
Label any `json:"label"`
Max any `json:"max"`
Size any `json:"size"`
Class string `json:"className"`
}
// One to five stars (a review's grade, the average of a product). The value is read as the label; the stars themselves are decoration.
templ Rating(p RatingProps) {
{{ vValue := p.Value }}
{{ vLabel := p.Label }}
{{ vMax := uiutils.Def(p.Max, float64(5)) }}
{{ vSize := uiutils.Def(p.Size, float64(16)) }}
{{ vFull := any(uiutils.Round(uiutils.Min(vMax, uiutils.Max(float64(0), vValue)))) }}
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: uiutils.S(uiutils.Cnv("inline-flex items-center gap-0", p.Class)), Attrs: uiutils.Attrs("role", "img", "aria-label", vLabel, "data-slot", "rating", "data-value", vFull)}}) {
for _, vI := range uiutils.L(uiutils.Range(vMax)) {
@icons.LucideIcon(icons.LucideIconProps{Name: "star", Size: uiutils.I(vSize), Class: uiutils.S(uiutils.Cond(uiutils.T(uiutils.Lt(vI, vFull)), "fill-foreground text-foreground", "text-border"))})
}
}
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework twig --dir src --registry-url https://registry.buildy.tw/twig --strict-cdnnpx ui8kit add rating{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
Source: packages/compose-gen/specs/parts/rating.json
One to five stars (a review's grade, the average of a product). The value is read as the label; the stars themselves are decoration.
Props (p): value, label, max, size, className. -#}
{%- set v_value = (p.value ?? null) -%}
{%- set v_label = (p.label ?? null) -%}
{%- set v_max = (p.max ?? 5) -%}
{%- set v_size = (p.size ?? 16) -%}
{%- set v_full = og_round(og_min([v_max, og_max([0, v_value])])) -%}
{%- set c_1 -%}
{%- for v_i in og_l(og_range(v_max)) %}
{{ include('icons/LucideIcon.html.twig', {'name': 'star', 'size': og_i(v_size), 'class': og_s((og_t(og_lt(v_i, v_full)) ? 'fill-foreground text-foreground' : 'text-border'))}, with_context = false) }}
{%- endfor %}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': og_s(og_cnv(['inline-flex items-center gap-0', (p.className ?? '')])), 'attrs': og_attrs(['role', 'img', 'aria-label', v_label, 'data-slot', 'rating', 'data-value', v_full]), 'children': c_1}, with_context = false) }}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework latte --dir src --registry-url https://registry.buildy.tw/latte --strict-cdnnpx ui8kit add rating{* Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
Source: packages/compose-gen/specs/parts/rating.json
One to five stars (a review's grade, the average of a product). The value is read as the label; the stars themselves are decoration.
Props (p): value, label, max, size, className. *}
{var $v_value = ($p['value'] ?? null)}
{var $v_label = ($p['label'] ?? null)}
{var $v_max = ($p['max'] ?? 5)}
{var $v_size = ($p['size'] ?? 16)}
{var $v_full = og_round(og_min([$v_max, og_max([0, $v_value])]))}
{capture $c_1}
{foreach og_l(og_range($v_max)) as $v_i}
{=og_include('icons/LucideIcon.latte', ['name' => 'star', 'size' => og_i($v_size), 'class' => og_s((og_t(og_lt($v_i, $v_full)) ? 'fill-foreground text-foreground' : 'text-border'))])|noescape}
{/foreach}
{/capture}
{=og_include('compose/Box.latte', ['class' => og_s(og_cnv(['inline-flex items-center gap-0', ($p['className'] ?? '')])), 'attrs' => og_attrs(['role', 'img', 'aria-label', $v_label, 'data-slot', 'rating', 'data-value', $v_full]), 'children' => (string) $c_1])|noescape}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework jinja2 --dir src --registry-url https://registry.buildy.tw/jinja2 --strict-cdnnpx ui8kit add rating{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
Source: packages/compose-gen/specs/parts/rating.json
One to five stars (a review's grade, the average of a product). The value is read as the label; the stars themselves are decoration.
Props (p): value, label, max, size, className. -#}
{%- set v_value = og_def(p['value'], none) -%}
{%- set v_label = og_def(p['label'], none) -%}
{%- set v_max = og_def(p['max'], 5) -%}
{%- set v_size = og_def(p['size'], 16) -%}
{%- set v_full = og_round(og_min([v_max, og_max([0, v_value])])) -%}
{%- set c_1 -%}
{%- for v_i in og_l(og_range(v_max)) %}
{{ og_include('icons/lucide-icon.html.j2', {'p': {'Name': 'star', 'Size': og_i(v_size), 'Class': og_s(('fill-foreground text-foreground' if og_t(og_lt(v_i, v_full)) else 'text-border'))}}) }}
{%- endfor %}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': og_s(og_cnv(['inline-flex items-center gap-0', og_def(p['className'], '')])), 'Attrs': og_attrs(['role', 'img', 'aria-label', v_label, 'data-slot', 'rating', 'data-value', v_full])}, 'children': c_1}) }}
Props
Names and types come from the xgen specs of the source. Standard HTML attributes (id, role, tabIndex, aria-*) pass through to the root element; the class prop is className in React.
One to five stars (a review's grade, the average of a product). The value is read as the label; the stars themselves are decoration.
| Prop | Type | Default | Description |
|---|---|---|---|
| value (required) | number | - | 0 to max, halves are rounded |
| label (required) | string | - | read to assistive technology, e.g. "4 of 5" |
| max | number | 5 | number of stars |
| size | number | 16 | size of a star in px |