# Switch URL: /lynx/components/switch Source: https://github.com/daangn/seed-design/blob/dev/docs/content/lynx/components/switch.mdx A component that allows you to instantly turn on or off specific settings and states. Lynx Engine 최소 버전: 3.6 사용 가능 버전: @seed-design/lynx-react@0.1.0, @seed-design/lynx-css@0.1.0 ## Preview ```tsx import "./styles"; import { useSeedClassName } from "@seed-design/lynx-react"; import { Switch } from "@/components/ui/switch"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); 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 ```tsx import "./styles"; import { VStack, useSeedClassName } from "@seed-design/lynx-react"; import { Switch, type SwitchProps } from "@/components/ui/switch"; function SwitchItem({ size, label }: { size: SwitchProps["size"]; label: string }) { return ; } export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( ); } ``` ### Tones #### Brand ```tsx import "./styles"; import { useSeedClassName } from "@seed-design/lynx-react"; import { Switch } from "@/components/ui/switch"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( ); } ``` #### Neutral ```tsx import "./styles"; import { useSeedClassName } from "@seed-design/lynx-react"; import { Switch } from "@/components/ui/switch"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( ); } ``` ### Long Label ```tsx import "./styles"; import { VStack, useSeedClassName } from "@seed-design/lynx-react"; import { Switch, type SwitchProps } from "@/components/ui/switch"; 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: SwitchProps["size"] }) { return ; } export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( ); } ``` ### Disabled ```tsx import "./styles"; import { useState } from "@lynx-js/react"; import { VStack, useSeedClassName } from "@seed-design/lynx-react"; import { Switch, type SwitchProps } from "@/components/ui/switch"; function SwitchItem({ disabled, label, tone = "brand", defaultChecked = false, }: { disabled: boolean; label: string; tone?: SwitchProps["tone"]; defaultChecked?: boolean; }) { return ; } export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); const [disabled, setDisabled] = useState(true); return ( ); } ``` ### Listening to Value Changes You can detect changes in the switch's selected state using `onCheckedChange`. ```tsx import "./styles"; import { useState } from "@lynx-js/react"; import { VStack, useSeedClassName } from "@seed-design/lynx-react"; import { Switch } from "@/components/ui/switch"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); const [count, setCount] = useState(0); const [lastValue, setLastValue] = useState(null); return ( { setCount((previous) => previous + 1); setLastValue(checked); }} /> onCheckedChange called: {count} times, last value:{" "} {lastValue === null ? "-" : JSON.stringify(lastValue)} ); } ``` ### Use Cases #### Using `Switchmark` `Switchmark` is a switch component without a label and can be used for custom layouts. ```tsx import "./styles"; import { HStack, Text, VStack, useSeedClassName } from "@seed-design/lynx-react"; import { Switchmark } from "@/components/ui/switch"; interface CustomSwitchProps { label: string; textStyle: "t7Regular" | "t7Medium" | "t7Bold"; defaultChecked?: boolean; } function CustomSwitch({ label, textStyle, defaultChecked = false }: CustomSwitchProps) { return ( {label} ); } export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( ); } ``` ## Web Version Differences Lynx `Switch` has the following differences from React `Switch`. - **Event handling**: Only `onCheckedChange` is exposed instead of `onChange`. The tap handler is owned internally by Switch. - **Rendering elements**: Renders native `` / `` elements instead of HTML ` ## Scale Feedback Lynx `Switch` does not currently provide feedback where `Switchmark` shrinks when pressed. This is not a CSS support issue, but because the current Rootage specification and Lynx Recipe do not have a pressed scale state. If you need this feedback, connect the pressed state and scale transform of the element wrapping `Switchmark` directly in your app. ## Unsupported Lynx Features The following features are not supported due to current Lynx platform constraints. ### Excluded due to Runtime Model Differences | Feature | Web Equivalent | Description | | ----------------------------------------- | ------------------------- | --------------------------------------------- | | `SwitchHiddenInput` | `` | No HTML form submission model in Lynx | | `name` / `value` / `required` / `invalid` | form field props | No native form submission model in Lynx | | `focus` / `focusVisible` | Keyboard focus | No keyboard focus concept in Lynx | | `onChange` (raw DOM event) | `React.ChangeEvent` | Not applicable. Use `onCheckedChange` instead |