v0.0.2
Demos (demo panel, opens in a new tab)

Star rating

Lets users give a star rating, or shows an existing rating.

zb:rating zubazar/ui-pro

PRO

This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →

Basic usage

Basic usage: view
BLADE

value is the starting rating, and label tells screen readers what the rating is about.

blade
<zb:rating :value="3" label="Order satisfaction" />
PropValues
valueInitial rating (integer)
labelScreen reader name; otherwise the page's aria-label, then "Rating"

Read-only

Read-only: view
BLADE

readonly draws a display that cannot be clicked; with fractional ratings only whole stars are filled.

blade
<div class="flex items-center gap-3">
    <zb:rating :value="4" readonly />
    <span class="text-sm text-dim">4.0 · 128 reviews</span>
</div>
PropValues
readonlytrue | false

Sizes

Sizes: view
BLADE

size picks the star size: sm in table rows, the default md, and lg in headings.

blade
<div class="flex flex-wrap items-center gap-8">
    <zb:rating :value="4" readonly size="sm" />
    <zb:rating :value="4" readonly />
    <zb:rating :value="4" readonly size="lg" />
</div>
PropValues
sizesm | md | lg

In a form

BLADE

With name, the selected rating is sent with the form in a hidden field; when no rating is selected (0), the field is sent empty.

blade
<form method="POST" action="{{ route('orders.rate', $order) }}">
    @csrf
    <zb:rating name="rating" :value="old('rating', 0)" label="Order satisfaction" />
    <zb:button type="submit" size="sm" class="mt-4">Submit</zb:button>
</form>
PropValues
nameName of the form field

With Livewire

BLADE

wire:model writes the selected rating to the Livewire property (on every click with .live); the validation error shows below the stars automatically.

blade
<zb:rating wire:model.live="rating" label="Order satisfaction" required hint="Your rating is not shown to the seller." />
PropValues
wire:modelLivewire property
hintShown when there is no error

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
valuenumber0Initial rating
maxnumber5Number of stars
readonlybooleanfalseRead-only display
size—mdsm · md · lg
label—nullScreen reader name. Otherwise the page's aria-label is used; without one, "Rating", in read-only mode "value / max". With aria-labelledby, no default name is written
name—nullField name for plain forms; the rating is sent in a hidden field (not in read-only mode)
hint—nullHelp text below the stars when there is no error; linked to the group of stars with aria-describedby (not in read-only mode)
input-class—nullClasses of the group of stars itself; class places the whole field

Advanced

2 examples

With Alpine

With Alpine: view
BLADE

x-model binds the selected rating to an Alpine variable; if the variable changes from outside, the stars update as well.

blade
<div x-data="{ rating: 3 }" class="flex items-center gap-3">
    <zb:rating x-model="rating" label="Order satisfaction" />
    <span class="text-sm text-dim">Selected: <span x-text="rating"></span> / 5</span>
</div>
PropValues
x-modelAlpine variable

Number of stars

Number of stars: view
BLADE

max sets the number of stars shown; the default is 5.

blade
<zb:rating :value="7" :max="10" readonly />
PropValues
maxNumber of stars

Developer notes

Events, hooks and details
  • The rating is also kept in a hidden field named after the property, and a change event is dispatched on click, so zb:form dirty-check and zb:form-actions see the change.
  • Attributes such as class and wire:key stay on the outer wrapper; required, aria-* and data-* go to the star group, and autofocus to the first star.
Zubazar UI · ZubazarDesigned for people.