# Star rating

Subtle web animation sketch from [https://mighil.com/animations](https://mighil.com/animations).

- **Live demo:** https://mighil.com/animations/star-rating
- **Standalone HTML:** https://mighil.com/animations/star-rating?download=1
- **This file:** https://mighil.com/animations/star-rating/star-rating.md
- **Technique:** `input`

## What it does

Hover previews the rating. Click locks gold.

## Instructions for AI / coding agents

Implement this micro-interaction in the user's stack (vanilla HTML, React, Vue, Svelte, etc.).

1. Use the **HTML** as the DOM structure (adapt JSX/templates as needed; keep class names and ids).
2. Include the **Theme** CSS (or map the same variables into the host design system). It switches light/dark with `prefers-color-scheme`.
3. Apply the **CSS** as-is when possible. It uses `var(--text-color)`, `var(--heading-color)`, `var(--background-color)`, etc. from the theme so both modes work.
4. Run the **JavaScript** after the markup mounts. Prefer `DOMContentLoaded` or a framework `useEffect` / `onMount`.
5. Keep motion respectful of `prefers-reduced-motion` if the snippet already checks it.
6. Do not depend on mighil.com runtime or fonts. Theme variables are enough for color.
7. If porting to a component, expose the interactive root via a ref and preserve accessibility attributes (`aria-*`, `role`, `button` types).

## Theme (auto light / dark)

```css
/* Auto light / dark — follows prefers-color-scheme */
:root{
  color-scheme: light dark;
  --text-color: hsl(0, 0%, 18%);
  --heading-color: hsl(0, 0%, 18%);
  --background-color: hsl(0, 0%, 99%);
  --bg-color: hsl(0, 0%, 99%);
  --link-color: hsl(0, 0%, 18%);
  --visited-color: hsl(0, 0%, 18%);
  --blockquote-color: hsl(0, 0%, 18%);
  --code-bg-color: hsl(0, 0%, 95%);
  --code-color: hsl(0, 0%, 18%);
  --sub-color: hsl(0, 0%, 29%);
}
@media (prefers-color-scheme: dark){
:root{
  --text-color: hsl(35, 25%, 85%);
  --heading-color: hsl(35.2, 32.6%, 82.5%);
  --background-color: hsl(60, 2.2%, 8.8%);
  --bg-color: hsl(60, 2.2%, 8.8%);
  --link-color: hsl(34.7, 55.3%, 79.8%);
  --visited-color: hsla(34.7, 55.3%, 79.8%, 0.85);
  --blockquote-color: hsl(35, 30%, 75%);
  --code-bg-color: hsl(30, 5%, 7.8%);
  --code-color: limegreen;
  --sub-color: hsla(34.7, 24.7%, 84.9%, 0.78);
}
}
```

## HTML

```html
<div class="stars" id="stars" role="radiogroup" aria-label="Rating">
        <button type="button" role="radio" aria-checked="false" aria-label="1 star" data-star="1"><svg viewBox="0 0 24 24"><path d="M12 3.2l2.4 4.9 5.4.8-3.9 3.8.9 5.4L12 15.6 7.2 18.1l.9-5.4L4.2 8.9l5.4-.8z"/></svg></button>
        <button type="button" role="radio" aria-checked="false" aria-label="2 stars" data-star="2"><svg viewBox="0 0 24 24"><path d="M12 3.2l2.4 4.9 5.4.8-3.9 3.8.9 5.4L12 15.6 7.2 18.1l.9-5.4L4.2 8.9l5.4-.8z"/></svg></button>
        <button type="button" role="radio" aria-checked="false" aria-label="3 stars" data-star="3"><svg viewBox="0 0 24 24"><path d="M12 3.2l2.4 4.9 5.4.8-3.9 3.8.9 5.4L12 15.6 7.2 18.1l.9-5.4L4.2 8.9l5.4-.8z"/></svg></button>
        <button type="button" role="radio" aria-checked="false" aria-label="4 stars" data-star="4"><svg viewBox="0 0 24 24"><path d="M12 3.2l2.4 4.9 5.4.8-3.9 3.8.9 5.4L12 15.6 7.2 18.1l.9-5.4L4.2 8.9l5.4-.8z"/></svg></button>
        <button type="button" role="radio" aria-checked="false" aria-label="5 stars" data-star="5"><svg viewBox="0 0 24 24"><path d="M12 3.2l2.4 4.9 5.4.8-3.9 3.8.9 5.4L12 15.6 7.2 18.1l.9-5.4L4.2 8.9l5.4-.8z"/></svg></button>
      </div>
```

## CSS

```css
@keyframes star-fill{
  0%{transform:scale(.9)}
  55%{transform:scale(1.06)}
  100%{transform:scale(1)}
}

.stars{display:inline-flex;gap:4px}

.stars button{
  appearance:none;-webkit-appearance:none;border:0;background:transparent;padding:4px;cursor:pointer;
  line-height:0;color:color-mix(in srgb,var(--text-color) 40%,transparent);
  transition:color .18s ease, transform .2s ease;
}

.stars button svg{width:22px;height:22px;display:block;transform-origin:center}

.stars button svg path{
  fill:transparent;stroke:currentColor;stroke-width:1.6;stroke-linejoin:round;
  transition:fill .22s ease, stroke .22s ease;
}

.stars button.is-preview,
.stars button.is-preview.is-on{
  color:var(--text-color);
}

.stars button.is-preview svg path,
.stars button.is-preview.is-on svg path{
  fill:currentColor;
  stroke:currentColor;
}

.stars button.is-on{
  color:color-mix(in srgb,#c9962a 88%,var(--text-color));
}

.stars button.is-on svg path{
  fill:color-mix(in srgb,#f0c14b 92%,#fff);
  stroke:color-mix(in srgb,#c9922a 90%,var(--text-color));
}

.stars button.is-pop svg{animation:star-fill .42s cubic-bezier(.34,1.35,.64,1) both;animation-delay:var(--star-delay,0ms)}

@media (prefers-reduced-motion: reduce){
  .breath-dot,.breath-ring,.wave.is-live span,.ink-drop,
  .marquee-track,
  .skel-line,.save-btn.is-loading .spin,.badge.is-bump,.like-btn.is-pop,.save-status.is-saving .dot,.stars button.is-pop svg,
  .field.is-invalid input,.save-btn.is-done .check path,.ux-check[aria-checked="true"] .ux-box path,.char-meta.is-warn,.success-banner.is-on path,
  .typing span,.conn-dot,.indet > i,.toast.is-on .toast-timer > i,.stagger-list.is-in .stagger-item,.submit-wrap.is-loading .submit-ring rect,.submit-wrap.is-settle,.hold-btn.is-settle,.confirm-btn.is-settle,.sync-btn.is-settle,.invite-btn.is-settle,.retry-btn.is-shake-err,.retry-btn.is-shake-ok,.cart-badge.is-pop,.mute-btn .x-line,.presence.is-ping .presence-dot::after,.tag-chip,.nudge-field.is-nudge,.tilt-scene:hover .tilt-tile::before,.demo-replay.is-tap,.hit.is-tap,.stack-toggle.is-tap,.nudge-btn.is-tap,.wipe.is-tap,.save-btn.is-tap,.copy-btn.is-tap,.like-btn.is-tap,.pin-btn.is-tap,.retry-btn.is-tap,.wizard-nav button.is-tap,.mute-btn.is-tap,
  .scan:hover::after,.scan.is-on::after,.cascade.is-in b,.stepper-val.is-bump,.otp input.is-pop,.rel-time.is-live .rel-dot,.sync-btn.is-spin .sync-arrows,.ok-field.is-ok .ok-check path,.unread-row.is-read .unread-check path,.mic-btn.is-on::before,.mic-btn.is-on::after,.ghost-wrap.is-flash,.cmd-palette li.is-run,.flag-row.is-warn,.mention.is-flash b{animation:none!important}

  .grow-link::after,.wipe,.wipe .b,.shuffle .card,.odo-digit > b,.flap-reel,.step-node,.step-line > i,
  .switch,.switch::after,.copy-btn .c-idle,.copy-btn .c-done,.field label,.tip-bubble,.chip,.like-btn svg,
  .disc-btn svg,.disc-panel,.snack,.save-btn,.ux-box,.seg-pill,.soft-tabs-ink,.list-actions,.success-banner,.search-clear,.pin-btn svg,.submit-ring rect,
  .hold-btn .hold-fill,.toast,.menu-btn .menu-line,.stars button,.conn,.retry-btn,.sort-item,.mute-btn .wave,.mute-btn .x-line,.wizard-dots span,.cart-badge,.range-bubble,
  .filter-list li,.upload-bar > i,.quota-track > i,.quota-tip,.splitbtn-toggle svg,.splitbtn-menu,.presence-dot,
  .draft-banner,.tag-chip,.empty-state,.empty-item,.deadline,.deadline-clock,.deadline-track > i,.edge-fade-mask,.dirty-bar,.seen-ticks path,.nudge-field,.stack-card,
  .blur-line,.xface span,.magnet-chip,.confirm-btn,.confirm-btn span,.offline-banner,.sort-flip-btn svg,.sort-flip-list li,.react-fan,.react-fan button,.sticky-cta,
  .pw-toggle .pw-eye,.pw-toggle .pw-slash,.otp input,.exp-search,.exp-search input,.ok-field input,.ok-field .ok-check,.unread-dot,.unread-check,.unread-row,.sync-label,.sync-btn .sync-arrows,.sync-btn .sync-check,
  .inline-edit .ie-title,.inline-edit .ie-rename,.inline-edit .ie-panel,.inline-edit .ie-input,.inline-edit .ie-bar,
  .ghost-wrap .ghost-hint,.ghost-wrap .ghost-key,.cmd-palette,.cmd-palette li,.cmd-stage::before,.crumb-mid,.crumb-more,.diff-add,.diff-add > .diff-add-inner,.slot > i,.zoom-thumb,.zoom-full,.zoom-caption,
  .bulk-bar,.bulk-check svg,.bulk-row,.env-switch .env-ink,.env-switch button,.invite-btn .invite-idle,.invite-btn .invite-done,.flag-row,.flag-sw,.flag-sw::after,.deploy-node,.deploy-node i,.deploy-wire > b,.ws-ink,.ws-item .ws-check,.thread,.thread-body,.thread-resolve svg,
  .unfurl-card,.unfurl-thumb,.seat,.form-prog-bar > i,.form-prog-field,.form-prog-field svg,.mention b,.glyph-tile path,.glyph-tile line,.glyph-tile circle{transition:none!important}
}

@media (hover: hover) and (pointer: fine){
  .stars button:hover{transform:translateY(-1px)}
}
```

## JavaScript

```js
// Star rating — preview = currentColor; commit = gold
  const stars = document.getElementById('stars');
  let rating = 0;
  function clearStarPop(b) {
    b.classList.remove('is-pop');
    b.style.removeProperty('--star-delay');
  }
  function resetStars() {
    if (!stars) return;
    rating = 0;
    stars.querySelectorAll('[data-star]').forEach((b) => {
      b.classList.remove('is-on', 'is-preview');
      clearStarPop(b);
      b.setAttribute('aria-checked', 'false');
    });
  }
  function paintPreview(n) {
    if (!stars) return;
    stars.querySelectorAll('[data-star]').forEach((b) => {
      const v = Number(b.getAttribute('data-star'));
      clearStarPop(b);
      b.classList.toggle('is-preview', v <= n);
      // never leave higher committed stars filled while previewing lower
      b.classList.toggle('is-on', rating > 0 && v <= Math.min(rating, n));
    });
  }
  function paintRated(animate) {
    if (!stars) return;
    stars.querySelectorAll('[data-star]').forEach((b) => {
      const v = Number(b.getAttribute('data-star'));
      const on = rating > 0 && v <= rating;
      const wasOn = b.classList.contains('is-on');
      b.classList.remove('is-preview');
      clearStarPop(b);
      b.classList.toggle('is-on', on);
      b.setAttribute('aria-checked', v === rating ? 'true' : 'false');
      if (animate && on && !wasOn && !reduce) {
        b.style.setProperty('--star-delay', ((v - 1) * 48) + 'ms');
        void b.offsetWidth;
        b.classList.add('is-pop');
      }
    });
  }
  if (stars) {
    stars.querySelectorAll('[data-star]').forEach((b) => {
      b.addEventListener('mouseenter', () => paintPreview(Number(b.getAttribute('data-star'))));
      b.addEventListener('focus', () => paintPreview(Number(b.getAttribute('data-star'))));
      b.addEventListener('click', () => {
        rating = Number(b.getAttribute('data-star'));
        paintRated(true);
      });
    });
    stars.addEventListener('mouseleave', () => paintRated(false));
  }

document.querySelectorAll('[data-act]').forEach((btn) => {
    btn.addEventListener('click', (e) => {
      e.stopPropagation();
      const act = btn.getAttribute('data-act');
      if (act === 'stars') resetStars();
    });
  });
```

---

_Source: https://mighil.com/animations/star-rating_
