UNPKG

@postnord/web-components

Version:

PostNord Web Components

274 lines (273 loc) 10.5 kB
/*! * Built with Stencil * By PostNord. */ import { h, Host } from "@stencil/core"; import { uuidv4 } from "../../../index"; /** * The `pn-toggle-switch` is built with a native `input[type="checkbox"]` in the background. * This means you can use native events to listen to the changes. * * @nativeChange Use the `change` event to listen when the switch state is being toggled. * * @slot helpertext - Set the helpertext with a slot instead of prop. Use a `span` element as wrapper. * Only use if you need HTML content, such as a link. */ export class PnToggleSwitch { constructor() { this.label = undefined; this.helpertext = undefined; this.checked = false; this.toggleid = this.id; this.name = null; this.value = null; this.small = false; this.large = false; this.disabled = false; this.loading = false; } id = `pn-toggle-${uuidv4()}`; idHelper = `${this.id}-helper`; hostElement; handleHelperId() { if (this.id !== this.toggleid) this.idHelper = `${this.toggleid}-helper`; } componentWillLoad() { this.handleHelperId(); } hasHelpertextProp() { return !!this.helpertext; } hasHelpertextSlot() { return !!this.hostElement.querySelector('[slot="helpertext"]'); } showHelpertext() { return this.hasHelpertextProp() || this.hasHelpertextSlot(); } describedBy() { return this.showHelpertext() ? this.idHelper : null; } render() { return (h(Host, { key: 'eb01cdf69af35b174085bb005422ea6c8ac1e6a6' }, h("div", { key: '382f46a78c37378a688e984d01e65ce2f220edbc', class: "pn-toggle" }, this.label && (h("div", { key: 'd44b03002b18a367702fee99d6de2dc2e8d57d3b', class: "pn-toggle-content" }, h("label", { key: '065e6ad5f4b8061ac2dcf638d6494de28f574009', htmlFor: this.toggleid, class: "pn-toggle-label" }, h("span", { key: 'fe1da7bce587378eb568b907b8e083b000e8f902' }, this.label)), h("p", { key: '35d07fdf4d85beb676c2a4d4b66911cf9403a1b0', id: this.idHelper, class: "pn-toggle-helper", hidden: !this.showHelpertext() }, this.hasHelpertextProp() && h("span", { key: '3b730e80874b20385933b7e87c4d2298134bd420' }, this.helpertext), h("slot", { key: '7aec8e24f9c921667ccc8f9cd3098c03b9010a5b', name: "helpertext" })))), h("div", { key: 'e13b30af32359aaf51a7ed480a86e41bbb57bb96', class: "pn-toggle-button", "data-small": this.small, "data-large": this.large, "data-loading": this.loading }, h("input", { key: '49d52f49af798b9776aee2aa82c46c0d9e0ef6b6', id: this.toggleid, class: "pn-toggle-element", type: "checkbox", name: this.name, value: this.value, checked: this.checked, disabled: this.disabled, "aria-describedby": this.describedBy() }), h("div", { key: 'c8c6a22c5b69579691757998e2affc50949efc54', class: "pn-toggle-bg" }), h("div", { key: '087c3d9b47c828bd06b80b0529cd9ce644457355', class: "pn-toggle-circle" }, h("svg", { key: '9d4cc14302cc75917142c1e98ba93060ad85b5dd', class: "pn-toggle-check", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none" }, h("polyline", { key: 'f32ea9266474201495e4f461023528bc65bccd4d', points: "4,12 9,17 20,6", "stroke-width": "3" })), h("pn-spinner", { key: '3889ccc50a535e04d9bf91f5e3cb58d8458bc32a' })))))); } static get is() { return "pn-toggle-switch"; } static get originalStyleUrls() { return { "$": ["pn-toggle-switch.scss"] }; } static get styleUrls() { return { "$": ["pn-toggle-switch.css"] }; } static get properties() { return { "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "A label for the toggle switch." }, "attribute": "label", "reflect": false }, "helpertext": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Provide a helpertext for the toggle switch." }, "attribute": "helpertext", "reflect": false }, "checked": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Precheck the toggle." }, "attribute": "checked", "reflect": false, "defaultValue": "false" }, "toggleid": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Set a unique HTML id." }, "attribute": "toggleid", "reflect": false, "defaultValue": "this.id" }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML" }], "text": "HTML toggle name." }, "attribute": "name", "reflect": false, "defaultValue": "null" }, "value": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML" }], "text": "HTML toggle value." }, "attribute": "value", "reflect": false, "defaultValue": "null" }, "small": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Visual" }], "text": "Make the toggle smaller." }, "attribute": "small", "reflect": false, "defaultValue": "false" }, "large": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Visual" }], "text": "Make the toggle larger." }, "attribute": "large", "reflect": false, "defaultValue": "false" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "State" }], "text": "Disable the toggle." }, "attribute": "disabled", "reflect": false, "defaultValue": "false" }, "loading": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "State" }], "text": "Set the loading state." }, "attribute": "loading", "reflect": false, "defaultValue": "false" } }; } static get elementRef() { return "hostElement"; } static get watchers() { return [{ "propName": "toggleid", "methodName": "handleHelperId" }]; } } //# sourceMappingURL=pn-toggle-switch.js.map