asafefe.dev

Sun Moon Icon

New

A theme toggle. The rays pull in, the sun sets, and the moon swings up in its place.

motion@soultel/use-reduced-motion-safe@soultel/use-icon-trigger
hover me

Installation

$ pnpm dlx shadcn@latest add @soultel/icon-sun-moon

No namespace set up? npx shadcn@latest add https://asafefe.dev/r/icon-sun-moon.json works too.

Manual installation
  1. 1. Install the dependencies.
    terminal1 lines
    npm install motion
  2. 2. Copy the source from the Code tab into components/icon-sun-moon.tsx.

Usage

example.tsx13 lines
import { useTheme } from "next-themes"
import { SunMoonIcon } from "@/components/icon-sun-moon"

export function ThemeToggle() {
  const { resolvedTheme, setTheme } = useTheme()
  const dark = resolvedTheme === "dark"

  return (
    <button onClick={() => setTheme(dark ? "light" : "dark")} aria-label="Toggle theme">
      <SunMoonIcon dark={dark} />
    </button>
  )
}

Props

PropTypeDefaultDescription
darkbooleanfalseShows the moon instead of the sun.
sizenumber24Width and height in px.
strokeWidthnumber2Stroke width on the 24 by 24 grid.
animate"hover" | "always" | boolean"hover""hover" plays when the nearest button or link (or the icon) is hovered, focused or tapped. "always" loops. A boolean plays each time it turns true, so a parent can drive it.
labelstring-Accessible name. Without one the icon is aria-hidden.
classNamestring-Classes for the svg. Color comes from currentColor.