Sol y Luna
GratisControles · Interruptor de tema donde el sol recoge sus ocho rayos hacia el centro y cede el lugar a la luna, animando stroke-dashoffset en vez de cruzar dos íconos.
Llevalo a tu proyecto
Pegá el código en tu proyecto, o pegá el prompt en Claude, Cursor, v0 o Lovable y que lo agregue por vos.
"use client";
import { useState } from "react";
export type SolLunaProps = {
/** Estado controlado: true muestra la luna. */
dark?: boolean;
onChange?: (dark: boolean) => void;
/** Etiquetas accesibles de cada estado. */
labels?: { toDark: string; toLight: string };
className?: string;
};
/**
* Interruptor de tema donde el sol no se cambia por una luna: recoge sus rayos
* hacia el centro y cede el lugar. Los rayos son ocho líneas con `pathLength=1`
* que se retraen animando `stroke-dashoffset`, así que el gesto es una sola
* transición CSS y no hay dos íconos peleando por el mismo espacio.
*
* Funciona controlado (`dark` + `onChange`) o solo, con su propio estado.
*/
export function SolLuna({ dark, onChange, labels, className = "" }: SolLunaProps) {
const [own, setOwn] = useState(false);
const isDark = dark ?? own;
const texts = labels ?? { toDark: "Cambiar al tema oscuro", toLight: "Cambiar al tema claro" };
const label = isDark ? texts.toLight : texts.toDark;
function toggle() {
const next = !isDark;
if (dark === undefined) setOwn(next);
onChange?.(next);
}
return (
<button
type="button"
onClick={toggle}
aria-label={label}
title={label}
data-dark={isDark ? "" : undefined}
className={`solluna ${className}`}
>
<style>{CSS}</style>
<svg className="solluna__icon solluna__sun" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="4.6" fill="currentColor" stroke="none" />
{Array.from({ length: 8 }, (_, i) => {
const a = (i / 8) * Math.PI * 2;
return (
<line
key={i}
className="solluna__ray"
pathLength={1}
x1={12 + Math.cos(a) * 7.6}
y1={12 + Math.sin(a) * 7.6}
x2={12 + Math.cos(a) * 9.9}
y2={12 + Math.sin(a) * 9.9}
/>
);
})}
</svg>
<svg className="solluna__icon solluna__moon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M20 14.3A8.2 8.2 0 0 1 9.7 4a8.4 8.4 0 1 0 10.3 10.3Z" />
</svg>
</button>
);
}
const CSS = `
.solluna {
--dur: 0.7s;
--ease: cubic-bezier(0.22, 1, 0.36, 1);
position: relative;
display: grid;
place-items: center;
width: 2.25rem;
height: 2.25rem;
border: 0;
border-radius: 999px;
background: none;
padding: 0;
color: currentColor;
cursor: pointer;
opacity: 0.65;
transition: opacity var(--dur) var(--ease);
}
.solluna:is(:hover, :focus-visible) {
opacity: 1;
outline: none;
}
.solluna__icon {
position: absolute;
width: 1.15rem;
height: 1.15rem;
fill: none;
stroke: currentColor;
stroke-width: 1.7;
stroke-linecap: round;
stroke-linejoin: round;
transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.solluna__moon {
opacity: 0;
transform: rotate(-35deg) scale(0.7);
}
.solluna[data-dark] .solluna__sun {
opacity: 0;
transform: rotate(35deg) scale(0.7);
}
.solluna[data-dark] .solluna__moon {
opacity: 1;
transform: rotate(0) scale(1);
}
.solluna__ray {
stroke-dasharray: 1;
stroke-dashoffset: 0;
transition: stroke-dashoffset var(--dur) var(--ease);
}
.solluna[data-dark] .solluna__ray {
stroke-dashoffset: 1;
}
@media (prefers-reduced-motion: reduce) {
.solluna__icon,
.solluna__ray { transition: none; }
}
`;
Instalar con la CLI de shadcnAvanzado
Si tu proyecto ya usa shadcn, la CLI escribe el archivo y instala las dependencias sola.
npx shadcn@latest add @interfaz/sol-luna