@postnord/web-components
Version:
PostNord Web Components
274 lines (273 loc) • 10.5 kB
JavaScript
/*!
* 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: '9806bc50780935f442660701539941efd05d92d5' }, h("div", { key: 'a2babfadbf6443da619b35f9d8cb09a7b0bd4a59', class: "pn-toggle" }, this.label && (h("div", { key: '525510807f8f7e505b589c8055f277b196f813cd', class: "pn-toggle-content" }, h("label", { key: 'cc58536e57de5a852762de90c5edf0ad2f92a05f', htmlFor: this.toggleid, class: "pn-toggle-label" }, h("span", { key: 'a43dba51d8becebf1f508300ccb72530400535c7' }, this.label)), h("p", { key: 'd86c250e53ceb45830444a13a8f2c609173542de', id: this.idHelper, class: "pn-toggle-helper", hidden: !this.showHelpertext() }, this.hasHelpertextProp() && h("span", { key: '02a55f8a4c9da87b33b31ecd3d30ad6a73d70714' }, this.helpertext), h("slot", { key: '065675c0046d2cbdd581ef0c5ec745a5b03393eb', name: "helpertext" })))), h("div", { key: '6cbc83780caacb03d778aa2b9f882858231206ca', class: "pn-toggle-button", "data-small": this.small, "data-large": this.large, "data-loading": this.loading }, h("input", { key: '1a7062923c2668cb41b4c4b14f948c4074cf416e', 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: '3a85ff013cb29f5c7bef3d088ec782a53b3924ac', class: "pn-toggle-bg" }), h("div", { key: '955a78cfd16980bd437e03e51f3cde49185fe954', class: "pn-toggle-circle" }, h("svg", { key: 'fc0802cf056f5413c1228584c53abae8b21a8813', class: "pn-toggle-check", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none" }, h("polyline", { key: 'e881ac5ae0b5838293ac2e5af74a0c56ddd85687', points: "4,12 9,17 20,6", "stroke-width": "3" })), h("pn-spinner", { key: '78e5dd0484be70fc75315475daffed5b2e63c8bb', size: 1.125 }))))));
}
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