Switch 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
import { Switch } from "seed-design/ui/switch" ;
export default function SwitchPreview () {
return < Switch defaultChecked />;
}
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/react yarn add @seed-design/react pnpm add @seed-design/react bun add @seed-design/react Copy the code below and paste it in /**
* @file ui:switch
* @requires @seed-design/react@^2.0.0
* @requires @seed-design/css@^2.0.0
**/
"use client" ;
import * as React from "react" ;
import { Switch as SeedSwitch } from "@seed-design/react" ;
export interface SwitchProps extends SeedSwitch . RootProps {
inputProps ?: React . InputHTMLAttributes < HTMLInputElement >;
rootRef ?: React . Ref < HTMLLabelElement >;
label ?: React . ReactNode ;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/switch
*/
export const Switch = React. forwardRef < HTMLInputElement , SwitchProps >(
({ inputProps , rootRef , label , ... otherProps }, ref ) => {
return (
< SeedSwitch.Root ref = {rootRef} { ... otherProps}>
< SeedSwitch.Control >
< SeedSwitch.Thumb />
</ SeedSwitch.Control >
{label && < SeedSwitch.Label >{label}</ SeedSwitch.Label >}
< SeedSwitch.HiddenInput ref = {ref} { ... inputProps} />
</ SeedSwitch.Root >
);
},
);
Switch.displayName = "Switch" ;
export interface SwitchmarkProps extends SeedSwitch . ControlProps {}
/**
* @see https://crispy.carrot-soft.tech/react/components/switch
*/
export const Switchmark = React. forwardRef < HTMLDivElement , SwitchmarkProps >(( props , ref ) => {
return (
< SeedSwitch.Control ref = {ref} { ... props}>
< SeedSwitch.Thumb />
</ SeedSwitch.Control >
);
});
Switchmark.displayName = "Switchmark" ;
/**
* @deprecated Use `Switchmark` instead. Will be removed in @seed-design/react@2.0.0.
*/
export const SwitchMark = Switchmark;
/**
* @deprecated Use `SwitchmarkProps` instead. Will be removed in @seed-design/react@2.0.0.
*/
export type SwitchMarkProps = SwitchmarkProps ;
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
inputProps?React.InputHTMLAttributes < HTMLInputElement > | undefined
rootRef?React.Ref < HTMLLabelElement > | undefined
label?React.ReactNode
disabled?boolean | undefined
invalid?boolean | undefined
required?boolean | undefined
checked?boolean | undefined
defaultChecked?boolean | undefined
onCheckedChange?(( checked : boolean ) => void ) | undefined
Deprecated Props size="medium" and size="small" are no longer supported. Use size="32" and size="16" instead.
import { VStack } from "@seed-design/react" ;
import { Switch } from "seed-design/ui/switch" ;
export default function SwitchSizes () {
return (
< VStack align = "center" gap = "spacingY.componentDefault" >
< Switch size = "32" label = "32 (default)" defaultChecked />
< Switch size = "24" label = "24" defaultChecked />
< Switch size = "16" label = "16" defaultChecked />
</ VStack >
);
}
import { Switch } from "seed-design/ui/switch" ;
export default function SwitchBrand () {
return < Switch tone = "brand" label = "Brand" defaultChecked />;
}
import { Switch } from "seed-design/ui/switch" ;
export default function SwitchNeutral () {
return < Switch tone = "neutral" label = "Neutral" defaultChecked />;
}
import { VStack } from "@seed-design/react" ;
import { Switch } from "seed-design/ui/switch" ;
export default function SwitchLongLabel () {
return (
< VStack gap = "spacingY.componentDefault" >
< Switch
size = "32"
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."
/>
< Switch
size = "24"
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."
/>
< Switch
size = "16"
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."
/>
</ VStack >
);
}
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 (
< VStack gap = "x8" align = "center" >
< VStack align = "flex-start" gap = "spacingY.componentDefault" >
< Switch disabled = {disabled} label = "Not Checked (Brand)" />
< Switch disabled = {disabled} defaultChecked label = "Checked (Brand)" />
< Switch disabled = {disabled} label = "Not Checked (Neutral)" tone = "neutral" />
< Switch disabled = {disabled} defaultChecked label = "Checked (Neutral)" tone = "neutral" />
</ VStack >
< Switch
size = "16"
checked = {disabled}
onCheckedChange = {setDisabled}
label = "Disable switches"
tone = "neutral"
/>
</ VStack >
);
}
You can detect switch selection state changes using onCheckedChange.
If you need to use events, you can add event handlers directly to the internal <input> element via inputProps.
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 < boolean | null >( null );
return (
< VStack gap = "x4" align = "center" >
< Switch
label = "Click me"
onCheckedChange = {( checked ) => {
setCount (( prev ) => prev + 1 );
setLastValue (checked);
}}
/>
< Text >
onCheckedChange called: {count} times, last value: { `${ lastValue ?? "-"}` }
</ Text >
</ VStack >
);
}
Switchmark is a switch component excluding the label, which can be used for custom layouts.
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 (
< VStack asChild gap = "x2" align = "center" >
< Switch.Root { ... props}>
< Switchmark />
< Switch.HiddenInput />
{children}
</ Switch.Root >
</ VStack >
);
}
export default function () {
return (
< HStack gap = "x6" >
< CustomSwitch >
< Text textStyle = "t7Regular" >regular</ Text >
</ CustomSwitch >
< CustomSwitch defaultChecked >
< Text textStyle = "t7Medium" >medium</ Text >
</ CustomSwitch >
< CustomSwitch >
< Text textStyle = "t7Bold" >bold</ Text >
</ CustomSwitch >
</ HStack >
);
}
Switch provides feedback where Switchmark shrinks slightly when pressed.
If you apply 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.
.my-row {
--seed-switchmark-feedback-scale : 1 ;
}