logoAnt Design

⌘ K
  • Design
  • Development
  • Components
  • Blog
  • Resources
YouMindTRACTIANLobeHubCodeRabbit
6.4.5
  • Components Overview
  • Changelog
    v6.4.5
  • General
    • Button
    • FloatButton
    • Icon
    • Typography
  • Layout
    • Divider
    • Flex
    • Grid
    • Layout
    • Masonry
      6.0.0
    • Space
    • Splitter
  • Navigation
    • Anchor
    • Breadcrumb
    • Dropdown
    • Menu
    • Pagination
    • Steps
    • Tabs
  • Data Entry
    • AutoComplete
    • Cascader
    • Checkbox
    • ColorPicker
    • DatePicker
    • Form
    • Input
    • InputNumber
    • Mentions
    • Radio
    • Rate
    • Select
    • Slider
    • Switch
    • TimePicker
    • Transfer
    • TreeSelect
    • Upload
  • Data Display
    • Avatar
    • Badge
    • Calendar
    • Card
    • Carousel
    • Collapse
    • Descriptions
    • Empty
    • Image
    • List
      DEPRECATED
    • Popover
    • QRCode
    • Segmented
    • Statistic
    • Table
    • Tag
    • Timeline
    • Tooltip
    • Tour
    • Tree
  • Feedback
    • Alert
    • Drawer
    • Message
    • Modal
    • Notification
    • Popconfirm
    • Progress
    • Result
    • Skeleton
    • Spin
    • Watermark
  • Other
    • Affix
    • App
    • BorderBeam
      6.4.0
    • ConfigProvider
    • Util
When To Use
Examples
Basic
Disabled
Text & icon
Two sizes
Loading
Custom semantic dom styling
API
Methods
Semantic DOM
Design Token
FAQ
Why not work in Form.Item?

Switch

Used to toggle between two states.
Importimport { Switch } from 'antd';
GitHub
components/switchIssueOpen issues
Docs
Edit this pageLLMs.md
SliderTimePicker

Resources

Ant Design X
Ant Design Charts
Ant Design Pro
Pro Components
Ant Design Mobile
Ant Design Mini
Ant Design Web3
Ant Design Landing-Landing Templates
Scaffolds-Scaffold Market
Umi-React Application Framework
dumi-Component doc generator
qiankun-Micro-Frontends Framework
Ant Motion-Motion Solution
China Mirror 🇨🇳

Community

Awesome Ant Design
Medium
X
yuque logoAnt Design in YuQue
Ant Design in Zhihu
Experience Cloud Blog
seeconf logoSEE Conf-Experience Tech Conference

Help

GitHub
Change Log
FAQ
For Agents
Bug Report
Issues
Discussions
StackOverflow
SegmentFault

Ant XTech logoMore Products

yuque logoYuQue-Document Collaboration Platform
AntV logoAntV-Data Visualization
Egg logoEgg-Enterprise Node.js Framework
Kitchen logoKitchen-Sketch Toolkit
Galacean logoGalacean-Interactive Graphics Solution
WeaveFox logoWeaveFox-AI Development with WeaveFox 🦊
xtech logoAnt Financial Experience Tech
Theme Editor
Made with ❤ by
Ant Group and Ant Design Community
loading

When To Use

  • If you need to represent the switching between two states or on-off state.
  • The difference between Switch and Checkbox is that Switch will trigger a state change directly when you toggle it, while Checkbox is generally used for state marking, which should work in conjunction with submit operation.

Examples

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersionGlobal Config
checkedDetermine whether the Switch is checkedbooleanfalse×
checkedChildrenThe content to be shown when the state is checkedReactNode-×
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-6.0.0
defaultCheckedWhether to set the initial statebooleanfalse×
defaultValueAlias for defaultCheckedboolean-5.12.0×
disabledDisable switchbooleanfalse×
loadingLoading state of switchbooleanfalse×
sizeThe size of the Switch, options: medium small'medium' | 'small'medium×
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-6.0.0
unCheckedChildrenThe content to be shown when the state is uncheckedReactNode-×
valueAlias for checkedboolean-5.12.0×
onChangeTrigger when the checked state is changingfunction(checked: boolean, event: Event)-×
onClickTrigger when clickedfunction(checked: boolean, event: Event)-×

Methods

NameDescription
blur()Remove focus
focus()Get focus

Semantic DOM

Design Token

Component TokenHow to use?
Token NameDescriptionTypeDefault Value
handleBgBackground color of Switch handlestring#fff
handleShadowShadow of Switch handlestring0 2px 4px 0 rgba(0,35,11,0.2)
handleSizeSize of Switch handlenumber18
handleSizeSMSize of small Switch handlenumber12
innerMaxMarginMaximum margin of content areanumber24
innerMaxMarginSMMaximum margin of content area of small Switchnumber18
innerMinMarginMinimum margin of content areanumber9
innerMinMarginSMMinimum margin of content area of small Switchnumber6
trackHeightHeight of Switchstring | number22
trackHeightSMHeight of small Switchstring | number16
trackMinWidthMinimum width of Switchstring | number44
trackMinWidthSMMinimum width of small Switchstring | number28
trackPaddingPadding of Switchnumber2
Global TokenHow to use?
Token NameDescriptionTypeDefault Value
colorPrimaryBrand color is one of the most direct visual elements to reflect the characteristics and communication of the product. After you have selected the brand color, we will automatically generate a complete color palette and assign it effective design semantics.string#1677ff
colorPrimaryBorderThe stroke color under the main color gradient, used on the stroke of components such as Slider.string#91caff
colorPrimaryHoverHover state under the main color gradient.string#4096ff
colorTextDefault text color which comply with W3C standards, and this color is also the darkest neutral color.stringrgba(0,0,0,0.88)
colorTextLightSolidControl the highlight color of text with background color, such as the text in Primary Button components.string#fff
colorTextQuaternaryThe fourth level of text color is the lightest text color, such as form input prompt text, disabled color text, etc.stringrgba(0,0,0,0.25)
colorTextTertiaryThe third level of text color is generally used for descriptive text, such as form supplementary explanation text, list descriptive text, etc.stringrgba(0,0,0,0.45)
fontFamilyThe font family of Ant Design prioritizes the default interface font of the system, and provides a set of alternative font libraries that are suitable for screen display to maintain the readability and readability of the font under different platforms and browsers, reflecting the friendly, stable and professional characteristics.string-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'
fontSizeThe most widely used font size in the design system, from which the text gradient will be derived.number14
fontSizeIconControl the font size of operation icon in Select, Cascader, etc. Normally same as fontSizeSM.number12
fontSizeSMSmall font sizenumber12
lineHeightLine height of text.number1.5714285714285714
lineWidthFocusControl the width of the line when the component is in focus state.number3
marginXXSControl the margin of an element, with the smallest size.number4
motionDurationMidMotion speed, medium speed. Used for medium element animation interaction.string0.2s
opacityLoadingControl the opacity of the loading state.number0.65

FAQ

Why not work in Form.Item?

Form.Item default bind value to value property, but Switch value property is checked. You can use valuePropName to change bind property.

tsx
<Form.Item name="fieldA" valuePropName="checked">
<Switch />
</Form.Item>
Basic

The most basic usage.

CodeSandbox Icon
Hitu Icon
codepen icon
External Link Icon
Expand Icon
Text & icon

With text and icon.

CodeSandbox Icon
Hitu Icon
codepen icon
External Link Icon
Expand Icon
Loading

Mark a pending state of switch.

CodeSandbox Icon
Hitu Icon
codepen icon
External Link Icon
Expand Icon
Disabled

Disabled state of Switch.

CodeSandbox Icon
Hitu Icon
codepen icon
External Link Icon
Expand Icon
Two sizes

size="small" represents a small sized switch.

CodeSandbox Icon
Hitu Icon
codepen icon
External Link Icon
Expand Icon
Custom semantic dom styling

You can customize the semantic dom style of Switch by passing objects/functions through classNames and styles.

CodeSandbox Icon
Hitu Icon
codepen icon
External Link Icon
Expand Icon
6.0.0


  • root
    6.0.0
    Root element with min-width, height, line-height, vertical alignment, background color, border, border radius, cursor style, transition animations, user selection and other basic switch container styles
  • content
    6.0.0
    Content element with block display, overflow hidden, border radius, height, padding, transition animations and other switch content area layout and styles
  • indicator
    6.3.0
    Indicator element with absolute positioning, width, height, background color, border radius, shadow, transition animations and other switch handle styles and interactive effects