Components

切换

复选框的切换开关替代方案。

<Switch defaultChecked />

API 参考

¥API Reference

此组件继承了 开关基础组件 的属性并支持 常用边距属性

¥This component inherits props from the Switch primitive and supports common margin props.

PropTypeDefault
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

示例

¥Examples

尺寸

¥Size

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

¥Use the size prop to control the size of the switch.

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

变体

¥Variant

使用 variant 属性控制开关的视觉样式。

¥Use the variant prop to control the visual style of the switch.

<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

使用 color 属性指定特定的 color

¥Use the color prop to assign a specific color.

<Flex gap="2">
<Switch color="indigo" defaultChecked />
<Switch color="cyan" defaultChecked />
<Switch color="orange" defaultChecked />
<Switch color="crimson" defaultChecked />
</Flex>

高对比度

¥High-contrast

使用 highContrast 属性增加亮色模式下的色彩对比度。

¥Use the highContrast prop to increase color contrast in light mode.

<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

使用 radius 属性指定特定的圆角值。

¥Use the radius prop to assign a specific radius value.

<Flex gap="3">
<Switch radius="none" defaultChecked />
<Switch radius="small" defaultChecked />
<Switch radius="full" defaultChecked />
</Flex>

对齐方式

¥Alignment

Text 中组合 Switch,会自动将其与第一行文本居中。

¥Composing Switch within Text automatically centers it with the first line of text.

<Flex direction="column" gap="3">
<Text as="label" size="2">
<Flex gap="2">
<Switch size="1" defaultChecked /> Sync settings
</Flex>
</Text>
<Text as="label" size="3">
<Flex gap="2">
<Switch size="2" defaultChecked /> Sync settings
</Flex>
</Text>
<Text as="label" size="4">
<Flex gap="2">
<Switch size="3" defaultChecked /> Sync settings
</Flex>
</Text>
</Flex>

它还能自动与多行文本对齐。

¥It is automatically well-aligned with multi-line text too.

已禁用

¥Disabled

使用原生 disabled 属性创建禁用的开关。

¥Use the native disabled attribute to create a disabled switch.

<Flex direction="column" gap="2">
<Text as="label" size="2">
<Flex gap="2">
<Switch size="1" />
Off
</Flex>
</Text>
<Text as="label" size="2">
<Flex gap="2">
<Switch size="1" defaultChecked />
On
</Flex>
</Text>
<Text as="label" size="2" color="gray">
<Flex gap="2">
<Switch size="1" disabled />
On
</Flex>
</Text>
<Text as="label" size="2" color="gray">
<Flex gap="2">
<Switch size="1" disabled defaultChecked />
Off
</Flex>
</Text>
</Flex>
Previous旋转器
Next表格