1 Run the shadcn CLI
npx shadcn@latest add https://toggles.dev/r/light-switch 2 Import and use
import { LightSwitch } from "@/components/ui/light-switch";
export default function App() {
return <LightSwitch />;
} 1 Install the package
▾
npm install @theme-toggles/react pnpm add @theme-toggles/react yarn add @theme-toggles/react bun add @theme-toggles/react 2 Import and use
import { LightSwitch } from "@theme-toggles/react";
import "@theme-toggles/react/styles/light-switch.css";
export default function App() {
return <LightSwitch />;
} 1 Copy the component
import { type ButtonHTMLAttributes, type CSSProperties, useId } from "react";
export interface LightSwitchProps extends Omit<
ButtonHTMLAttributes<HTMLButtonElement>,
"children"
> {
duration?: number;
[key: `data-${string}`]: string | number | boolean | null | undefined;
}
export function LightSwitch({
duration = 350,
className,
type = "button",
title = "Toggle theme",
"aria-label": ariaLabel = "Toggle theme",
...props
}: LightSwitchProps) {
const toggleId = useId();
const clipPaddleId = `toggles.dev-light-switch-paddle-${toggleId}`;
return (
<button
type={type}
title={title}
aria-label={ariaLabel}
className={className}
{...props}
>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
style={
{
"--toggles-light-switch--duration": `${duration}ms`,
} as CSSProperties
}
>
<defs>
<clipPath id={clipPaddleId}>
<path
d={"M7 3h10v9H7Z"}
className="transition-[d,translate] duration-(--toggles-light-switch--duration) [transition-timing-function:cubic-bezier(0,0,0.15,1.25)] dark:[d:path('M7_12h10v9H7Z')] dark:not-supports-[d:path('M0_0')]:translate-y-[9px]"
/>
</clipPath>
</defs>
<rect
x={5}
y={1}
width={14}
height={22}
rx={2}
stroke={"currentColor"}
fill={"none"}
strokeWidth={1.5}
/>
<rect
x={7}
y={3}
width={10}
height={18}
rx={1}
stroke={"currentColor"}
fill={"none"}
strokeWidth={1}
/>
<rect
x={8}
y={4}
width={8}
height={16}
rx={0.5}
fill={"currentColor"}
clipPath={`url(#${clipPaddleId})`}
/>
</svg>
</button>
);
} 1 Install the package
▾
npm install @theme-toggles/svelte pnpm add @theme-toggles/svelte yarn add @theme-toggles/svelte bun add @theme-toggles/svelte 2 Import and use
<script>
import { LightSwitch } from "@theme-toggles/svelte";
import "@theme-toggles/svelte/styles/light-switch.css";
</script>
<LightSwitch /> 1 Copy the component
<script context="module" lang="ts">
let nextId = 0;
</script>
<script lang="ts">
import type { HTMLButtonAttributes } from "svelte/elements";
interface $$Props extends Omit<HTMLButtonAttributes, "children"> {
duration?: number;
ariaLabel?: string;
class?: string;
}
export let duration = 350;
export let type: HTMLButtonAttributes["type"] = "button";
export let title = "Toggle theme";
export let ariaLabel = "Toggle theme";
let className = "";
export { className as class };
const toggleId = `light-switch-${++nextId}`;
const clipPaddleId = `toggles.dev-light-switch-paddle-${toggleId}`;
</script>
<button
{type}
{title}
aria-label={ariaLabel}
class={className}
on:click
{...$$restProps}
>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
style={`--toggles-light-switch--duration: ${duration}ms`}
>
<defs>
<clipPath id={clipPaddleId}>
<path
d={"M7 3h10v9H7Z"}
class={"transition-[d,translate] duration-(--toggles-light-switch--duration) [transition-timing-function:cubic-bezier(0,0,0.15,1.25)] dark:[d:path('M7_12h10v9H7Z')] dark:not-supports-[d:path('M0_0')]:translate-y-[9px]"}
/>
</clipPath>
</defs>
<rect
x={5}
y={1}
width={14}
height={22}
rx={2}
stroke={"currentColor"}
fill={"none"}
stroke-width={1.5}
/>
<rect
x={7}
y={3}
width={10}
height={18}
rx={1}
stroke={"currentColor"}
fill={"none"}
stroke-width={1}
/>
<rect
x={8}
y={4}
width={8}
height={16}
rx={0.5}
fill={"currentColor"}
clip-path={`url(#${clipPaddleId})`}
/>
</svg>
</button> 1 Install the package
▾
npm install @theme-toggles/vue pnpm add @theme-toggles/vue yarn add @theme-toggles/vue bun add @theme-toggles/vue 2 Import and use
<script setup lang="ts">
import { LightSwitch } from "@theme-toggles/vue";
import "@theme-toggles/vue/styles/light-switch.css";
</script>
<template>
<LightSwitch />
</template> 1 Copy the component
<script lang="ts">
let nextId = 0;
</script>
<script setup lang="ts">
import { useAttrs } from "vue";
interface Props {
duration?: number;
type?: "button" | "submit" | "reset";
title?: string;
ariaLabel?: string;
}
const props = withDefaults(defineProps<Props>(), {
duration: 350,
type: "button",
title: "Toggle theme",
ariaLabel: "Toggle theme",
});
const attrs = useAttrs();
const toggleId = `light-switch-${++nextId}`;
const clipPaddleId = `toggles.dev-light-switch-paddle-${toggleId}`;
</script>
<template>
<button
v-bind="attrs"
:type="props.type"
:title="props.title"
:aria-label="props.ariaLabel"
>
<svg
width="1em"
height="1em"
viewBox="0 0 24 24"
aria-hidden="true"
:style="{ '--toggles-light-switch--duration': `${props.duration}ms` }"
>
<defs>
<clipPath :id="clipPaddleId">
<path
:d="'M7 3h10v9H7Z'"
:class="'transition-[d,translate] duration-(--toggles-light-switch--duration) [transition-timing-function:cubic-bezier(0,0,0.15,1.25)] dark:[d:path(\'M7_12h10v9H7Z\')] dark:not-supports-[d:path(\'M0_0\')]:translate-y-[9px]'"
/>
</clipPath>
</defs>
<rect
:x="5"
:y="1"
:width="14"
:height="22"
:rx="2"
:stroke="'currentColor'"
:fill="'none'"
:stroke-width="1.5"
/>
<rect
:x="7"
:y="3"
:width="10"
:height="18"
:rx="1"
:stroke="'currentColor'"
:fill="'none'"
:stroke-width="1"
/>
<rect
:x="8"
:y="4"
:width="8"
:height="16"
:rx="0.5"
:fill="'currentColor'"
:clip-path="`url(#${clipPaddleId})`"
/>
</svg>
</button>
</template>