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
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
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
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
<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
<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>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>
);
}<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('-') } } }) --><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>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
| Property | Type | Default |
|---|---|---|
value | number | 0 |
max | number | 5 |
required | boolean | false |
readonly | boolean | false |
disabled | boolean | false |
label | string | "Rating" |
| Event | When |
|---|---|
valuechange | The 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.