{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"theme-toggle-sun-moon-morph","type":"registry:component","title":"Theme Toggle Sun Moon Morph","description":"A dark-mode switch whose thumb morphs from a sun into a crescent moon through a real SVG mask, while the track shifts from drifting clouds to twinkling stars. Controlled or uncontrolled, no dependencies.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/theme-toggle-sun-moon-morph.tsx","content":"\"use client\";\n\nimport { useId, useState } from \"react\";\nimport type { ButtonHTMLAttributes, CSSProperties, MouseEvent } from \"react\";\n\nexport interface ThemeToggleSunMoonMorphProps\n  extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, \"onChange\" | \"role\" | \"children\" | \"type\"> {\n  /** Controlled state. true = dark (moon). */\n  checked?: boolean;\n  /** Initial state when uncontrolled. */\n  defaultChecked?: boolean;\n  /** Fires with the new dark boolean. */\n  onChange?: (dark: boolean) => void;\n  /** Height of the pill in px. The pill is 1.78x as wide. */\n  size?: number;\n  /** Accessible name. */\n  label?: string;\n}\n\nconst TSM_CSS = [\n  \".tsm{position:relative;display:inline-block;flex:none;padding:0;margin:0;border:0;background:none;cursor:pointer;border-radius:9999px;line-height:1;-webkit-tap-highlight-color:transparent;touch-action:manipulation;--tsm-x:0em;--tsm-s:1}\",\n  \".tsm[data-dark='true']{--tsm-x:.78em}\",\n  \".tsm[data-press='true']{--tsm-s:.9}\",\n  \".tsm:disabled{cursor:not-allowed;opacity:.5;filter:saturate(.6)}\",\n  \".tsm:focus-visible{outline:2px solid #4d8dff;outline-offset:3px}\",\n  \".tsm-track{position:absolute;inset:0;overflow:hidden;border-radius:9999px;box-shadow:inset 0 .04em .1em rgba(0,0,0,.32),0 0 0 1px rgba(127,127,127,.38)}\",\n  \".tsm-sky{position:absolute;inset:0;transition:opacity 700ms ease}\",\n  \".tsm-sky-day{background:linear-gradient(180deg,#4fb2ff 0%,#9fd9ff 100%)}\",\n  \".tsm-sky-night{opacity:0;background:linear-gradient(180deg,#04061a 0%,#1b2457 100%)}\",\n  \".tsm[data-dark='true'] .tsm-sky-night{opacity:1}\",\n  \".tsm-clouds{position:absolute;inset:0;transition:transform 720ms cubic-bezier(.5,0,.25,1),opacity 480ms ease}\",\n  \".tsm[data-dark='true'] .tsm-clouds{transform:translateY(.55em);opacity:0}\",\n  \".tsm-cloud{position:absolute;display:block;animation:tsm-drift 7s ease-in-out infinite alternate}\",\n  \".tsm-stars{position:absolute;inset:0}\",\n  \".tsm-star{position:absolute;display:block;transform:scale(0) rotate(-40deg);opacity:0;transition:transform 520ms cubic-bezier(.34,1.56,.5,1),opacity 260ms ease}\",\n  \".tsm[data-dark='true'] .tsm-star{transform:scale(1) rotate(0deg);opacity:1;transition-delay:var(--tsm-sd)}\",\n  \".tsm-star svg{display:block;width:100%;height:100%;animation:tsm-twinkle 2.6s ease-in-out infinite;animation-delay:var(--tsm-td)}\",\n  \".tsm-thumb{position:absolute;left:.11em;top:.11em;width:.78em;height:.78em;transform:translateX(var(--tsm-x)) scale(var(--tsm-s));transition:transform 640ms cubic-bezier(.34,1.45,.5,1)}\",\n  \".tsm[data-press='true'] .tsm-thumb{transition-duration:180ms;transition-timing-function:ease-out}\",\n  \".tsm-glow{position:absolute;inset:-.1em;border-radius:9999px;transition:opacity 600ms ease}\",\n  \".tsm-glow-day{background:radial-gradient(closest-side,rgba(255,240,170,.75),rgba(255,214,90,0))}\",\n  \".tsm-glow-night{opacity:0;background:radial-gradient(closest-side,rgba(180,198,255,.42),rgba(150,170,255,0))}\",\n  \".tsm[data-dark='true'] .tsm-glow-day{opacity:0}\",\n  \".tsm[data-dark='true'] .tsm-glow-night{opacity:1}\",\n  \".tsm-icon{position:absolute;inset:0;width:100%;height:100%;overflow:visible;transition:transform 820ms cubic-bezier(.34,1.3,.5,1)}\",\n  \".tsm[data-dark='true'] .tsm-icon{transform:rotate(-24deg)}\",\n  \".tsm-icon *{transform-box:fill-box}\",\n  \".tsm-rays{transform-box:view-box;transform-origin:24px 24px;transition:transform 900ms cubic-bezier(.3,1.2,.4,1)}\",\n  \".tsm:hover:not([data-dark='true']) .tsm-rays{transform:rotate(22.5deg)}\",\n  \".tsm-ray{transform-box:view-box;transform-origin:24px 24px;transform:rotate(var(--r)) scaleY(1);opacity:1;transition:transform 420ms cubic-bezier(.34,1.5,.5,1) var(--tsm-in),opacity 220ms ease var(--tsm-in)}\",\n  \".tsm[data-dark='true'] .tsm-ray{transform:rotate(var(--r)) scaleY(.15);opacity:0;transition:transform 300ms cubic-bezier(.5,0,.75,0) var(--tsm-out),opacity 200ms ease var(--tsm-out)}\",\n  \".tsm-core{transform-origin:50% 50%;transform:scale(.667);transition:transform 640ms cubic-bezier(.34,1.35,.5,1)}\",\n  \".tsm[data-dark='true'] .tsm-core{transform:scale(1)}\",\n  \".tsm-sun,.tsm-moon{transition:opacity 480ms ease}\",\n  \".tsm-moon{opacity:0}\",\n  \".tsm[data-dark='true'] .tsm-sun{opacity:0}\",\n  \".tsm[data-dark='true'] .tsm-moon{opacity:1}\",\n  \".tsm-bite{transform:translate(15px,-15px);transition:transform 560ms cubic-bezier(.55,0,.3,1)}\",\n  \".tsm[data-dark='true'] .tsm-bite{transform:translate(0,0);transition-delay:140ms}\",\n  \".tsm-crater{opacity:0;transition:opacity 400ms ease}\",\n  \".tsm[data-dark='true'] .tsm-crater{opacity:1;transition-delay:480ms}\",\n  \".tsm-ring{position:absolute;inset:0;border-radius:9999px;border:.03em solid rgba(255,196,70,.95);pointer-events:none;animation:tsm-ring 720ms cubic-bezier(.2,.7,.3,1) forwards}\",\n  \".tsm[data-dark='true'] .tsm-ring{border-color:rgba(190,206,255,.95)}\",\n  \"@keyframes tsm-drift{from{transform:translateX(-.05em)}to{transform:translateX(.06em)}}\",\n  \"@keyframes tsm-twinkle{0%,100%{opacity:1;transform:scale(1)}50%{opacity:.45;transform:scale(.7)}}\",\n  \"@keyframes tsm-ring{from{transform:scale(1);opacity:.9}to{transform:scale(2.3);opacity:0}}\",\n  \"@media (prefers-reduced-motion:reduce){.tsm *,.tsm{animation:none!important;transition-duration:200ms!important;transition-delay:0s!important;transition-timing-function:ease!important}.tsm-ring{display:none}.tsm-rays,.tsm:hover:not([data-dark='true']) .tsm-rays{transform:none}}\",\n].join(\"\\n\");\n\nconst TSM_RAYS = [0, 45, 90, 135, 180, 225, 270, 315];\nconst TSM_STARS: { x: number; y: number; s: number }[] = [\n  { x: 0.2, y: 0.2, s: 0.11 },\n  { x: 0.48, y: 0.6, s: 0.08 },\n  { x: 0.33, y: 0.76, s: 0.055 },\n  { x: 0.6, y: 0.2, s: 0.065 },\n  { x: 0.12, y: 0.6, s: 0.065 },\n  { x: 0.74, y: 0.47, s: 0.05 },\n  { x: 0.46, y: 0.33, s: 0.045 },\n];\nconst TSM_SPARKLE = \"M5 0L6.1 3.9L10 5L6.1 6.1L5 10L3.9 6.1L0 5L3.9 3.9Z\";\nconst TSM_CLOUD = \"M7 16a6 6 0 0 1 .8-11.9A9 9 0 0 1 24.6 3.2A7 7 0 0 1 34.5 8.8 4.6 4.6 0 0 1 33.5 16Z\";\n\nexport function ThemeToggleSunMoonMorph({\n  checked,\n  defaultChecked = false,\n  onChange,\n  size = 36,\n  label = \"Dark mode\",\n  className,\n  style,\n  onClick,\n  onPointerDown,\n  onPointerUp,\n  onPointerLeave,\n  onPointerCancel,\n  disabled,\n  ...rest\n}: ThemeToggleSunMoonMorphProps) {\n  const [inner, setInner] = useState(defaultChecked);\n  const [press, setPress] = useState(false);\n  const [burst, setBurst] = useState(0);\n  const uid = useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const dark = checked ?? inner;\n  const px = Math.min(256, Math.max(16, size));\n  const hit = \"calc(max(0px, (40px - 1em) / 2) * -1)\";\n\n  function toggle(e: MouseEvent<HTMLButtonElement>) {\n    onClick?.(e);\n    if (e.defaultPrevented) return;\n    const next = !dark;\n    if (checked === undefined) setInner(next);\n    setBurst((n) => n + 1);\n    onChange?.(next);\n  }\n\n  return (\n    <>\n      <style>{TSM_CSS}</style>\n      <button\n        {...rest}\n        type=\"button\"\n        role=\"switch\"\n        aria-checked={dark}\n        aria-label={label}\n        disabled={disabled}\n        data-dark={dark ? \"true\" : \"false\"}\n        data-press={press && !disabled ? \"true\" : \"false\"}\n        className={\"tsm\" + (className ? \" \" + className : \"\")}\n        style={{ fontSize: px + \"px\", width: \"1.78em\", height: \"1em\", ...style }}\n        onClick={toggle}\n        onPointerDown={(e) => {\n          setPress(true);\n          onPointerDown?.(e);\n        }}\n        onPointerUp={(e) => {\n          setPress(false);\n          onPointerUp?.(e);\n        }}\n        onPointerLeave={(e) => {\n          setPress(false);\n          onPointerLeave?.(e);\n        }}\n        onPointerCancel={(e) => {\n          setPress(false);\n          onPointerCancel?.(e);\n        }}\n      >\n        <span aria-hidden=\"true\" style={{ position: \"absolute\", inset: hit }} />\n        <span aria-hidden=\"true\" className=\"tsm-track\">\n          <span className=\"tsm-sky tsm-sky-day\" />\n          <span className=\"tsm-sky tsm-sky-night\" />\n          <span className=\"tsm-clouds\">\n            <span className=\"tsm-cloud\" style={{ right: \".12em\", bottom: \".05em\", width: \".52em\", height: \".21em\" }}>\n              <svg viewBox=\"0 0 40 16\" width=\"100%\" height=\"100%\" style={{ display: \"block\" }}>\n                <path d={TSM_CLOUD} fill=\"#ffffff\" />\n              </svg>\n            </span>\n            <span\n              className=\"tsm-cloud\"\n              style={{ right: \".56em\", bottom: \"-.03em\", width: \".36em\", height: \".15em\", opacity: 0.7, animationDirection: \"alternate-reverse\", animationDuration: \"9s\" }}\n            >\n              <svg viewBox=\"0 0 40 16\" width=\"100%\" height=\"100%\" style={{ display: \"block\" }}>\n                <path d={TSM_CLOUD} fill=\"#ffffff\" />\n              </svg>\n            </span>\n          </span>\n          <span className=\"tsm-stars\">\n            {TSM_STARS.map((st, i) => (\n              <span\n                key={i}\n                className=\"tsm-star\"\n                style={\n                  {\n                    left: st.x + \"em\",\n                    top: st.y + \"em\",\n                    width: st.s + \"em\",\n                    height: st.s + \"em\",\n                    \"--tsm-sd\": 260 + i * 70 + \"ms\",\n                    \"--tsm-td\": i * 0.37 + \"s\",\n                  } as CSSProperties\n                }\n              >\n                <svg viewBox=\"0 0 10 10\">\n                  <path d={TSM_SPARKLE} fill=\"#eef2ff\" />\n                </svg>\n              </span>\n            ))}\n          </span>\n        </span>\n        <span aria-hidden=\"true\" className=\"tsm-thumb\">\n          <span className=\"tsm-glow tsm-glow-day\" />\n          <span className=\"tsm-glow tsm-glow-night\" />\n          {burst > 0 ? <span key={burst} className=\"tsm-ring\" /> : null}\n          <svg className=\"tsm-icon\" viewBox=\"0 0 48 48\">\n            <defs>\n              <linearGradient id={\"tsmS\" + uid} x1=\"0.2\" y1=\"0\" x2=\"0.8\" y2=\"1\">\n                <stop offset=\"0\" stopColor=\"#ffe27a\" />\n                <stop offset=\"1\" stopColor=\"#ff9a1f\" />\n              </linearGradient>\n              <linearGradient id={\"tsmM\" + uid} x1=\"0.1\" y1=\"0\" x2=\"0.9\" y2=\"1\">\n                <stop offset=\"0\" stopColor=\"#fbfcff\" />\n                <stop offset=\"1\" stopColor=\"#b9c4f2\" />\n              </linearGradient>\n              <mask id={\"tsmK\" + uid} maskUnits=\"userSpaceOnUse\" x=\"-8\" y=\"-8\" width=\"64\" height=\"64\">\n                <rect x=\"-8\" y=\"-8\" width=\"64\" height=\"64\" fill=\"#fff\" />\n                <circle className=\"tsm-bite\" cx=\"33\" cy=\"15\" r=\"12\" fill=\"#000\" />\n              </mask>\n            </defs>\n            <g className=\"tsm-rays\">\n              {TSM_RAYS.map((deg, i) => (\n                <line\n                  key={deg}\n                  className=\"tsm-ray\"\n                  x1=\"24\"\n                  y1=\"3\"\n                  x2=\"24\"\n                  y2=\"8\"\n                  stroke=\"#ffb23a\"\n                  strokeWidth=\"3.2\"\n                  strokeLinecap=\"round\"\n                  style={\n                    {\n                      \"--r\": deg + \"deg\",\n                      \"--tsm-in\": 280 + i * 24 + \"ms\",\n                      \"--tsm-out\": i * 16 + \"ms\",\n                    } as CSSProperties\n                  }\n                />\n              ))}\n            </g>\n            <g mask={\"url(#tsmK\" + uid + \")\"}>\n              <g className=\"tsm-core\">\n                <circle className=\"tsm-sun\" cx=\"24\" cy=\"24\" r=\"15\" fill={\"url(#tsmS\" + uid + \")\"} />\n                <circle className=\"tsm-moon\" cx=\"24\" cy=\"24\" r=\"15\" fill={\"url(#tsmM\" + uid + \")\"} />\n                <g className=\"tsm-crater\" fill=\"#8e9bd6\" fillOpacity=\"0.4\">\n                  <circle cx=\"15.5\" cy=\"27\" r=\"2.8\" />\n                  <circle cx=\"21\" cy=\"35\" r=\"1.8\" />\n                  <circle cx=\"13.5\" cy=\"19.5\" r=\"1.6\" />\n                </g>\n              </g>\n            </g>\n          </svg>\n        </span>\n      </button>\n    </>\n  );\n}\n","type":"registry:component","target":"components/viberdy/theme-toggle-sun-moon-morph.tsx"}],"meta":{"tier":"free","tags":["theme","toggle","sun","moon","dark-mode","switch","svg","morph"],"href":"https://viberdy.dev/library/theme-toggle-sun-moon-morph","aiPrompt":"Create a single-file React component named ThemeToggleSunMoonMorph (\"use client\", React only, no animation library, no Tailwind needed: it injects one plain <style> element with a .tsm class set). It is a pill-shaped dark-mode switch whose thumb is one SVG that morphs from a sun into a crescent moon while the track turns from day sky to night sky.\n\nProps (all optional, plus every other button attribute passed through; onClick runs first and can preventDefault): checked (controlled, true = dark), defaultChecked false, onChange(dark: boolean), size 36 (pill height in px, clamped 16-256; width is 1.78 x height), label \"Dark mode\" (aria-label), className, style, disabled.\n\nStructure: a button type=\"button\" role=\"switch\" aria-checked={dark} aria-label, data-dark and data-press attributes drive all CSS. font-size = size px so everything inside is sized in em. Width 1.78em, height 1em, border-radius full, no border, touch-action manipulation. A hidden hit span extends the target to at least 40px: inset: calc(max(0px,(40px - 1em)/2) * -1). Focus-visible: 2px solid #4d8dff outline, offset 3px (visible on light and dark pages).\n\nTrack (aria-hidden, overflow hidden, inset shadow plus a 1px ring of rgba(127,127,127,.38)): two stacked sky layers cross-faded over 700ms: day linear-gradient(180deg,#4fb2ff,#9fd9ff), night linear-gradient(180deg,#04061a,#1b2457). Two white cloud SVG shapes (viewBox 0 0 40 16, one .52em wide at right .12em bottom .05em, one .36em wide at 70% opacity) drift +-.05em on a 7s/9s alternate loop and sink .55em while fading out at night (720ms). Seven four-point sparkle stars (positions as fractions of em, sizes .045-.11em, fill #eef2ff) scale in from 0 with a -40deg rotation, staggered 260ms + 70ms per star, then twinkle (opacity 1 to .45, scale 1 to .7, 2.6s, staggered).\n\nThumb: .78em square at left/top .11em. transform: translateX(0 or .78em) scale(1; .9 while pressed); 640ms cubic-bezier(.34,1.45,.5,1) (small overshoot), pressed 180ms ease-out (a squish, never a stretch, so the icon stays round). Behind the icon two radial glows cross-fade: amber rgba(255,196,70,.6) by day, periwinkle rgba(180,198,255,.42) at night. On each toggle a ring (border .03em amber or periwinkle) scales 1 to 2.3 and fades over 720ms (keyed remount).\n\nIcon SVG, viewBox 0 0 48 48, overflow visible, rotates 0 to -24deg (820ms cubic-bezier(.34,1.3,.5,1)). Eight rays: lines from y 3 to 8 about (24,24), stroke #ffb23a width 3.2, round caps, each rotated k*45deg. Going dark they scaleY(.15) and fade over 300ms, staggered 16ms each; coming back they extend with a 420ms overshoot, delayed 280ms + 24ms each. By day, hovering rotates the whole ray group 22.5deg. Core: circle r15 at the centre scaled .667 by day and 1 at night (640ms overshoot), with two stacked fills cross-faded (sun gradient #ffe27a to #ff9a1f, moon gradient #fbfcff to #b9c4f2) and three faint crater circles (#8e9bd6 at 40%) fading in at night after 480ms. The crescent comes from a real SVG mask on the core group: a white rect plus a black \"bite\" circle r12 which sits at (33,15) when dark and is translated (15,-15) off the core when light (560ms ease-in-out, 140ms delay when going in).\n\nBehaviour: native button, Space and Enter toggle; controlled or uncontrolled. Disabled is 50% opacity with not-allowed cursor.\nprefers-reduced-motion: no keyframe animation (no drift, twinkle or ring), no ray spin, every transition 200ms plain ease with no delay or overshoot.\nUse ids from useId() stripped to alphanumerics for the gradients and mask. Never paint with any site colour token.","license":"https://viberdy.dev/license"}}