JGuang

Switch

Switch · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Switch

Used to toggle between two states.

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

  • Basic · basic

  • Disabled · disabled

  • Text & icon · text

  • Two sizes · size

  • Loading · loading

  • Custom semantic dom styling · style-class

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

Semantic nodeNative descriptionVersion
rootRoot 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 styles6.0.0
contentContent element with block display, overflow hidden, border radius, height, padding, transition animations and other switch content area layout and styles6.0.0
indicatorIndicator element with absolute positioning, width, height, background color, border radius, shadow, transition animations and other switch handle styles and interactive effects6.3.0

Semantic description source: vendor/components/switch/demo/_semantic.tsx

Design Token

Types, inheritance and declarations retain the pinned native source. Defaults are computed by the native theme and may be overridden by ConfigProvider; this table does not invent static defaults.

TokenTypeNative description
trackHeightnumber | stringHeight of Switch
trackHeightSMnumber | stringHeight of small Switch
trackMinWidthnumber | stringMinimum width of Switch
trackMinWidthSMnumber | stringMinimum width of small Switch
trackPaddingnumberPadding of Switch
handleBgstringBackground color of Switch handle
handleShadowstringShadow of Switch handle
handleSizenumberSize of Switch handle
handleSizeSMnumberSize of small Switch handle
innerMinMarginnumberMinimum margin of content area
innerMaxMarginnumberMaximum margin of content area
innerMinMarginSMnumberMinimum margin of content area of small Switch
innerMaxMarginSMnumberMaximum margin of content area of small Switch

Declaration source: vendor/components/switch/style/index.ts

import type { CSSObject } from '@ant-design/cssinjs';

import { unit } from '@ant-design/cssinjs';

import { FastColor } from '@ant-design/fast-color';

import { genFocusStyle, resetComponent } from '../../style';

import { genNoMotionRawStyle, genNoMotionStyle } from '../../style/motion';

import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';

import { genStyleHooks, mergeToken } from '../../theme/internal';

export interface ComponentToken {
  /**
   *  @desc 开关高度
   *  @descEN Height of Switch
   */
  trackHeight: number | string;
  /**
   * @desc 小号开关高度
   * @descEN Height of small Switch
   */
  trackHeightSM: number | string;
  /**
   * @desc 开关最小宽度
   * @descEN Minimum width of Switch
   */
  trackMinWidth: number | string;
  /**
   * @desc 小号开关最小宽度
   * @descEN Minimum width of small Switch
   */
  trackMinWidthSM: number | string;
  /**
   * @desc 开关内边距
   * @descEN Padding of Switch
   */
  trackPadding: number;
  /**
   * @desc 开关把手背景色
   * @descEN Background color of Switch handle
   */
  handleBg: string;
  /**
   * @desc 开关把手阴影
   * @descEN Shadow of Switch handle
   */
  handleShadow: string;
  /**
   * @desc 开关把手大小
   * @descEN Size of Switch handle
   */
  handleSize: number;
  /**
   * @desc 小号开关把手大小
   * @descEN Size of small Switch handle
   */
  handleSizeSM: number;
  /**
   * @desc 内容区域最小边距
   * @descEN Minimum margin of content area
   */
  innerMinMargin: number;
  /**
   * @desc 内容区域最大边距
   * @descEN Maximum margin of content area
   */
  innerMaxMargin: number;
  /**
   * @desc 小号开关内容区域最小边距
   * @descEN Minimum margin of content area of small Switch
   */
  innerMinMarginSM: number;
  /**
   * @desc 小号开关内容区域最大边距
   * @descEN Maximum margin of content area of small Switch
   */
  innerMaxMarginSM: number;
}

interface SwitchToken extends FullToken<'Switch'> {
  switchDuration: string;
  switchColor: string;
  switchDisabledOpacity: number;
  switchLoadingIconSize: number | string;
  switchLoadingIconColor: string;
  switchHandleActiveInset: string;
}

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.

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

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/switch/index.tsx
  • vendor/components/switch/style/index.ts

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.