# Switch
URL: /react/components/switch
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/switch.mdx
A component that allows turning specific settings and states on or off immediately.
사용 가능 버전: @seed-design/react@0.0.1, @seed-design/css@0.0.1
## Preview
```tsx
import { Switch } from "seed-design/ui/switch";
export default function SwitchPreview() {
return ;
}
```
## Installation
- npm: npx @seed-design/cli@latest add ui:switch
- pnpm: pnpm dlx @seed-design/cli@latest add ui:switch
- yarn: yarn dlx @seed-design/cli@latest add ui:switch
- bun: bun x @seed-design/cli@latest add ui:switch
## Props
### `Switch`
### `Switchmark`
## Examples
### Sizes
`size="medium"` and `size="small"` are no longer supported. Use `size="32"` and `size="16"` instead.
```tsx
import { VStack } from "@seed-design/react";
import { Switch } from "seed-design/ui/switch";
export default function SwitchSizes() {
return (
);
}
```
### Tones
#### Brand
```tsx
import { Switch } from "seed-design/ui/switch";
export default function SwitchBrand() {
return ;
}
```
#### Neutral
```tsx
import { Switch } from "seed-design/ui/switch";
export default function SwitchNeutral() {
return ;
}
```
### Long Label
```tsx
import { VStack } from "@seed-design/react";
import { Switch } from "seed-design/ui/switch";
export default function SwitchLongLabel() {
return (
);
}
```
### Disabled
```tsx
import { VStack } from "@seed-design/react";
import { useState } from "react";
import { Switch } from "seed-design/ui/switch";
export default function SwitchDisabled() {
const [disabled, setDisabled] = useState(true);
return (
);
}
```
### Listening to Value Changes
You can detect switch selection state changes using `onCheckedChange`.
If you need to use events, you can add event handlers directly to the internal `` element via `inputProps`.
```tsx
import { VStack, Text } from "@seed-design/react";
import { Switch } from "seed-design/ui/switch";
import { useState } from "react";
export default function SwitchValueChanges() {
const [count, setCount] = useState(0);
const [lastValue, setLastValue] = useState(null);
return (
{
setCount((prev) => prev + 1);
setLastValue(checked);
}}
/>
onCheckedChange called: {count} times, last value: {`${lastValue ?? "-"}`}
);
}
```
### Use Cases
#### Using `Switchmark`
`Switchmark` is a switch component excluding the label, which can be used for custom layouts.
```tsx
import { HStack, Text, VStack } from "@seed-design/react";
import { Switch } from "@seed-design/react/primitive";
import { Switchmark } from "seed-design/ui/switch";
function CustomSwitch({ children, ...props }: Switch.RootProps) {
return (
{children}
);
}
export default function () {
return (
regularmediumbold
);
}
```
## Scale Feedback
`Switch` provides feedback where `Switchmark` shrinks slightly when pressed.
If you apply [Scale Feedback](/react/components/concepts/scale-feedback) directly to the element wrapping `Switchmark`, the outer element and `Switchmark` shrink together, causing the shrinking to appear doubled. Setting `--seed-switchmark-feedback-scale` to `1` on a parent element of `Switchmark` allows you to disable only `Switchmark`'s shrinking. Since this variable is inherited, you can set it on an element wrapping the entire `Switch`.
```css title="MyRow.css"
.my-row {
--seed-switchmark-feedback-scale: 1;
}
```