UNPKG

@kelvininc/ui-components

Version:
151 lines (150 loc) 5.59 kB
import { Host, h } from "@stencil/core"; import { EComponentSize } from "../../utils/types"; import { throttle } from "lodash-es"; import { EIconName } from "../icon/icon.types"; import { DEFAULT_THROTTLE_WAIT } from "../../config"; /** * @part icon-svg - The switch icon. * @part icon-square - The switch icon square container. * @part button - The switch button. */ export class KvSwitchButton { constructor() { /** @inheritdoc */ this.disabled = false; /** @inheritdoc */ this.checked = false; /** @inheritdoc */ this.size = EComponentSize.Large; } onStateChange() { if (this.disabled) { return; } this.checked = !this.checked; this.switchChange.emit(this.checked); } connectedCallback() { this.onSwitchClick = throttle(() => this.onStateChange(), DEFAULT_THROTTLE_WAIT); } render() { return (h(Host, { key: '7736911aa53d3e8763a7dd0a71fd72edf34e9d55' }, h("slot", { key: '667c99e44b9a358354ab436cef71788043062769', name: "left-slot" }), h("div", { key: '20ccf041d5282a6f6a07b509e9e3e57ab7c4f898', class: { 'switch-button': true, 'switch-button--disabled': this.disabled, 'switch-button--on': this.checked, [`switch-button--${this.size}`]: true }, part: "button", onClick: this.onSwitchClick }, h("div", { key: '7e9206b97182a94f1fb74b490ee4e06396e6db33', class: "icon-square", part: "icon-square" }, h("kv-icon", { key: '6130a01087284d9fc14c8954d35d54d019f9d2d0', name: EIconName.CheckState, part: "icon-svg" }))), h("slot", { key: '17367d117ac247c86990d4f6ef59c2dafa114ec0', name: "right-slot" }))); } static get is() { return "kv-switch-button"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["switch-button.scss"] }; } static get styleUrls() { return { "$": ["switch-button.css"] }; } static get properties() { return { "disabled": { "type": "boolean", "attribute": "disabled", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "inheritdoc", "text": undefined }], "text": "(optional) If `true` the button is disabled. Default `false`" }, "getter": false, "setter": false, "reflect": true, "defaultValue": "false" }, "checked": { "type": "boolean", "attribute": "checked", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "inheritdoc", "text": undefined }], "text": "(optional) If `true` the button is ON. Default `false`" }, "getter": false, "setter": false, "reflect": true, "defaultValue": "false" }, "size": { "type": "string", "attribute": "size", "mutable": false, "complexType": { "original": "EComponentSize", "resolved": "EComponentSize.Large | EComponentSize.Small", "references": { "EComponentSize": { "location": "import", "path": "../../utils/types", "id": "src/utils/types/index.ts::EComponentSize" } } }, "required": false, "optional": false, "docs": { "tags": [{ "name": "inheritdoc", "text": undefined }], "text": "(optional) Button's size. Default `EComponentSize.Large`" }, "getter": false, "setter": false, "reflect": false, "defaultValue": "EComponentSize.Large" } }; } static get events() { return [{ "method": "switchChange", "name": "switchChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [{ "name": "inheritdoc", "text": undefined }], "text": "Emitted when switch's state changes" }, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} } }]; } }