UNPKG

@postnord/web-components

Version:
449 lines (448 loc) 17 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. {@since v7.12.0} */ export class PnToggleSwitch { id = `pn-toggle-${uuidv4()}`; idHelper = `${this.id}-helper`; hostElement; /** A label for the toggle switch. */ label; /** Provide a helpertext for the toggle switch. */ helpertext; /** HTML toggle value. @category Native attributes */ value = null; /** HTML toggle name. @category Native attributes */ name = null; /** Precheck the toggle. @category Native attributes */ checked = false; /** Set the switch as required. @since v7.25.0 @category Native attributes */ required = false; /** Disable the toggle. @category Native attributes */ disabled = false; /** Make the toggle smaller. @category Features */ small = false; /** Make the toggle larger. @category Features */ large = false; /** Set the loading state. @category Features */ loading = false; /** Set a custom ID for the switch. @since v7.25.0 @category HTML attributes */ pnId; /** * Provide the label via an aria attribute. * We strongly recommend you use the `label` prop instead. * @since v7.25.0 * @category HTML attributes */ pnAriaLabel; /** * Provide the label from another element via its ID. * We strongly recommend you use the `label` prop instead. * @since v7.25.0 * @category HTML attributes */ pnAriaLabelledby; /** Set a custom ID for the switch. @deprecated Use `pn-id` instead. @category HTML attributes */ toggleid; handleHelperId() { this.idHelper = `${this.getId()}-helper`; } getId() { return this.pnId || this.toggleid || this.id; } getSpinnerSize() { if (this.small) return 1; if (this.large) return 1.25; return 1.125; } hasHelpertextProp() { return !!this.helpertext; } hasHelpertextSlot() { return !!this.hostElement.querySelector('[slot="helpertext"]'); } showHelpertext() { return this.hasHelpertextProp() || this.hasHelpertextSlot(); } describedBy() { return this.showHelpertext() ? this.idHelper : null; } displayLabel() { return !!this.label; } getAriaLabel() { return !this.displayLabel() && !this.pnAriaLabelledby ? this.pnAriaLabel : null; } getAriaLabelledby() { return !this.displayLabel() && !this.pnAriaLabel ? this.pnAriaLabelledby : null; } render() { return (h(Host, { key: 'a3363d23f18ca6fd7e9240dcaa46946cda07dd7b' }, h("div", { key: '203140db6d9953527ba4f15e7289bffb2e69a2a1', class: "pn-toggle" }, h("div", { key: 'dac906f74e8aa9899bcb1c99ee53e82695f5a280', class: "pn-toggle-content" }, this.label && (h("label", { key: 'fecf18137bbec21e45feb4380f6c0ebdd0dae01a', htmlFor: this.getId(), class: "pn-toggle-label" }, h("span", { key: '176e29c2756be27e0975690fe0d5f555b382ef6f' }, this.label))), h("p", { key: '865da739546b4f5214fdb63d22a1ea23f1029203', id: this.idHelper, class: "pn-toggle-helper", hidden: !this.showHelpertext() }, this.hasHelpertextProp() && h("span", { key: 'c958031faa669b81215300bd4bc2b31fed018e1a' }, this.helpertext), h("slot", { key: '01267d6e878a089a7174e2747cd232fd9a250f28', name: "helpertext" }))), h("div", { key: 'e3f74dd8febd161788f7a9d197e4216cfa00513e', class: "pn-toggle-button", "data-small": this.small, "data-large": this.large, "data-loading": this.loading }, h("input", { key: '2e9627c525e29bf561072b6d5578b379cba5f597', id: this.getId(), class: "pn-toggle-element", type: "checkbox", name: this.name, value: this.value, checked: this.checked, required: this.required, disabled: this.disabled, "aria-label": this.getAriaLabel(), "aria-labelledby": this.getAriaLabelledby(), "aria-describedby": this.describedBy() }), h("div", { key: '45c2b0269215a6ffa48654541f81d70f33eb57d3', class: "pn-toggle-bg" }), h("div", { key: '684e373a709db9877993c18f29eefc68e6f74585', class: "pn-toggle-circle" }, h("svg", { key: 'e918edb5254603543bc20d639164c05f56db1c54', class: "pn-toggle-check", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none" }, h("polyline", { key: '4f5441441932cd5e645686a8226ddd6a9744b47c', points: "4,12 9,17 20,6", "stroke-width": "3" })), h("pn-spinner", { key: 'a0d94d932142f174c009bf0fb61d23714f9f700d', size: this.getSpinnerSize() })))))); } 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": false, "optional": true, "docs": { "tags": [], "text": "A label for the toggle switch." }, "getter": false, "setter": false, "reflect": false, "attribute": "label" }, "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." }, "getter": false, "setter": false, "reflect": false, "attribute": "helpertext" }, "value": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Native attributes" }], "text": "HTML toggle value." }, "getter": false, "setter": false, "reflect": false, "attribute": "value", "defaultValue": "null" }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Native attributes" }], "text": "HTML toggle name." }, "getter": false, "setter": false, "reflect": false, "attribute": "name", "defaultValue": "null" }, "checked": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Native attributes" }], "text": "Precheck the toggle." }, "getter": false, "setter": false, "reflect": false, "attribute": "checked", "defaultValue": "false" }, "required": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "since", "text": "v7.25.0" }, { "name": "category", "text": "Native attributes" }], "text": "Set the switch as required." }, "getter": false, "setter": false, "reflect": false, "attribute": "required", "defaultValue": "false" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Native attributes" }], "text": "Disable the toggle." }, "getter": false, "setter": false, "reflect": false, "attribute": "disabled", "defaultValue": "false" }, "small": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Features" }], "text": "Make the toggle smaller." }, "getter": false, "setter": false, "reflect": false, "attribute": "small", "defaultValue": "false" }, "large": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Features" }], "text": "Make the toggle larger." }, "getter": false, "setter": false, "reflect": false, "attribute": "large", "defaultValue": "false" }, "loading": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "Features" }], "text": "Set the loading state." }, "getter": false, "setter": false, "reflect": false, "attribute": "loading", "defaultValue": "false" }, "pnId": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "since", "text": "v7.25.0" }, { "name": "category", "text": "HTML attributes" }], "text": "Set a custom ID for the switch." }, "getter": false, "setter": false, "reflect": false, "attribute": "pn-id" }, "pnAriaLabel": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "since", "text": "v7.25.0" }, { "name": "category", "text": "HTML attributes" }], "text": "Provide the label via an aria attribute.\nWe strongly recommend you use the `label` prop instead." }, "getter": false, "setter": false, "reflect": false, "attribute": "pn-aria-label" }, "pnAriaLabelledby": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "since", "text": "v7.25.0" }, { "name": "category", "text": "HTML attributes" }], "text": "Provide the label from another element via its ID.\nWe strongly recommend you use the `label` prop instead." }, "getter": false, "setter": false, "reflect": false, "attribute": "pn-aria-labelledby" }, "toggleid": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "deprecated", "text": "Use `pn-id` instead." }, { "name": "category", "text": "HTML attributes" }], "text": "Set a custom ID for the switch." }, "getter": false, "setter": false, "reflect": false, "attribute": "toggleid" } }; } static get elementRef() { return "hostElement"; } static get watchers() { return [{ "propName": "pnId", "methodName": "handleHelperId" }, { "propName": "toggleid", "methodName": "handleHelperId", "handlerOptions": { "immediate": true } }]; } }