生态产品多个设计器现已发布候选版本

查看生态总览
Skip to content

Switch ​

Data entry component. This page is the API reference, generated from the source code. Usage guidance and live examples are on the Chinese page.

Anatomy ​

Parts in bold are required.

data-scope="switch": root · thumb · hidden-input · label · text

API reference ​

Descriptions of individual props, events and slots are written in Chinese and appear on the Chinese page.

Artifacts ​

LayerValue
Custom element<xh-switch>
Vue componentsXhSwitch
ComposableuseSwitch
State machineswitchMachine
Skin@xihan-ui/styles/switch.css

Props ​

PropTypeRequired
checkedboolean
defaultCheckedboolean
ariaLabelstring
disabledboolean
readOnlyboolean
invalidboolean
requiredboolean
loadingboolean
namestring
valuestring
toneTone
sizeSize
onCheckedChange(details: SwitchCheckedChangeDetails) => void

Events ​

Custom elements put the payload in detail; Vue emits an event of the same name.

EventPayload
checked-changeSwitchCheckedChangeDetails

React adapter props ​

Only the props each component declares itself: DOM attributes inherited from ComponentPropsWithRef are left out, and root props already listed in the Props table are not repeated. The Vue counterpart is the slot table above.

React componentPropTypeRequired
XhSwitchchildrenReactNode

State ​

Public state is written to data-state.

PartValues
root'checked' | 'unchecked'
thumb'checked' | 'unchecked'
label'checked' | 'unchecked'
text'checked' | 'unchecked'

The following names are internal to the state machine.

States: off · on

Events: TOGGLE · CONTROLLED.ON · CONTROLLED.OFF · FORM.RESET · DRAG.START · DRAG.MOVE · DRAG.END · CLICK.SWALLOW · PRESS.START · PRESS.END

Guards: isCheckedControlled · defaultsToChecked · canPress

connect API ​

getXxxProps() returns the host attributes of the matching part.

MemberType
checkedboolean
loadingboolean
setChecked(next: boolean) => void
getRootProps() => T['button']
getThumbProps() => T['element']
getHiddenInputProps() => T['input']
getLabelProps() => T['label']
getTextProps() => T['element']

Accessibility ​

Keyboard ​

Specification: W3C APG. Each row is a stable behavior id that the conformance tests cover; the conditions and effects are described on the Chinese page.

KeysBehavior
Space / Enterswitch.kbd.toggle
Space / Enterswitch.kbd.press

ARIA ​

These attributes are produced by connect.

PartAttributeValue
rootaria-busy'true' | undefined
rootaria-checked'true' | 'false'
rootaria-invalid'true' | 'false'
rootaria-labelprops.ariaLabel
rootaria-readonly'true' | 'false'
rootaria-required'true' | 'false'
rootrole'switch'

Style reference ​

Skin ​

@xihan-ui/styles/switch.css is written against [data-scope="switch"][data-part="root"] part selectors; the published build uses the mount class .xh-scope-switch in place of data-scope (same specificity) and sits in the xihan.components layer. Put overrides in xihan.overrides; the part selectors work as usual.

Data attributes ​

Produced by connect; valueless attributes are omitted while their condition does not hold.

PartAttributeValue
rootdata-disabled'' (present only while the condition holds)
rootdata-dragging'' (present only while the condition holds)
rootdata-invalid'' (present only while the condition holds)
rootdata-loading'' (present only while the condition holds)
rootdata-pressed'' (present only while the condition holds)
rootdata-readonly'' (present only while the condition holds)
rootdata-required'' (present only while the condition holds)
rootdata-sizeprops.size
rootdata-state'checked' | 'unchecked'
rootdata-toneprops.tone
rootdata-xh-action-control''
rootdata-xh-action-display'always'
rootdata-xh-action-profile'text'
rootdata-xh-action-sizeprops.size
rootdata-xh-action-variant'outline'
thumbdata-animating'' (present only while the condition holds)
thumbdata-disabled'' (present only while the condition holds)
thumbdata-dragging'' (present only while the condition holds)
thumbdata-loading'' (present only while the condition holds)
thumbdata-state'checked' | 'unchecked'
thumbdata-xh-loading-ring''
labeldata-disabled'' (present only while the condition holds)
labeldata-sizeprops.size
labeldata-state'checked' | 'unchecked'
textdata-disabled'' (present only while the condition holds)
textdata-state'checked' | 'unchecked'

CSS variables ​

The public override slots, generated from what the standalone skin actually consumes.

VariablePartCSS propertyStateDefault from
--xh-switch-bgroot--xh-ink-surface
background-color
default
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
readonly
xh-ink-surface
--xh-bg-subtle-active
--xh-switch-bg-checkedroot--xh-ink-surface
background-color
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
readonly
state=checked
xh-ink-surface
--xh-_switch-accent
--xh-switch-bg-checked-pressedrootbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
state=checked
--xh-_tone-active
--xh-switch-bg-checked-readonlyroot--xh-ink-surface
background-color
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
readonly
state=checked
xh-ink-surface
--xh-bg-subtle-active
--xh-switch-bg-disabledroot--xh-ink-surface
background-color
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
readonly
xh-ink-surface
--xh-bg-subtle
--xh-switch-bg-pressedrootbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_switch-track-bg
--xh-switch-borderrootbox-shadowcontrast=more
default
disabled
focus-visible
hover
is(:active, [data-pressed])
is(:hover, [data-loading])
loading
not([data-disabled])
not([data-loading])
pressed
state=unchecked
where([data-contrast='more'])
--xh-border-strong
transparent
--xh-switch-border-checkedrootbox-shadowdisabled
focus-visible
hover
is(:active, [data-pressed])
is(:hover, [data-loading])
loading
not([data-disabled])
not([data-loading])
pressed
state=checked
--xh-_switch-accent
--xh-switch-border-checked-readonlyrootbox-shadowcontrast=more
disabled
focus-visible
hover
is(:active, [data-pressed])
is(:hover, [data-loading])
loading
not([data-disabled])
not([data-loading])
pressed
readonly
state=checked
where([data-contrast='more'])
--xh-border-control
--xh-border-strong
--xh-switch-border-disabledrootbox-shadowdisabled
focus-visible
hover
is(:active, [data-pressed])
is(:hover, [data-loading])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-border-default
--xh-switch-border-invalidrootbox-shadowdisabled
focus-visible
hover
invalid
is(:active, [data-pressed])
is(:hover, [data-loading])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-border-invalid
--xh-switch-fgrootcolordefault
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-fg-default
--xh-switch-fg-checkedrootcolordisabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
state=checked
--xh-_tone-on
--xh-switch-fg-checked-readonlyrootcolordisabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
readonly
state=checked
--xh-fg-default
--xh-switch-fg-disabledrootcolordisabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-fg-disabled
--xh-switch-label-fglabelcolordefault--xh-fg-default
--xh-switch-label-fg-disabledlabelcolordisabled--xh-fg-subtle
--xh-switch-label-font-sizelabelfont-sizedefault--xh-_switch-label-font-size
--xh-switch-label-gaplabelgapdefault--xh-control-gap-md
--xh-switch-label-leadinglabelline-heightdefault--xh-leading-normal
--xh-switch-loading-durationthumbanimationxh-loading-ring--xh-motion-loop-spin
--xh-switch-loading-fgthumbborder
border-color
@media (prefers-reduced-motion: reduce)
@media print
motion=reduce
where([data-motion='reduce'])
xh-loading-ring
xh-loading-ring=overlay
--xh-_switch-accent
--xh-switch-radiusrootborder-radiusdefault--xh-shape-pill
--xh-switch-thumbthumbbackgrounddefault--xh-bg-surface-raised
--xh-switch-thumb-borderthumbborderdefault--xh-border-default
--xh-switch-thumb-fgthumbcolordefault--xh-fg-default
--xh-switch-thumb-fg-disabledroot
thumb
colordisabled--xh-fg-disabled
--xh-switch-thumb-press-stretchroot
thumb
inline-size
translate
disabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
not([data-readonly])
pressed
readonly
state=checked
--xh-motion-distance-sm
--xh-switch-thumb-radiusthumbborder-radiusdefault--xh-shape-circle
--xh-switch-thumb-shadowthumbbox-shadowdefault--xh-elevation-raised
--xh-switch-thumb-shadow-disabledroot
thumb
box-shadowdisablednone
--xh-switch-thumb-shadow-pressedroot
thumb
box-shadowdisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
not([data-readonly])
pressed
readonly
none
--xh-switch-thumb-shadow-readonlyroot
thumb
box-shadowreadonlynone

Released under The MIT License