moon

MoonIcon is an animated crescent-moon SVG icon for React that wiggles its outline with a short rotation when hovered. It can also be driven imperatively through a ref with startAnimation and stopAnimation.

moon
LIVE · running in a sandboxed iframe
Installed with plain shadcn add · no workarounds · theme neutral (none shipped)
See how it was built

Installation

pnpm dlx shadcn@latest add @heroicons-animated/moon

Requires @heroicons-animated in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { MoonIcon } from "@/components/ui/moon";<MoonIcon />
  • Theme or dark-mode toggles where a moon icon represents night mode and should feel lively on hover.
  • Navigation items, sidebars, or settings menus that need a moon icon with subtle micro-interaction.
  • Buttons or call-to-action elements where the icon should react to hover or be triggered programmatically.
  • Places where you need the icon to animate in sync with app state by calling startAnimation or stopAnimation through a ref.

Examples

Basic hover animation

Renders the moon icon at the default size. Hovering the wrapper div plays the wiggle animation automatically.

basic-hover-animation.tsx
import { MoonIcon } from "@/components/ui/moon";export function Basic() {  return <MoonIcon />;}

Custom size and color

Sets a larger size through the size prop and colors the icon with a Tailwind text color class, since the stroke uses currentColor.

custom-size-and-color.tsx
import { MoonIcon } from "@/components/ui/moon";export function Colored() {  return <MoonIcon size={40} className="text-indigo-500" />;}

Controlled animation via ref

Attaches a ref to take control of the animation. Once a ref is attached, hover events are forwarded to onMouseEnter and onMouseLeave instead of playing the animation, so the parent decides when to start and stop it.

controlled-animation-via-ref.tsx
"use client";import { useRef } from "react";import { MoonIcon, type MoonIconHandle } from "@/components/ui/moon";export function ControlledMoon() {  const moonRef = useRef<MoonIconHandle>(null);  return (    <div      onMouseEnter={() => moonRef.current?.startAnimation()}      onMouseLeave={() => moonRef.current?.stopAnimation()}    >      <MoonIcon ref={moonRef} />    </div>  );}

Dark mode toggle button

Uses the moon icon inside a button for a theme switch. The ref lets the click handler trigger the wiggle once when the theme changes.

dark-mode-toggle-button.tsx
"use client";import { useRef } from "react";import { MoonIcon, type MoonIconHandle } from "@/components/ui/moon";export function ThemeToggle() {  const moonRef = useRef<MoonIconHandle>(null);  return (    <button      type="button"      aria-label="Toggle dark mode"      className="inline-flex items-center justify-center rounded-md p-2 hover:bg-accent"      onClick={() => moonRef.current?.startAnimation()}    >      <MoonIcon ref={moonRef} size={20} />    </button>  );}

API reference

PropTypeDefaultDescription
sizenumber28Width and height of the SVG in pixels. Applied to both the width and height attributes of the svg element.
classNamestring—Class names applied to the outer div wrapper, passed through cn(). Use it for color (text-* classes), margins, or layout.
onMouseEnter(e: React.MouseEvent<HTMLDivElement>) => void—Called on mouse enter. When the component is controlled via ref, this handler replaces the built-in hover animation and is invoked instead of playing the animation.
onMouseLeave(e: React.MouseEvent<HTMLDivElement>) => void—Called on mouse leave. When the component is controlled via ref, this handler replaces the built-in return-to-rest animation and is invoked instead of resetting it.
refReact.Ref<MoonIconHandle>—Forwarded ref exposing startAnimation() and stopAnimation(). Attaching a ref switches the component into controlled mode.
HTMLAttributes<HTMLDivElement>extends HTMLAttributes<HTMLDivElement>—All other standard div attributes (id, style, aria-*, data-*, and so on) are spread onto the outer div wrapper.

Accessibility

  • The icon is purely decorative by default and has no text alternative. If it conveys meaning, add aria-label or a visually hidden label on the wrapper, or pass role="img" with aria-label through the spread props.
  • If the icon sits inside an interactive element such as a button, give the button an accessible name (for example aria-label="Toggle dark mode") and mark the icon itself as aria-hidden="true" to avoid redundant announcements.
  • The wrapper is a plain div with no tabindex or keyboard handlers. Keyboard users cannot trigger the hover animation, so use startAnimation from a focusable control if the animation is meaningful.
  • Respect reduced-motion preferences: the wiggle is a short rotation, but if motion sensitivity is a concern, skip calling startAnimation or wrap the trigger in a prefers-reduced-motion check.
  • The icon uses currentColor, so it keeps the contrast of the surrounding text color. Verify contrast against the background in both light and dark themes.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "moon" component (heroicons-animated/moon) from its shadcn registry.1. Install it with: npx shadcn@latest add @heroicons-animated/moon2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: size, className, onMouseEnter, onMouseLeave, ref, HTMLAttributes<HTMLDivElement>.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { MoonIcon } from "@/components/ui/moon";<MoonIcon />```

Files & dependencies

  • moon.tsx
dependenciesmotion

Looks similar, elsewhere