Zubra UI
Code for
GitHub

star-rating

A star rating that is a form control: it submits its value under its name, is invalid while required and unset, and goes back to 0 when its form resets. readonly shows a rating.

Try it: events appear here (valuechange).

Source: star-rating.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/star-rating.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/star-rating.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/star-rating.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/star-rating.json
One file, no dependencies (3.7 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/star-rating.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/star-rating.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/star-rating.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/star-rating.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/star-rating.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/star-rating.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/star-rating.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/star-rating.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/star-rating.js"></script>

The compiled install writes src/components/zubra/star-rating.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<star-rating label="Rate this recipe" name="rating"></star-rating>

<script type="module">
  import './components/zubra/star-rating.js';

  const element = document.querySelector('star-rating');
  element.value = 4;
  element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
</script>
Example.jsx
import './components/zubra/star-rating.js';

const value = 4;

export function StarRatingExample() {
  return (
    <star-rating
      label="Rate this recipe"
      name="rating"
      value={value}
      onvaluechange={(event) => console.log('valuechange', event.detail)}
    ></star-rating>
  );
}
Example.vue
<script setup>
import './components/zubra/star-rating.js';
const value = 4;
const onValuechange = (event) => console.log('valuechange', event.detail);
</script>

<template>
  <star-rating
    label="Rate this recipe"
    name="rating"
    :value.prop="value"
    @valuechange="onValuechange"
  ></star-rating>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/star-rating.js';
  const value = 4;
</script>

<star-rating
  label="Rate this recipe"
  name="rating"
  {value}
  onvaluechange={(event) => console.log('valuechange', event.detail)}
></star-rating>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/star-rating.js';

@Component({
  selector: 'app-star-rating-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <star-rating
      label="Rate this recipe"
      name="rating"
      [value]="value"
      (valuechange)="onValuechange($event)"
    ></star-rating>
  `,
})
export class StarRatingExample {
  value = 4;

  onValuechange(event: Event) {
    console.log('valuechange', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
valuenumber0
maxnumber5
requiredbooleanfalse
readonlybooleanfalse
disabledbooleanfalse
labelstring"Rating"
Events
EventWhen
valuechangeThe user picked a rating; detail is the number of stars.

Size

1.1 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 18.0 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour