UNPKG

@trimble-oss/moduswebcomponents

Version:

Modus Web Components is a modern, accessible UI library built with Stencil JS that provides reusable web components following Trimble's Modus design system. This updated version focuses on improved flexibility, enhanced theming options, comprehensive cust

344 lines (343 loc) 12.5 kB
import { h, Host, } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-switch.tailwind"; import { DAISY_TO_MODUS_LABEL_SIZE } from "../constants"; import { inheritAriaAttributes } from "../utils"; /** * A customizable switch component */ export class ModusWcSwitch { constructor() { this.inheritedAttributes = {}; /** Custom CSS class to apply to the inner div. */ this.customClass = ''; /** The disabled state of the switch. */ this.disabled = false; /** The indeterminate state of the switch. */ this.indeterminate = false; /** Name of the form control. Submitted with the form as part of a name/value pair. */ this.name = ''; /** A value is required for the form to be submittable. */ this.required = false; /** The size of the input. */ this.size = 'md'; /** The value of the switch. */ this.value = false; this.handleBlur = (event) => { this.inputBlur.emit(event); }; this.handleFocus = (event) => { this.inputFocus.emit(event); }; this.handleInput = (event) => { this.inputChange.emit(event); }; } componentDidRender() { const checkbox = this.el.querySelector('input[type="checkbox"]'); if (checkbox) { checkbox.indeterminate = this.indeterminate; } } componentWillLoad() { if (!this.el.ariaLabel) { this.el.ariaLabel = 'Switch button'; } this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-toggle']; const propClasses = convertPropsToClasses({ size: this.size }); // The order CSS classes are added matters to CSS specificity if (propClasses) classList.push(propClasses); if (this.customClass) classList.push(this.customClass); return classList.join(' '); } render() { const labelSize = this.size && DAISY_TO_MODUS_LABEL_SIZE[this.size]; return (h(Host, { key: 'b16a7b3a221b9ee5e020d9819d846ee15673dde8', class: "modus-wc-switch-host" }, h("input", Object.assign({ key: 'e6bff5d7c8c45ebd624f994658043841cbb6ffca', "aria-checked": this.indeterminate ? 'mixed' : this.value, "aria-disabled": this.disabled, checked: this.value, class: this.getClasses(), disabled: this.disabled, id: this.inputId, name: this.name, onBlur: this.handleBlur, onFocus: this.handleFocus, onInput: this.handleInput, required: this.required, role: "switch", tabIndex: this.inputTabIndex, type: "checkbox" }, this.inheritedAttributes)), this.label && (h("modus-wc-input-label", { key: 'a071923c913151436feafa001e03916ba68b23c0', forId: this.inputId, labelText: this.label, required: this.required, size: labelSize })))); } static get is() { return "modus-wc-switch"; } static get originalStyleUrls() { return { "$": ["modus-wc-switch.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-switch.css"] }; } static get properties() { return { "customClass": { "type": "string", "attribute": "custom-class", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Custom CSS class to apply to the inner div." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "disabled": { "type": "boolean", "attribute": "disabled", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The disabled state of the switch." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "false" }, "indeterminate": { "type": "boolean", "attribute": "indeterminate", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The indeterminate state of the switch." }, "getter": false, "setter": false, "reflect": true, "defaultValue": "false" }, "inputId": { "type": "string", "attribute": "input-id", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The ID of the input element." }, "getter": false, "setter": false, "reflect": false }, "inputTabIndex": { "type": "number", "attribute": "input-tab-index", "mutable": false, "complexType": { "original": "number", "resolved": "number | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The tabindex of the input." }, "getter": false, "setter": false, "reflect": false }, "label": { "type": "string", "attribute": "label", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The text to display within the label." }, "getter": false, "setter": false, "reflect": false }, "name": { "type": "string", "attribute": "name", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Name of the form control. Submitted with the form as part of a name/value pair." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "required": { "type": "boolean", "attribute": "required", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "A value is required for the form to be submittable." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "false" }, "size": { "type": "string", "attribute": "size", "mutable": false, "complexType": { "original": "ModusSize", "resolved": "\"lg\" | \"md\" | \"sm\" | undefined", "references": { "ModusSize": { "location": "import", "path": "../types", "id": "src/components/types.ts::ModusSize" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "The size of the input." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'md'" }, "value": { "type": "boolean", "attribute": "value", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The value of the switch." }, "getter": false, "setter": false, "reflect": true, "defaultValue": "false" } }; } static get events() { return [{ "method": "inputBlur", "name": "inputBlur", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the input loses focus." }, "complexType": { "original": "FocusEvent", "resolved": "FocusEvent", "references": { "FocusEvent": { "location": "global", "id": "global::FocusEvent" } } } }, { "method": "inputChange", "name": "inputChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the input value changes." }, "complexType": { "original": "InputEvent", "resolved": "InputEvent", "references": { "InputEvent": { "location": "global", "id": "global::InputEvent" } } } }, { "method": "inputFocus", "name": "inputFocus", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the input gains focus." }, "complexType": { "original": "FocusEvent", "resolved": "FocusEvent", "references": { "FocusEvent": { "location": "global", "id": "global::FocusEvent" } } } }]; } static get elementRef() { return "el"; } }