Inner Moon Theme Toggle
Animated dark mode toggle component for React, Svelte, and Vue.
1Run the shadcn CLI
npx shadcn@latest add https://toggles.dev/r/inner-moon2Import and use
"use client";
import { useState } from "react";
import { InnerMoon } from "@/components/ui/inner-moon";
export default function App() {
const [toggled, setToggled] = useState(false);
return (
<>
{/* Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. */}
<InnerMoon
toggled={toggled}
onClick={() => setToggled((value) => !value)}
/>
</>
);
}1Install the package
▾
npm install @theme-toggles/reactpnpm add @theme-toggles/reactyarn add @theme-toggles/reactbun add @theme-toggles/react2Import and use
"use client";
import { useState } from "react";
import { InnerMoon } from "@theme-toggles/react";
import "@theme-toggles/react/styles/inner-moon.css";
export default function App() {
const [toggled, setToggled] = useState(false);
return (
<>
{/* Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. */}
<InnerMoon
toggled={toggled}
onClick={() => setToggled((value) => !value)}
/>
</>
);
}1Copy the component
import { type ButtonHTMLAttributes, type CSSProperties } from "react";
export interface InnerMoonProps extends Omit<
ButtonHTMLAttributes<HTMLButtonElement>,
"children"
> {
duration?: number;
/** Whether the toggle should render in its dark-theme state. */
toggled?: boolean;
[key: `data-${string}`]: string | number | boolean | null | undefined;
}
export function InnerMoon({
duration = 500,
toggled,
className,
type = "button",
title = "Toggle theme",
"aria-label": ariaLabel = "Toggle theme",
"aria-pressed": ariaPressed,
...props
}: InnerMoonProps) {
return (
<button
{...props}
type={type}
title={title}
aria-label={ariaLabel}
aria-pressed={toggled ?? ariaPressed}
className={[
className,
toggled === true ? "dark" : toggled === false ? "light" : undefined,
]
.filter(Boolean)
.join(" ")}
>
<svg
width="1em"
height="1em"
viewBox="0 0 32 32"
aria-hidden="true"
fill={"currentColor"}
style={
{ "--toggles-inner-moon--duration": `${duration}ms` } as CSSProperties
}
>
<path
d={
"M27.5 11.5v-7h-7L16 0l-4.5 4.5h-7v7L0 16l4.5 4.5v7h7L16 32l4.5-4.5h7v-7L32 16l-4.5-4.5zM16 25.4a9.39 9.39 0 1 1 0-18.8 9.39 9.39 0 1 1 0 18.8z"
}
className="origin-center motion-safe:transition-transform motion-safe:duration-(--toggles-inner-moon--duration) motion-safe:[transition-timing-function:cubic-bezier(0,0,0.15,1.25)] dark:rotate-180"
/>
<circle
cx={16}
cy={16}
r={7.6}
className="origin-center motion-safe:transition-transform motion-safe:[transition-timing-function:cubic-bezier(0.4,0,0.2,1)] motion-safe:[transition-duration:calc(var(--toggles-inner-moon--duration)/1.5)] dark:translate-x-[15%]"
/>
</svg>
</button>
);
}2Import and use
"use client";
import { useState } from "react";
// Update this path to where you copied the component.
import { InnerMoon } from "./InnerMoon";
export default function App() {
const [toggled, setToggled] = useState(false);
return (
<>
{/* Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. */}
<InnerMoon
toggled={toggled}
onClick={() => setToggled((value) => !value)}
/>
</>
);
}1Install the package
▾
npm install @theme-toggles/sveltepnpm add @theme-toggles/svelteyarn add @theme-toggles/sveltebun add @theme-toggles/svelte2Import and use
<script>
import { InnerMoon } from "@theme-toggles/svelte";
import "@theme-toggles/svelte/styles/inner-moon.css";
let toggled = false;
</script>
<!-- Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. -->
<InnerMoon {toggled} on:click={() => (toggled = !toggled)} />1Copy the component
<script lang="ts">
import type { HTMLButtonAttributes } from "svelte/elements";
interface $$Props extends Omit<HTMLButtonAttributes, "children"> {
duration?: number;
toggled?: boolean;
ariaLabel?: string;
class?: string;
}
export let duration = 500;
export let toggled: boolean | undefined = undefined;
export let type: HTMLButtonAttributes["type"] = "button";
export let title = "Toggle theme";
export let ariaLabel = "Toggle theme";
let className = "";
export { className as class };
</script>
<button
{...$$restProps}
{type}
{title}
aria-label={ariaLabel}
aria-pressed={toggled ?? $$restProps["aria-pressed"]}
class={[
className,
toggled === true ? "dark" : toggled === false ? "light" : undefined,
]
.filter(Boolean)
.join(" ")}
on:click
>
<svg
width="1em"
height="1em"
viewBox="0 0 32 32"
aria-hidden="true"
fill={"currentColor"}
style={`--toggles-inner-moon--duration: ${duration}ms`}
>
<path
d={"M27.5 11.5v-7h-7L16 0l-4.5 4.5h-7v7L0 16l4.5 4.5v7h7L16 32l4.5-4.5h7v-7L32 16l-4.5-4.5zM16 25.4a9.39 9.39 0 1 1 0-18.8 9.39 9.39 0 1 1 0 18.8z"}
class={"origin-center motion-safe:transition-transform motion-safe:duration-(--toggles-inner-moon--duration) motion-safe:[transition-timing-function:cubic-bezier(0,0,0.15,1.25)] dark:rotate-180"}
/>
<circle
cx={16}
cy={16}
r={7.6}
class={"origin-center motion-safe:transition-transform motion-safe:[transition-timing-function:cubic-bezier(0.4,0,0.2,1)] motion-safe:[transition-duration:calc(var(--toggles-inner-moon--duration)/1.5)] dark:translate-x-[15%]"}
/>
</svg>
</button>2Import and use
<script>
// Update this path to where you copied the component.
import InnerMoon from "./InnerMoon.svelte";
let toggled = false;
</script>
<!-- Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. -->
<InnerMoon {toggled} on:click={() => (toggled = !toggled)} />1Install the package
▾
npm install @theme-toggles/vuepnpm add @theme-toggles/vueyarn add @theme-toggles/vuebun add @theme-toggles/vue2Import and use
<script setup lang="ts">
import { ref } from "vue";
import { InnerMoon } from "@theme-toggles/vue";
import "@theme-toggles/vue/styles/inner-moon.css";
const toggled = ref(false);
</script>
<template>
<!-- Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. -->
<InnerMoon :toggled="toggled" @click="toggled = !toggled" />
</template>1Copy the component
<script setup lang="ts">
import { useAttrs } from "vue";
export interface Props {
duration?: number;
toggled?: boolean;
type?: "button" | "submit" | "reset";
title?: string;
ariaLabel?: string;
}
const props = withDefaults(defineProps<Props>(), {
duration: 500,
type: "button",
title: "Toggle theme",
ariaLabel: "Toggle theme",
});
const attrs = useAttrs();
</script>
<template>
<button
v-bind="attrs"
:type="props.type"
:title="props.title"
:aria-label="props.ariaLabel"
:aria-pressed="props.toggled ?? attrs['aria-pressed']"
:class="
props.toggled === true
? 'dark'
: props.toggled === false
? 'light'
: undefined
"
>
<svg
width="1em"
height="1em"
viewBox="0 0 32 32"
aria-hidden="true"
:fill="'currentColor'"
:style="{ '--toggles-inner-moon--duration': `${props.duration}ms` }"
>
<path
:d="'M27.5 11.5v-7h-7L16 0l-4.5 4.5h-7v7L0 16l4.5 4.5v7h7L16 32l4.5-4.5h7v-7L32 16l-4.5-4.5zM16 25.4a9.39 9.39 0 1 1 0-18.8 9.39 9.39 0 1 1 0 18.8z'"
:class="'origin-center motion-safe:transition-transform motion-safe:duration-(--toggles-inner-moon--duration) motion-safe:[transition-timing-function:cubic-bezier(0,0,0.15,1.25)] dark:rotate-180'"
/>
<circle
:cx="16"
:cy="16"
:r="7.6"
:class="'origin-center motion-safe:transition-transform motion-safe:[transition-timing-function:cubic-bezier(0.4,0,0.2,1)] motion-safe:[transition-duration:calc(var(--toggles-inner-moon--duration)/1.5)] dark:translate-x-[15%]'"
/>
</svg>
</button>
</template>2Import and use
<script setup lang="ts">
import { ref } from "vue";
// Update this path to where you copied the component.
import InnerMoon from "./InnerMoon.vue";
const toggled = ref(false);
</script>
<template>
<!-- Alternatively, omit `toggled` when your app applies `dark` or `light`
to a root element—the toggle follows that class automatically. -->
<InnerMoon :toggled="toggled" @click="toggled = !toggled" />
</template>