Switch A component that allows you to instantly turn on or off specific settings and states.
@seed-design/lynx-react@0.1.0, @seed-design/lynx-css@0.1.0
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 (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< view className = "switch-preview" >
< Switch accessibility-label = "Switch" defaultChecked />
</ view >
</ view >
);
}
npx @seed-design/cli@latest add ui:switch pnpm dlx @seed-design/cli@latest add ui:switch yarn dlx @seed-design/cli@latest add ui:switch bun x @seed-design/cli@latest add ui:switch
Install dependencies
npm install @seed-design/lynx-react yarn add @seed-design/lynx-react pnpm add @seed-design/lynx-react bun add @seed-design/lynx-react Copy the code below and paste it in /**
* @file ui:switch
* @requires @seed-design/lynx-react@>=0.1.0 <1.0.0
* @requires @seed-design/lynx-css@>=0.1.0 <1.0.0
**/
import * as React from "@lynx-js/react" ;
import { Switch as SeedSwitch } from "@seed-design/lynx-react" ;
export interface SwitchProps extends SeedSwitch . RootProps {
label ?: React . ReactNode ;
}
/**
* @see https://crispy.carrot-soft.tech/lynx/components/switch
*/
export const Switch = React. forwardRef < unknown , SwitchProps >(
({ label , children , "accessibility-label" : accessibilityLabel , ... otherProps }, ref ) => {
return (
< SeedSwitch.Root
ref = {ref}
accessibility-label = {accessibilityLabel ?? ( typeof label === "string" ? label : undefined )}
{ ... otherProps}
>
< SeedSwitch.Control >
< SeedSwitch.Thumb />
</ SeedSwitch.Control >
{label != null ? < SeedSwitch.Label >{label}</ SeedSwitch.Label > : null }
{children}
</ SeedSwitch.Root >
);
},
);
Switch.displayName = "Switch" ;
export interface SwitchmarkProps extends Omit < SeedSwitch . RootProps , "children" > {}
/**
* @see https://crispy.carrot-soft.tech/lynx/components/switch
*/
export const Switchmark = React. forwardRef < unknown , SwitchmarkProps >(( props , ref ) => {
return (
< SeedSwitch.Root ref = {ref} { ... props}>
< SeedSwitch.Control >
< SeedSwitch.Thumb />
</ SeedSwitch.Control >
</ SeedSwitch.Root >
);
});
Switchmark.displayName = "Switchmark" ;
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
label?React.ReactNode
defaultChecked?boolean | undefined
onCheckedChange?(( checked : boolean ) => void ) | undefined
style?CSSProperties | undefined
children?React.ReactNode
className?string | undefined
className?string | undefined
style?CSSProperties | undefined
defaultChecked?boolean | undefined
onCheckedChange?(( checked : boolean ) => void ) | undefined
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 < Switch size = {size} label = {label} defaultChecked />;
}
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< VStack className = "switch-preview" gap = "spacingY.componentDefault" >
< SwitchItem size = "32" label = "32 (default)" />
< SwitchItem size = "24" label = "24" />
< SwitchItem size = "16" label = "16" />
</ VStack >
</ view >
);
}
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 (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< view className = "switch-preview" >
< Switch tone = "brand" label = "Brand" defaultChecked />
</ view >
</ view >
);
}
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 (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< view className = "switch-preview" >
< Switch tone = "neutral" label = "Neutral" defaultChecked />
</ view >
</ view >
);
}
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 < Switch size = {size} label = {label} />;
}
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< VStack className = "switch-preview" gap = "spacingY.componentDefault" >
< SwitchItem size = "32" />
< SwitchItem size = "24" />
< SwitchItem size = "16" />
</ VStack >
</ view >
);
}
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 < Switch disabled = {disabled} tone = {tone} defaultChecked = {defaultChecked} label = {label} />;
}
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
const [ disabled , setDisabled ] = useState ( true );
return (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< VStack className = "switch-preview" gap = "x8" >
< VStack gap = "spacingY.componentDefault" >
< SwitchItem disabled = {disabled} label = "Not Checked (Brand)" />
< SwitchItem disabled = {disabled} defaultChecked label = "Checked (Brand)" />
< SwitchItem disabled = {disabled} tone = "neutral" label = "Not Checked (Neutral)" />
< SwitchItem disabled = {disabled} tone = "neutral" defaultChecked label = "Checked (Neutral)" />
</ VStack >
< Switch
size = "16"
tone = "neutral"
checked = {disabled}
onCheckedChange = {setDisabled}
label = "Disable switches"
/>
</ VStack >
</ view >
);
}
You can detect changes in the switch's selected state using onCheckedChange.
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 < boolean | null >( null );
return (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< VStack className = "switch-preview" gap = "x4" >
< Switch
label = "Click me"
onCheckedChange = {( checked ) => {
setCount (( previous ) => previous + 1 );
setLastValue (checked);
}}
/>
< text className = "switch-preview__status" >
onCheckedChange called: {count} times, last value:{ " " }
{lastValue === null ? "-" : JSON . stringify (lastValue)}
</ text >
</ VStack >
</ view >
);
}
Switchmark is a switch component without a label and can be used for custom layouts.
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 (
< VStack gap = "x2" align = "center" >
< Switchmark accessibility-label = {label} defaultChecked = {defaultChecked} />
< Text textStyle = {textStyle}>{label}</ Text >
</ VStack >
);
}
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-switch-root` }>
< HStack className = "switch-preview" gap = "x6" >
< CustomSwitch label = "regular" textStyle = "t7Regular" />
< CustomSwitch label = "medium" textStyle = "t7Medium" defaultChecked />
< CustomSwitch label = "bold" textStyle = "t7Bold" />
</ HStack >
</ view >
);
}
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 <view> / <text> elements instead of HTML <label> / <input>.
Compound structure : The structure of Switch.Root → Switch.Control → Switch.Thumb / Switch.Label and the flow of sharing state/variant with Context are the same. It also supports passing tone/size directly to Control to override Root.
Accessibility : Switch.Root is exposed as an accessibility element by default. The role description is switch, and the value is checked or not checked depending on the selection state. The disabled trait is passed in the disabled state. Registry Switch uses the string label as the accessibility name. For Switchmark or compound labels, specify accessibility-label directly.
ref forwarding : All sub-components support React.forwardRef.
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.
The following features are not supported due to current Lynx platform constraints.
Feature Web Equivalent Description SwitchHiddenInput<input type="checkbox">No HTML form submission model in Lynx name / value / required / invalidform field props No native form submission model in Lynx focus / focusVisibleKeyboard focus No keyboard focus concept in Lynx onChange (raw DOM event)React.ChangeEventNot applicable. Use onCheckedChange instead