Key rules
chatty-switchkhai báo 6 attribute, 0 property không có attribute, 1 event, 1 slot, 4 part và 1 CSS custom property.- Import 1 module
@avada/chatty-ui/elements/switch.jslà definechatty-switch; chunk chỉ import element nó dùng. Prototype chuẩn: trong guide. - Đổi tên attribute, event, slot, part hay CSS custom property là đổi API: diff của custom-elements.json phải có trong 1 MR.
On this page
Overview
A form-associated on/off switch with its own label; the change applies immediately. Class ChattySwitch.
Attributes
| Name | Property | Type | Default | Description |
|---|---|---|---|---|
checked |
checked |
boolean |
false |
On state; the checked attribute is the default restored by form reset. |
disabled |
disabled |
boolean |
false |
Disables the switch; not submitted. |
name |
name |
string |
'' |
Form field name. |
value |
value |
string |
'on' |
Submitted value while on. |
label |
label |
string |
'' |
Label text when the default slot is empty. |
label- |
labelHidden |
boolean |
false |
Hides the label visually; it stays the accessible name. |
Properties
Attributes also expose the properties listed in the Property column above.
Not declared in the manifest.
Events
| Name | Detail type | Description |
|---|---|---|
change |
Event |
On/ |
Slots
| Name | Description |
|---|---|
| default | Label content; falls back to the label attribute. |
Parts
| Name | Description |
|---|---|
base |
Label wrapper (track and text). |
control |
Native checkbox input with role switch. |
thumb |
Sliding thumb. |
label |
Label text. |
CSS custom properties
| Name | Default | Description |
|---|---|---|
- |
– | Track colour while on; defaults to the accent. |
Usage
import '@avada/chatty-ui/tokens/tokens.css';
import '@avada/chatty-ui/elements/switch.js';
<chatty-switch label="Sound notifications"></chatty-switch>
<chatty-switch label="Show on mobile" checked></chatty-switch>
<chatty-switch label="AI agent" label-hidden checked></chatty-switch>
Live preview
Component thật dùng cùng story với trang component; dữ liệu mẫu nằm trong frame cục bộ.
Default: trạng thái mặc định và nội dung mẫu của element.
Show code
<chatty-switch label="Sound notifications"></chatty-switch>
<chatty-switch label="Show on mobile" checked></chatty-switch>
<chatty-switch label="AI agent" label-hidden checked></chatty-switch>Các variant hoặc state chính đặt cạnh nhau trong cùng ví dụ.
Show code
<chatty-switch label="Disabled" disabled></chatty-switch>
<chatty-switch label="Disabled on" checked disabled></chatty-switch>Anatomy
Số trong hình khớp bảng bên dưới. Phần chỉ hiện ở state khác được ghi Not shown; ví dụ không thêm phần tử giả.
Anatomy: một instance mặc định riêng, có khoảng trống quanh các phần được đánh số.
Show code
<chatty-switch label="Sound notifications"></chatty-switch>
<chatty-switch label="Show on mobile" checked></chatty-switch>
<chatty-switch label="AI agent" label-hidden checked></chatty-switch>| Part | Element | Source |
|---|---|---|
| 1 | CSS part base |
chatty- |
| 2 | CSS part control |
chatty- |
| 3 | CSS part thumb |
chatty- |
| 4 | CSS part label |
chatty- |
Keyboard
| Key | Action |
|---|---|
| Space | Toggle the switch. |
Source
- chatty-ui/custom-elements.json