# 开关

切换开关，替代复选框。

```jsx
<Switch defaultChecked />
```

## [API 参考](#api-参考)

该组件继承自 [Switch 原语](/primitives/docs/components/switch) 的属性，并支持 [常见的边距属性](/themes/docs/overview/layout#margin-props)。

| Prop           | Type                                                 | Default          |
| -------------- | ---------------------------------------------------- | ---------------- |
| `size`         | `Responsive<"1" \| "2" \| "3">`                      | `"2"`            |
| `variant`      | `"classic" \| "surface" \| "soft"`                   | `"surface"`      |
| `color`        | `enum`                                               | No default value |
| `highContrast` | `boolean`                                            | No default value |
| `radius`       | `"none" \| "small" \| "medium" \| "large" \| "full"` | No default value |

## [示例](#示例)

### [尺寸](#尺寸)

使用 `size` 属性来控制开关的大小。

```jsx
<Flex align="center" gap="2">
  <Switch size="1" defaultChecked />

  <Switch size="2" defaultChecked />

  <Switch size="3" defaultChecked />
</Flex>
```

### [变体](#变体)

使用 `variant` 属性来控制开关的视觉风格。

```jsx
<Flex gap="2">
  <Flex direction="column" gap="3">
    <Switch variant="surface" />

    <Switch variant="classic" />

    <Switch variant="soft" />
  </Flex>

  <Flex direction="column" gap="3">
    <Switch variant="surface" defaultChecked />

    <Switch variant="classic" defaultChecked />

    <Switch variant="soft" defaultChecked />
  </Flex>
</Flex>
```

### [颜色](#颜色)

使用 `color` 属性来指定特定的 [颜色](/themes/docs/theme/color)。

```jsx
<Flex gap="2">
  <Switch color="indigo" defaultChecked />

  <Switch color="cyan" defaultChecked />

  <Switch color="orange" defaultChecked />

  <Switch color="crimson" defaultChecked />
</Flex>
```

### [高对比度](#高对比度)

使用 `highContrast` 属性在亮模式下增加颜色对比度。

```jsx
<Grid rows="2" gapX="2" gapY="3" display="inline-grid" flow="column">
  <Switch color="indigo" defaultChecked />

  <Switch color="indigo" defaultChecked highContrast />

  <Switch color="cyan" defaultChecked />

  <Switch color="cyan" defaultChecked highContrast />

  <Switch color="orange" defaultChecked />

  <Switch color="orange" defaultChecked highContrast />

  <Switch color="crimson" defaultChecked />

  <Switch color="crimson" defaultChecked highContrast />

  <Switch color="gray" defaultChecked />

  <Switch color="gray" defaultChecked highContrast />
</Grid>
```

### [半径](#半径)

使用 `radius` 属性来指定特定的半径值。

```jsx
<Flex gap="3">
  <Switch radius="none" defaultChecked />

  <Switch radius="small" defaultChecked />

  <Switch radius="full" defaultChecked />
</Flex>
```

### [对齐](#对齐)

将 `Switch` 组合在 `Text` 中自动与第一行文本居中对齐。

```jsx
<Flex direction="column" gap="3">
  <Text as="label" size="2">
    <Flex gap="2">
      <Switch size="1" defaultChecked /> 同步设置
    </Flex>
  </Text>

  <Text as="label" size="3">
    <Flex gap="2">
      <Switch size="2" defaultChecked /> 同步设置
    </Flex>
  </Text>

  <Text as="label" size="4">
    <Flex gap="2">
      <Switch size="3" defaultChecked /> 同步设置
    </Flex>
  </Text>
</Flex>
```

它也自动与多行文本良好对齐。

### [禁用](#禁用)

使用原生 `disabled` 属性来创建一个禁用的开关。

```jsx
<Flex direction="column" gap="2">
  <Text as="label" size="2">
    <Flex gap="2">
      <Switch size="1" />
      关闭
    </Flex>
  </Text>

  <Text as="label" size="2">
    <Flex gap="2">
      <Switch size="1" defaultChecked />
      开启
    </Flex>
  </Text>

  <Text as="label" size="2" color="gray">
    <Flex gap="2">
      <Switch size="1" disabled />
      开启
    </Flex>
  </Text>

  <Text as="label" size="2" color="gray">
    <Flex gap="2">
      <Switch size="1" disabled defaultChecked />
      关闭
    </Flex>
  </Text>
</Flex>
```

<!--$-->

<!--/$-->
