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

查看生态总览
Skip to content

Toggle ​

General 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="toggle": root

API reference ​

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

Artifacts ​

LayerValue
Custom element<xh-toggle>
Vue componentsXhToggle
ComposableuseToggle
State machinetoggleMachine
Skin@xihan-ui/styles/toggle.css

Props ​

PropTypeRequired
pressedboolean
defaultPressedboolean
disabledboolean
variantActionVariant
toneTone
sizeSize
iconOnlyboolean
fullWidthboolean
onPressedChange(details: TogglePressedChangeDetails) => void

Events ​

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

EventPayload
pressed-changeTogglePressedChangeDetails

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
XhTogglechildrenReactNode

State ​

Public state is written to data-state.

PartValues
root'on' | 'off'

The following names are internal to the state machine.

States: off · on

Events: TOGGLE · CONTROLLED.ON · CONTROLLED.OFF · PRESS.START · PRESS.END

Guards: isPressedControlled · canPress

connect API ​

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

MemberType
pressedboolean
setPressed(next: boolean) => void
getRootProps() => T['button']

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 / Entertoggle.kbd.toggle
Space / Entertoggle.kbd.press

ARIA ​

These attributes are produced by connect.

PartAttributeValue
rootaria-pressed'true' | 'false'

Style reference ​

Skin ​

@xihan-ui/styles/toggle.css is written against [data-scope="toggle"][data-part="root"] part selectors; the published build uses the mount class .xh-scope-toggle 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-full-width'' (present only while the condition holds)
rootdata-icon-only'' (present only while the condition holds)
rootdata-pressed'' (present only while the condition holds)
rootdata-sizeprops.size
rootdata-state'on' | 'off'
rootdata-toneprops.tone
rootdata-variantprops.variant
rootdata-xh-action-control''
rootdata-xh-action-display'always'
rootdata-xh-action-profile'icon' | 'text'
rootdata-xh-action-sizeprops.size
rootdata-xh-action-variant'solid' | 'ghost' | props.variant
rootdata-xh-ink-surface'' (present only while the condition holds)

CSS variables ​

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

VariablePartCSS propertyStateDefault from
--xh-toggle-bgroot--xh-ink-surface
background-color
default
focus-visible
xh-ink-surface
--xh-_action-variant-bg-focus-visible
--xh-_action-variant-bg-rest
--xh-toggle-bg-activerootbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-bg-pressed
--xh-toggle-bg-disabledroot--xh-ink-surface
background-color
disabled
xh-ink-surface
--xh-_action-variant-bg-disabled
--xh-toggle-bg-hoverrootbackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-bg-hover
--xh-toggle-bg-onroot--xh-ink-surface
background-color
disabled
focus-visible
not([data-variant='solid'])
state=on
variant=solid
xh-ink-surface
--xh-_tone
--xh-toggle-bg-on-activerootbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
not([data-variant='solid'])
pressed
state=on
variant=solid
--xh-_tone-active
--xh-toggle-bg-on-disabledroot--xh-ink-surface
background-color
disabled
not([data-variant='solid'])
state=on
variant=solid
xh-ink-surface
--xh-_action-variant-bg-rest
--xh-_toggle-bg-on
--xh-toggle-bg-on-hoverrootbackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
not([data-variant='solid'])
state=on
variant=solid
--xh-_tone-hover
--xh-toggle-borderrootborder
border-color
default
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-border-focus-visible
--xh-_action-variant-border-hover
--xh-_action-variant-border-pressed
--xh-_action-variant-border-rest
--xh-toggle-border-disabledrootborder-colordisabled--xh-_action-variant-border-disabled
--xh-toggle-border-onrootborder
border-color
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
not([data-variant='solid'])
pressed
state=on
variant=solid
--xh-_tone
--xh-toggle-fgrootcolor@media print
default
disabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
state=on
--xh-_action-variant-fg-focus-visible
--xh-_action-variant-fg-hover
--xh-_action-variant-fg-pressed
--xh-_action-variant-fg-rest
--xh-fg-default
--xh-toggle-fg-disabledrootcolordisabled--xh-_action-variant-fg-disabled
--xh-toggle-fg-onrootcolordisabled
focus-visible
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
not([data-variant='solid'])
pressed
state=on
variant=solid
--xh-_tone-on
--xh-toggle-fg-on-disabledrootcolordisabled
not([data-variant='solid'])
state=on
variant=solid
--xh-_action-variant-fg-rest
--xh-_toggle-fg-on
--xh-toggle-font-sizerootfont-sizedefault--xh-_action-profile-font-size
--xh-toggle-font-weightrootfont-weightdefault--xh-text-label-weight
--xh-toggle-gaprootgapdefault--xh-_action-profile-gap
--xh-toggle-hrootblock-size
inline-size
default
xh-action-profile=icon
--xh-_action-profile-visual-size
--xh-toggle-icon-size*
root
--xh-icon-size
block-size
inline-size
default--xh-_action-profile-glyph-size
--xh-toggle-pxrootpadding-inlinedefault--xh-_action-profile-padding-inline
--xh-toggle-radiusrootborder-radiusdefault--xh-shape-control
--xh-toggle-shadowrootbox-shadowdisabled
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
state=on
--xh-_highlight-tone

Released under The MIT License