1280px
설치
이 명령 한 줄이면 필요한 파일과 패키지가 함께 설치돼요.
터미널터미널터미널bash
npx shadcn@latest add https://dsui.vercel.app/r/switch.json파일
components/arc/switch/switch.module.csscomponents/arc/switch/switch.tsx
npm 패키지
@radix-ui/react-switch@^1.4.1motion@^14.2.0
- v1.1.0리퀴드 글래스 변형(variant="liquid-glass")을 더했어요.
- v1.0.0처음 공개했어요.
switch.module.cssswitch.module.cssswitch.module.csscss
.switch { display: inline-flex; min-height: var(--control-height-md); align-items: center; gap: var(--space-3); padding: 0; border: 0; color: var(--foreground); background: transparent; font: inherit; font-size: var(--text-sm); cursor: pointer; -webkit-tap-highlight-color: transparent; }
.switch:disabled { cursor: not-allowed; opacity: .5; }
/* Fixed box: the thumb moves inside it with transforms, so nothing around the switch shifts. The track crossfades between the two fills. */
.track { position: relative; box-sizing: border-box; display: flex; flex: 0 0 auto; width: 42px; height: 24px; align-items: center; padding: 3px; border-radius: var(--radius-pill); background: var(--control-track); isolation: isolate; transition: background-color var(--duration-fast) var(--ease-standard), box-shadow var(--duration-fast) var(--ease-standard), transform var(--duration-spring) var(--ease-spring); }
.track::before { content: ""; position: absolute; inset: 0; z-index: -1; border-radius: inherit; background: var(--control-on); opacity: 0; transition: opacity var(--duration-standard) var(--ease-standard); }
.switch[data-state="checked"] .track::before { opacity: 1; }
.thumb { flex: 0 0 auto; width: 18px; height: 18px; border-radius: var(--radius-pill); background: var(--control-thumb); box-shadow: var(--control-thumb-shadow); will-change: transform; transition: background-color var(--duration-standard) var(--ease-standard); }
.switch[data-state="checked"] .thumb { background: var(--control-thumb-on); }
@media (hover: hover) and (pointer: fine) {
.switch[data-state="unchecked"]:hover:not(:disabled) .track { background: var(--control-track-hover); }
.switch:hover:not(:disabled) .track { transform: scale(1.06); }
.switch[data-state="checked"]:hover:not(:disabled) .track { box-shadow: var(--shadow-accent-raised); }
}
.switch:active:not(:disabled) .track { transform: scale(.94); }
/* Liquid glass: the layer paints the track and shapes the thumb as a lens, so neither has a fill or a shadow of its own. */
.glass .track { background: var(--liquid-glass-tint, color-mix(in oklab, var(--surface) 66%, transparent)) !important; box-shadow: none !important; }
/* The thumb keeps a little ink under its lens, so on and off read at a glance even on a flat page. */
.glass .thumb { background: color-mix(in oklab, var(--foreground) 32%, transparent) !important; box-shadow: none !important; }
.glass[data-state="checked"] .thumb { background: var(--foreground) !important; }
.glass .track::before { content: none; }
.switch:focus-visible { outline: none; }
.label { line-height: var(--leading-body); }
@media (prefers-reduced-motion: reduce) { .track, .track::before, .thumb { transition: none; } .switch:hover .track, .switch:active .track { transform: none; } }
switch.tsxswitch.tsxswitch.tsxtsx
"use client";
import { forwardRef, useEffect, useRef, useState } from "react";
import type { ComponentPropsWithoutRef, ElementRef } from "react";
import * as SwitchPrimitive from "@radix-ui/react-switch";
import { animate, motion, useMotionValue } from "motion/react";
import { useReducedMotion } from "../lib/use-reduced-motion";
import type { Transition } from "motion/react";
import { motionTokens } from "../lib/motion-tokens";
import styles from "./switch.module.css";
import { playUiSound } from "../sound/play";
import { LiquidGlassLayer, liquidGlassHost } from "../liquid-glass/liquid-glass-layer";
export interface SwitchProps extends ComponentPropsWithoutRef<typeof SwitchPrimitive.Root> {
label?: string;
/** `liquid-glass` draws the track and thumb as glass: clear with a prism edge when on, frosted when off. */
variant?: "default" | "liquid-glass";
}
/** Track inner width (42 - 6 padding) minus the 18px thumb; keep in sync with switch.module.css. */
const size = 18;
const travel = 18;
/** How far the thumb widens toward the other side while pressed. */
const stretch = 5;
/** Critically damped: the thumb lands on its end without overshooting the state it reports. */
const glide: Transition = motionTokens.spring.settle;
export const Switch = forwardRef<ElementRef<typeof SwitchPrimitive.Root>, SwitchProps>(function Switch(
{ label, variant = "default", className, checked, defaultChecked, onCheckedChange, onPointerDown, onPointerUp, onPointerLeave, onPointerCancel, onKeyDown, onKeyUp, onBlur, ...props },
ref,
) {
const reduceMotion = useReducedMotion();
const [internal, setInternal] = useState(defaultChecked ?? false);
const [pressed, setPressed] = useState(false);
const on = checked ?? internal;
// A brief stretch along the travel, so the thumb reads as moving mass rather than a sliding dot.
const scaleX = useMotionValue(1);
const shown = useRef(on);
// A pointer or Space press already stretched the thumb, so its release should not add a second stretch on top.
const releasedAt = useRef(-Infinity);
useEffect(() => {
if (shown.current === on) return;
shown.current = on;
const fromPress = performance.now() - releasedAt.current < 250;
if (reduceMotion || fromPress) return;
const controls = animate(scaleX, [1, 1.16, 1], { duration: motionTokens.duration.moderate, times: [0, 0.4, 1], ease: ["easeOut", "easeInOut"] });
return () => controls.stop();
}, [on, reduceMotion, scaleX]);
const extra = pressed && !reduceMotion && !props.disabled ? stretch : 0;
const glass = variant === "liquid-glass";
const classes = [styles.switch, glass ? styles.glass : undefined, className].filter(Boolean).join(" ");
return (
<SwitchPrimitive.Root
{...props}
ref={ref}
checked={on}
onCheckedChange={next => { if (checked === undefined) setInternal(next); playUiSound(next ? "toggle-on" : "toggle-off"); onCheckedChange?.(next); }}
onPointerDown={event => { onPointerDown?.(event); if (event.button === 0) setPressed(true); }}
onPointerUp={event => { onPointerUp?.(event); if (pressed && !props.disabled) releasedAt.current = performance.now(); setPressed(false); }}
onPointerLeave={event => { onPointerLeave?.(event); setPressed(false); }}
onPointerCancel={event => { onPointerCancel?.(event); setPressed(false); }}
onKeyDown={event => { onKeyDown?.(event); if (event.key === " ") setPressed(true); }}
onKeyUp={event => { onKeyUp?.(event); if (pressed && !props.disabled) releasedAt.current = performance.now(); setPressed(false); }}
onBlur={event => { onBlur?.(event); setPressed(false); }}
className={classes}
aria-label={props["aria-label"] ?? label}
>
<span className={glass ? `${liquidGlassHost} ${styles.track}` : styles.track}>
{glass ? <LiquidGlassLayer preset="switch" restPreset="switchOff" resting={!on} lens="[data-glass-lens]" /> : null}
{/* The thumb stretches like a held finger and keeps its far edge anchored, then travels on a spring. */}
<motion.span className={styles.thumb} data-glass-lens={glass || undefined} style={{ scaleX }} initial={false} animate={{ x: on ? travel - extra : 0, width: size + extra }} transition={reduceMotion ? { duration: 0 } : { x: glide, width: motionTokens.spring.snappy }} />
</span>
{label ? <span className={styles.label}>{label}</span> : null}
</SwitchPrimitive.Root>
);
});
Switch.displayName = "Switch";
export default Switch;
사용된 항목
- 리퀴드 글래스컴포넌트
빛이 굴절하고 물결이 흐르는 유리 면이에요. 원하는 내용을 감싸거나 글자를 유리로 채울 수 있어요.
이 항목을 쓰는 조합
- 쿠키 동의컴포넌트
쿠키 종류별로 동의를 받고 나중에 다시 바꿀 수 있어요.
- 생성형 UI컴포넌트
AI 모델이 OpenUI나 json-render로 쓴 화면을 dsui 컴포넌트로 그려 주는 컴포넌트예요.
- 뉴스레터 구독블록
이메일을 받아 구독을 확인해 주고 지난 호를 겹쳐 보여 주는 가입 블록이에요.
- 요금제 비교블록
월간과 연간 결제를 바꿔 가며 두 요금제의 기능을 견줘 보는 블록이에요.
- 설정 패널블록
소리, 화면 모드, 시즌 테마, 글자 크기, 움직임을 한 서랍에 모은 설정 블록이에요. 언어나 계정 같은 묶음도 더할 수 있어요.
- 단계형 설정블록
설정을 두 단계로 보여 주는 블록이에요. 처음에는 소리, 화면, 언어 같은 묶음 목록이 나오고, 하나를 고르면 그 묶음의 화면이 오른쪽에서 밀려 들어와요.