# Switch
URL: /lynx/components/switch
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/lynx/components/switch.mdx
특정 옵션이나 설정을 켜거나 끌 수 있도록 돕는 토글 컴포넌트입니다.
Lynx Engine 최소 버전: 3.6
사용 가능 버전: @seed-design/lynx-react@0.1.0, @seed-design/lynx-css@0.1.0
## Preview
```tsx
import { root } from "@lynx-js/react";
import { Switch, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
);
}
root.render();
```
## Installation
- npm: npx @seed-design/cli add ui:switch
- pnpm: pnpm dlx @seed-design/cli add ui:switch
- yarn: yarn dlx @seed-design/cli add ui:switch
- bun: bun x @seed-design/cli add ui:switch
## Props
### `Switch`
### `Switchmark`
## Examples
### Sizes
```tsx
import { root } from "@lynx-js/react";
import { Switch, VStack, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function SwitchItem({ size, label }: { size: Switch.RootProps["size"]; label: string }) {
return (
{label}
);
}
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
);
}
root.render();
```
### Tones
#### Brand
```tsx
import { root } from "@lynx-js/react";
import { Switch, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
Brand
);
}
root.render();
```
#### Neutral
```tsx
import { root } from "@lynx-js/react";
import { Switch, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
Neutral
);
}
root.render();
```
### Long Label
```tsx
import { root } from "@lynx-js/react";
import { Switch, VStack, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
const label =
"Consequat ut veniam aliqua deserunt occaecat enim occaecat veniam et et cillum nulla officia incididunt incididunt. Sint laboris labore occaecat fugiat culpa voluptate ullamco in elit dolore exercitation nulla.";
function SwitchItem({ size }: { size: Switch.RootProps["size"] }) {
return (
{label}
);
}
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
);
}
root.render();
```
### Disabled
```tsx
import { root, useState } from "@lynx-js/react";
import { Switch, VStack, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function SwitchItem({
disabled,
label,
tone = "brand",
defaultChecked = false,
}: {
disabled: boolean;
label: string;
tone?: Switch.RootProps["tone"];
defaultChecked?: boolean;
}) {
return (
{label}
);
}
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [disabled, setDisabled] = useState(true);
return (
Disable switches
);
}
root.render();
```
### Listening to Value Changes
`onCheckedChange`로 스위치의 선택 상태 변경을 감지할 수 있습니다.
```tsx
import { root, useState } from "@lynx-js/react";
import { Switch, VStack, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [count, setCount] = useState(0);
const [lastValue, setLastValue] = useState(null);
return (
{
setCount((previous) => previous + 1);
setLastValue(checked);
}}
>
Click me
onCheckedChange called: {count} times, last value: {JSON.stringify(lastValue)}
);
}
root.render();
```
### Use Cases
#### Using `Switchmark`
`Switch.Control`과 `Switch.Thumb`을 직접 조합하면 레이블 배치를 자유롭게 구성할 수 있습니다.
```tsx
import { root } from "@lynx-js/react";
import { HStack, Switch, VStack, useSeedClassName } from "@seed-design/lynx-react";
import "./styles";
function CustomSwitch({
label,
defaultChecked = false,
}: {
label: string;
defaultChecked?: boolean;
}) {
return (
{label}
);
}
function Root() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
);
}
root.render();
```
## 웹 버전과의 차이
Lynx `Switch`는 React `Switch`와 다음과 같은 차이가 있습니다.
- **이벤트 핸들링**: `onChange` 대신 `onCheckedChange`만 노출합니다. tap 핸들러는 Switch 내부에서 소유합니다.
- **렌더링 요소**: HTML `
## Lynx 미지원 기능
현재 Lynx 플랫폼 제약으로 다음 기능이 지원되지 않습니다.
### 런타임 모델 차이로 제외
| 기능 | 웹 대응 | 설명 |
| ----------------------------------------- | ------------------------- | ---------------------------- |
| `SwitchHiddenInput` | `` | Lynx에 HTML form 제출 모델 없음 |
| `name` / `value` / `required` / `invalid` | form field props | Lynx에 native form 제출 모델 없음 |
| `focus` / `focusVisible` | 키보드 포커스 | Lynx에 키보드 포커스 개념 없음 |
| `onChange` (raw DOM event) | `React.ChangeEvent` | 의미 없음. `onCheckedChange`로 대체 |
### 추후 CSS 지원 시 추가 예정
| 기능 | 웹 대응 | 설명 |
| ------------------------ | ------------- | --------------------------------------------- |
| `active` (pressed) 모디파이어 | `data-active` | `switchmark` recipe CSS에 pressed 상태가 정의되면 활성화 |