On this page

chatty-switch

API chuẩn của element chatty-switch, sinh từ custom-elements.json của prototype trong guide.

Copy page as MarkdownRead Markdown ↗

Key rules

  • chatty-switch khai 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.js là define chatty-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-hidden 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/off changed by the user, re-dispatched from the host.

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
--chatty-switch-on-color – 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ộ.

Standard
LightDarkDesktopMobile

Default: trạng thái mặc định và nội dung mẫu của element.

Show code
chatty-switch.element.jsWrap code linesCopy
<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>
Standard
LightDarkDesktopMobile

Các variant hoặc state chính đặt cạnh nhau trong cùng ví dụ.

Show code
chatty-switch.element.jsWrap code linesCopy
<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ả.

Standard
LightDarkDesktopMobile

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.element.jsWrap code linesCopy
<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-ui/custom-elements.json
2 CSS part control chatty-ui/custom-elements.json
3 CSS part thumb chatty-ui/custom-elements.json
4 CSS part label chatty-ui/custom-elements.json

Keyboard

Key Action
Space Toggle the switch.

Source

  • chatty-ui/custom-elements.json